944,052 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1422
  • C RSS
Aug 8th, 2006
0

Listing in numeric order

Expand Post »
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.
Similar Threads
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
sgriffiths is offline Offline
61 posts
since Jun 2006
Aug 8th, 2006
1

Re: Listing in numeric order

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Aug 8th, 2006
1

Re: Listing in numeric order

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
Reputation Points: 38
Solved Threads: 3
Junior Poster in Training
Eddy Dean is offline Offline
56 posts
since Jul 2006
Aug 8th, 2006
0

Re: Listing in numeric order

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
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Aug 8th, 2006
0

Re: Listing in numeric order

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
Reputation Points: 38
Solved Threads: 3
Junior Poster in Training
Eddy Dean is offline Offline
56 posts
since Jul 2006
Aug 10th, 2006
0

Re: Listing in numeric order

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".)
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: wat is flag??pls...can someone explain it to me!!
Next Thread in C Forum Timeline: Help me out in Quadratic equation problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC