Listing in numeric order

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2006
Posts: 61
Reputation: sgriffiths is an unknown quantity at this point 
Solved Threads: 0
sgriffiths sgriffiths is offline Offline
Junior Poster in Training

Listing in numeric order

 
0
  #1
Aug 8th, 2006
Hello

i have a a struct which contains the below

1
10
11
2
3

But i need this to be in numeric order, ie

1
2
3
10
11

below is my code

  1. do {
  2. fprintf(fReport, "List Name = %d\n", LIST[t].name );
  3. t++;
  4.  
  5. } while( t != k )

How can i bring them back in numeric order?
Last edited by sgriffiths; Aug 8th, 2006 at 9:12 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,758
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Listing in numeric order

 
1
  #2
Aug 8th, 2006
You will have to sort all the information you want to print out before you print the information.

Obtain all items to print.
Sort items to print.
Print items.

The process that does the sorting can be any of a number of protocols including bubble sort, quick sort, insert sort, the sort algorhythms from the Standard Template Library, etc. Which sorting protocol is best for you depends on your knowledge base, how many items you have to sort, how random the original series is, etc, etc.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 56
Reputation: Eddy Dean is an unknown quantity at this point 
Solved Threads: 3
Eddy Dean Eddy Dean is offline Offline
Junior Poster in Training

Re: Listing in numeric order

 
1
  #3
Aug 8th, 2006
Wikipedia has a whole page dedicated to sorting algorithms:

http://en.wikipedia.org/wiki/Sorting_algorithm

Snippet taken from the Dutch wikipedia, "invoer" is the array you want to sort and lengte is the number of values in the array

  1. void bubblesort(int invoer[],int lengte){
  2. int i,j,tijdelijk;
  3. for(j=0;j<lengte-1;j++){
  4. for(i=1;i<lengte-j;i++){
  5. if(invoer[i-1]>invoer[i]){
  6. tijdelijk=invoer[i];
  7. invoer[i]=invoer[i-1];
  8. invoer[i-1]=tijdelijk;
  9. }
  10. }
  11. }
  12. }

note that this is a really in-efficient way of sorting. I once executed it on a 14000 number array. Took several minutes.

this:
  1. void straightselection(int invoer[],int lengte){
  2. int i,j,kleinste,tijdelijk;
  3. for(j=0;j<lengte-1;j++){
  4. kleinste=j;
  5. for(i=j+1;i<lengte;i++){
  6. if(invoer[i]<invoer[kleinste]) kleinste=i;
  7. }
  8. if(kleinste!=j){
  9. tijdelijk=invoer[j];
  10. invoer[j]=invoer[kleinste];
  11. invoer[kleinste]=tijdelijk;
  12. }
  13. }
  14. }

is also taken from the Dutch wikipedia. It is a straight selection sort algorithm. It looks for the smallest item in the array and puts it on the end of the array. You might want to improve this algorithm by letting it search for the smallest and the largest at the same time.


Goodluck,
Eddy
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Listing in numeric order

 
0
  #4
Aug 8th, 2006
If u want the fastest algorithm performance wise, quicksort is one of them. Even though its a recursive algorithm but still does the trick nonetheless considering the ulimited size of stack.

If u want the completer source code maybe this would help
http://linux.wku.edu/~lamonml/algor/sort/quick.html
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 56
Reputation: Eddy Dean is an unknown quantity at this point 
Solved Threads: 3
Eddy Dean Eddy Dean is offline Offline
Junior Poster in Training

Re: Listing in numeric order

 
0
  #5
Aug 8th, 2006
If the list to sort will only consist of integers and you know the smallest and the largest value you can also use counting sort. Rather fast, very easy.


Greetz, Eddy
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Listing in numeric order

 
0
  #6
Aug 10th, 2006
You can use the standard C function qsort(), which, despite its name, doesn't have to use a quicksort algorithm, just a sorting algorithm.

If you want to just sort a couple of numbers then you could use the bubble sort (slow but simple). (Google for "bubble sort".)
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1282 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC