Hi,

I'm in need of some pseudo code helping me with following:

I have an Array of x members each 5 dwords in size. The first member is the ID and the second one is another ID in case the first ID is available multiple times.

It's looking like this:

5 - 1 - 0 - 0 - 0
2 - 2 - 0 - 0 - 0
2 - 1 - 0 - 0 - 0
4 - 1 - 0 - 0 - 0
2 - 3 - 0 - 0 - 0

Now I would like to sort this and get this array from the lowest to the highest taking in consideration the second ID too.

Like the result should be:

2 - 1 - 0 - 0 - 0
2 - 2 - 0 - 0 - 0
2 - 3 - 0 - 0 - 0
4 - 1 - 0 - 0 - 0
5 - 1 - 0 - 0 - 0

Now I tried exchanging, position saving in another table and so on.. but I seem to f* up whatever I do.. (bad planning is the cause - came to realize that)

Could someone just come up with some easy to read pseudo code for me so that I finally get a decent headstart?
Posted on 2004-06-24 10:57:33 by JimmyClif
What about insertsort? You search for the lowest value, then insert it into another array, the proceed on to find a bigger value or something like that?
Posted on 2004-06-24 11:00:22 by roticv
how about 'bouble' sort algorithm
Posted on 2004-06-24 11:43:17 by wizzra
Well, I don't care about speed for one. And modifying an existing algo to move my 5 dwords with it just seems more complicated than actually wrting this thing. Especially considering that I hardly ever expect more than 20 members in that array :)

How does this look?



mov edi, o$ NewArray
mov esi, o$ Array
m2m LoopingCounter, NumberOfArrayMembers

A0:
mov ebx, o$ Array
mov ecx, NumberOfArrayMembers
A1:
mov eax, d$ [ebx]
mov esi, ebx ; save position of that value
jmp A3
A2:
add ebx, 20 ; next record
cmp eax, d$ [ebx]
jb A1
A3:
loop A2

push esi
mov ecx, 5
rep movsd
pop esi
mov D$ [esi], -1 ; copied record gets highest ID we won't grab it again

dec LoopingCounter
jnz A0
Posted on 2004-06-24 12:24:28 by JimmyClif
This is what I would do if I were in your situation: use qsort() from MSVCRT.DLL or other C libraries, with comparison function like


/* C-like pseudo-code */
int compare_function(void *a, void *b)
{
your_data_type *d1 = *((your_data_type *)a);
your_data_type *d2 = *((your_data_type *)b);
if (d1->id1 > d2->id1) return 1;
else if (d1->id1 < d2->id1) return -1;
else {
/* d1->id1 == d2->id1, now compare id2 */
if (d1->id2 > d2->id2) return 1;
else if (d1->id2 < d2->id2) return -1;
else {
/* until you exhaust all your sort keys. */
...
}
}
}

It should be fairly straightforward to convert it to assembly code.


Oh, BTW, quick sort itself has been posted multiple times in this board, so you don't actually need MSVCRT.DLL. You only need to modify the posted code to suit your need.
Posted on 2004-06-24 19:01:47 by Starless
I would simply sort them first according to the 2nd member and then resort the result according to the 1st member. Just try it on paper, it works.

Raymond.
Posted on 2004-06-24 23:00:22 by Raymond