News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Sorting algorithm question. Help plz ASAP!

Started by Nimue, November 30, 2009, 08:42:44 AM

Previous topic - Next topic

Nimue

http://pastebin.com/m5765b295

No idea why this sort isn't working!

Been debugging it for hours and it LOOKS like it should work.... If anyone can help me, that'd be great. I'm at my wit's end here.

This IS for a project, but this is the last part I actually need working and like... If someone can explain to me what's going wrong, I can fix it. Please, please help me.
This is the main looping code for sorting.

mov ecx,10000
LOOPSORTLOOP:
push ecx
mov ecx,numbercount
mov esi, offset randomnumbers
SORTLOOP:
mov eax,[esi]
add esi,8
mov ebx,[esi]
; cmp ebx,0
; je loopstart
cmp eax,ebx
jl lesser
loopstart:
loop SORTLOOP
pop ecx
loop LOOPSORTLOOP
call crlf
mov ecx, numbercount
mov esi, offset randomnumbers
PRINTLOOPER:
mov eax,[esi]
call writehex
call crlf
add esi, 8
loop PRINTLOOPER



lesser:
sub esi,8
mov [esi],ebx
add esi,8
mov [esi],eax
jmp loopstart

japheth


Hm, it looks like a simple bubblesort algorithm.

Are you aware that the number of compares must be 1 less than the number of items? That is, if <numbercount> contains the number of items, then you should do <numbercount-1> compares only!


hutch--

Thanks Japheth, I did not recognise what it was.

here is a link to a bubble sort that does work.

http://www.masm32.com/board/index.php?topic=8130.0
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

there is no need to adjust the index

        mov     eax,[esi]
        mov     ebx,[esi+4]
        cmp     eax,ebx
        jbe     no_swap  ;i use unsigned branch - JL is a signed branch - also - no swap if they are equal

        mov     [esi],ebx
        mov     [esi+4],eax

no_swap:

that way, when you get to the end, your pointer isn't screwed up
i assume you are sorting dwords - not bytes
there are a number of optimizations that can be made
here is a link to a 16-bit bubble-sort that i wrote to sort bytes - maybe you can get ideas
http://www.masm32.com/board/index.php?topic=12740.msg98448#msg98448

Nimue

I figured it out. It was that I was using writebin - which doesn't show sign. I was freaking out lol.