Hey SF, I just finished some code to generate 50 random floating points between 1-100. In the code, I have each floating point stored in an index from array[0] to array[49]. Is there ANY efficient way of sorting the floating points with an increasing value? For instance, the numbers generated would look like
2.4343 (stored in array[0])
56.298 (stored in array[1])
91.526 (stored in array[2])
35.781 (stored in array[3])
27.742 (stored in array[4])
...
I don't know how to sort each floating point without having use a loop like
Code:
for (x=0; x<=49; x++)
if (array[0]<array[x])
array[0]=array[x];
if (array[1]<array[x])
array[1]=array[x];
/*(all the way up to array[49])*/
Is there a better way to do this? Thanks in advance
there are dozens of better ways to do this, fastest is the shell sort which basically keeps breaking down your array into halves, then it decides which half is bigger and then inserts back into the array, all the way back up to the original, it looks like
Shell Sort (Sorting the array A[size])
Determine the number of segments by dividing the number of cells by two.
While the number of segments are greater than zero
{
For each segment, we do an Insertion Sort.
(think on how to write the loops here efficiently... )
Divide the number of segments by two.
}
void shellSort(int numbers[], int array_size)
{
int i, j, increment, temp;
PS : stole this from wikiPedia [Only registered and activated users can see links. ]
you could use a bubble sort too, just google it on wiki and it has the pseudo for you to try out, im pretty sure they even have the code for c.... lol
PPS: just found this [Only registered and activated users can see links. ], a list of several different possible algorithms
"I love those boobs that are all slapping around and hitting her in face and shit when your having sex with the girl its crazy!!!" --- ME, justnow.... lol
Last edited by trevor146 : 11-22-2009 at 07:06 PM.