Security Site Forum  

Go Back   Security Site Forum > Technology > UNIX/Linux/Mac

Reply
 
Thread Tools Display Modes
sorting increasing #'s into array indices? (C)
Old
  (#1 (permalink))
SSF Active Member
 
cold_lucid's Avatar
 
Status: Offline
Posts: 181
Join Date: Jan 2008
Location: sietch tabr
sorting increasing #'s into array indices? (C) - 11-20-2009, 11:39 PM

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


Why drink and drive... when you can smoke and fly
   
Reply With Quote
 
Old
  (#2 (permalink))
SSF Senior Member
 
trevor146's Avatar
 
Status: Offline
Posts: 143
Join Date: Jun 2007
Location: Canada
11-22-2009, 06:58 PM

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;

increment = 3;
while (increment > 0)
{
for (i=0; i < array_size; i++)
{
j = i;
temp = numbers[i];
while ((j >= increment) && (numbers[j-increment] > temp))
{
numbers[j] = numbers[j - increment];
j = j - increment;
}
numbers[j] = temp;
}
if (increment/2 != 0)
increment = increment/2;
else if (increment == 1)
increment = 0;
else
increment = 1;
}
}


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.
   
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
help increasing max number of connections per program somethin420 Windows 95/98/Me/NT/2000/XP/Vista/Win7 0 12-29-2008 11:07 PM
Sorting your porn pictures ? Drac Discussions & Debates 9 02-08-2008 05:05 PM
Consequences of porn industry's increasing piracy problems MurrayH Discussions & Debates 11 10-06-2007 04:21 AM



Powered by vBulletin® Version 3.6.8 PL2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.

...... XXX adult password pass board forum
Stand With Haiti

Page generated in 0.20851588 seconds (21.81% PHP - 78.19% MySQL) with 16 queries