i have been doing my research and i cant find out how to sort numbers in C. can someone tell me the code please
i have been doing my research and i cant find out how to sort numbers in C. can someone tell me the code please
There's lots of ways to sort: bubble sort, quicksort, heapsort, mergesort, ................ There's nothing C specific about sorting. (Except perhaps that the standard C library does contain a quicksort implementation.)
You want to put the elements into an array, then call a function that compares each element to the next one in a for loop, reverses them if necessary, and trips a switch to indicate a change was made. If the switch has been tripped by the end of the for, the function calls itself recursively (using the newly ordered array) until it makes it through the loop without tripping the switch -- meaning the array is now sorted.
for character strings, have a look at this:
http://www.intergate.com/~halfcountp...phabetize.html
Last edited by MK27; 10-07-2008 at 10:16 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
this is a simplified illustration with numbers:
Code:#include <stdio.h> void sortnumbers (short int *ray) { short int sw=0, i, tmp; for (i=0;i<9;i++) { // don't compare 10th to 10th+1... if (ray[i] > ray[i+1]) { tmp=ray[i]; ray[i]=ray[i+1]; ray[i+1]=tmp; sw++; } } if (sw > 0) sortnumbers(ray); } int main (int argc, char *argv[]) { short int n[10], i, x=0; for (i=32; i>2; i-=3) {n[x]=i;x++;} for (i=0;i<10;i++) printf("%d ",n[i]); sortnumbers(n); puts("sorted:"); for (i=0;i<10;i++) printf("%d ",n[i]); }
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Unless you have special requirements on the sort algorithm, use qsort(). Note that the cppreference.com page I linked to has an example that sorts integers, but you need to #include <stdlib.h>, not <cstdlib>, since this is C, not C++. This is what tabstop meant by hinting that "the standard C library does contain a quicksort implementation", though the C standard does not seem to actually mandate quicksort as the algorithm.Originally Posted by joker_tony
That sounds like a recursive bubble sort, which is vastly inferior to quicksort for general purpose sorting of an array.Originally Posted by MK27
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
its a matter of sorting faster. bubble sort is painfully slow
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter