What does this code do?

EvanB

Senior member
Nov 3, 2001
268
0
0
Could someone explain to me what exactly this code does? I have been staring at it and seem to be missing the function of it.
Thanks in advance :)

 

EvanB

Senior member
Nov 3, 2001
268
0
0
How do you get the indents to work in the attack code box? It takes all the spacing out.
 

EvanB

Senior member
Nov 3, 2001
268
0
0
Ok, must have done it wrong. Thanks for fixing it. Any idea what that does?
 

Cerebus451

Golden Member
Nov 30, 2000
1,425
0
76
It's sorting the array

EDIT: Or are you looking for a detailed explaination as to how it is sorting?
 

manly

Lifer
Jan 25, 2000
13,346
4,102
136
Originally posted by: EvanB
How do you get the indents to work in the attack code box? It takes all the spacing out.
Obviously you are writing malware. :)
 

EvanB

Senior member
Nov 3, 2001
268
0
0
Cerebus451:

Well, I can see that it sorts the array, but I'm not that sure how. Is it recursive? It looks like it calls itself. I see that it loops through the array, but I don't know exactly how it does the sorting.

Evan
 

Cerebus451

Golden Member
Nov 30, 2000
1,425
0
76
Originally posted by: EvanB
Cerebus451:

Well, I can see that it sorts the array, but I'm not that sure how. Is it recursive? It looks like it calls itself. I see that it loops through the array, but I don't know exactly how it does the sorting.

Evan
Correct. The top part of the function allocates a new array equal to the size of the original. It then copies the original array into the temporary array and recursively calls itself to sort the temporary array in 2 halves. Once the 2 halves are sorted, it merges the sorted halves back into the original array (l is the index to the first sorted half, r is the index to the second sorted half. It simply walks up the 2 sides of the array, knowing that each side is sorted, taking the lower value from either side to place into the next slot of the original array). The case that breaks the recursion is when an array of 1 or 2 is sorted. If there are 2 elements, they are compared and swapped if necessary (1 element is automatically sorted).

This sort beats the crap out of your memory manager, but is somewhat efficient otherwise. There are much better ways to sort (like qsort).