Thread: how do you sort in c

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    62

    how do you sort in c

    i have been doing my research and i cant find out how to sort numbers in C. can someone tell me the code please

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joker_tony
    i have been doing my research and i cant find out how to sort numbers in C. can someone tell me the code please
    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.

    Quote Originally Posted by MK27
    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.
    That sounds like a recursive bubble sort, which is vastly inferior to quicksort for general purpose sorting of an array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    That sounds like a recursive bubble sort, which is vastly inferior to quicksort for general purpose sorting of an array.
    Do you mean in terms of speed and memory use? After all, either it's sorted right, or it isn't.
    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

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    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

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Take a look at this. Its a safe executeable, don't worry. It was written by iMalc.

    If you play with it for a bit you will soon realize that perhaps the speed differencial is significant.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  4. radix sort and radix exchange sort.
    By whatman in forum C Programming
    Replies: 1
    Last Post: 07-31-2003, 12:24 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM