Sorting Arrays???

This is a discussion on Sorting Arrays??? within the C Programming forums, part of the General Programming Boards category; I've just started c and java this semester and and I'm writting a small program that calculates your handicap. Basically ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    1

    Sorting Arrays???

    I've just started c and java this semester and and I'm writting a small program that calculates your handicap.

    Basically all my calculation gets put into an array. But I have no idea how to get the information from that array.

    The array holds 20 integers. I need to find the 10 smallest integers in the array.

    I've heard its called sorting but im not sure what needs to be done.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    There's this handy button called "search" which will find your answer if you type in some appropriate word.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    maybe use qsort() to sort the numbers first in ascending order, then grab the first 10 elements of the sorted array.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    11
    There are several ways of sorting arrays..

    I'll post one here, so you'll have an idea:

    Code:
    #include <stdio.h>
    
    #define MAX 10
    
    int main(void)
    {
        int array[MAX] = {2, 3, 4, 1, 7, 8, 9, 6, 0, 5};
    
        int backup;
        int sorted = 0;      /*0 = false, 1 = true, if array is sorted -> sorted = true */
        int i;
    
        // compare array[i] to array[i + 1], if array[i] > array[i + 1], switch them
    
        while(!sorted) {
    	sorted = 1;
    
            for (i = 0; i < MAX - 1; i++) 
     	    if (array[i] > array[i + 1]) {     /* switching values */
    	        backup = array[i];
    	        array[i] = array[i + 1];
    	        array[i + 1] = backup;
    
    		sorted = 0;			
                } 
        }	
        
        for(i = 0; i < MAX; i++)
    	printf("%d\n", array[i]);
    
        return 0;
    }

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    You could try for example Google to search for "sorting algorithms". It would lead you to sites like this: http://www.concentric.net/~ttwang/sort/sort.htm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Sorting Arrays
    By DaniiChris in forum C Programming
    Replies: 11
    Last Post: 08-03-2008, 08:23 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21