| | |
Listing in numeric order
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2006
Posts: 61
Reputation:
Solved Threads: 0
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
How can i bring them back in numeric order?
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
C Syntax (Toggle Plain Text)
do { fprintf(fReport, "List Name = %d\n", LIST[t].name ); t++; } while( t != k )
How can i bring them back in numeric order?
Last edited by sgriffiths; Aug 8th, 2006 at 9:12 am.
•
•
Join Date: Jul 2005
Posts: 1,758
Reputation:
Solved Threads: 283
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.
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.
•
•
Join Date: Jul 2006
Posts: 56
Reputation:
Solved Threads: 3
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
note that this is a really in-efficient way of sorting. I once executed it on a 14000 number array. Took several minutes.
this:
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
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
C Syntax (Toggle Plain Text)
void bubblesort(int invoer[],int lengte){ int i,j,tijdelijk; for(j=0;j<lengte-1;j++){ for(i=1;i<lengte-j;i++){ if(invoer[i-1]>invoer[i]){ tijdelijk=invoer[i]; invoer[i]=invoer[i-1]; invoer[i-1]=tijdelijk; } } } }
note that this is a really in-efficient way of sorting. I once executed it on a 14000 number array. Took several minutes.
this:
C Syntax (Toggle Plain Text)
void straightselection(int invoer[],int lengte){ int i,j,kleinste,tijdelijk; for(j=0;j<lengte-1;j++){ kleinste=j; for(i=j+1;i<lengte;i++){ if(invoer[i]<invoer[kleinste]) kleinste=i; } if(kleinste!=j){ tijdelijk=invoer[j]; invoer[j]=invoer[kleinste]; invoer[kleinste]=tijdelijk; } } }
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
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
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
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
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".)
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
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
![]() |
Similar Threads
- help with programming (C)
- Displaying News Articles (PHP)
- extracting the middle number of 3 inputs (C++)
Other Threads in the C Forum
- Previous Thread: wat is flag??pls...can someone explain it to me!!
- Next Thread: Help me out in Quadratic equation problem
Views: 1282 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api append array arrays bash binarysearch centimeter char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory drawing dynamic executable feet fflush file floatingpointvalidation fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide infiniteloop initialization interest kilometer lazy license linked linkedlist linux linuxsegmentationfault list match matrix meter microsoft multi mysql oddnumber odf open openwebfoundation pattern pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scheduling segmentationfault send shape single socketprogramming spoonfeeding stack standard strchr string strings structures student suggestions system test testautomation unix urboc user voidmain() win32 win32api windows.h






