Thread: help with sorting arrays

  1. #1
    Registered User
    Join Date
    Dec 2001

    help with sorting arrays

    Hello I've taken some of the advice and corrected some of the errors from the reply but i need the bubble sort explaining in a bit more detail, heres the code and thanks in advance for looking at it whom ever does.

    void datain(int, int[10000][3]);
    void dataout(int, int[10000][3]);
    void sortid( int, int *([3]), int);
    void main()


    int down, person[10000][3];

    /*choose the size of table required*/
    printf("Please enter the amount of the people required: \n\r");
    printf("Amount of people required: %d\n\r",down);

    /*validate user input*/
    while ((down <=0) || (down > 10000))
    printf("please re-enter the number of people required:\n\r");

    /*Create database and input data*/
    datain(down, person);

    /*print out database unsorted*/
    dataout(down, person);

    /*sort the personnel ID array*/
    sortid(down, person, array);

    void datain( int numberofpeople, int *(person[3]), int array)
    /*load up person arrays*/
    int array1;

    for (array1 = 1; array1 <= numberofpeople; array1 ++)
    printf("\n\rPlease input the person identification: ");

    printf("Please input the person Wage: ");

    printf("Please input the person Sex(0 is male and 1 is female): ");
    /*validate user input*/
    while ((person[array1][2] < 0) || (person[array1][2] > 1))
    printf("please re-enter the sex data\n\r");

    printf("Please input the person Age: ");
    /*validate user input*/
    while ((person[array1][3] < 16) || (person[array1][3] > 65))
    printf("please re-enter the age data\n\r");

    /*print out database unsorted*/
    void dataout (int number,int person[10000][3])
    int array;
    printf(" \n\r The Unsorted Data:\n");
    printf(" ---------------------\n\r");
    printf("Personnal ID Wage Sex Age\n\r");
    for (array = 1; array <= number; array ++)



    }/*here is the bit that goes wrong*/
    void sortid( int number, int *(person[3]), int array)
    /*select display from user*/
    char words;
    char nochmal;/* this means 'again' in german*/
    nochmal = 1;
    while (nochmal == 1)

    printf("\n\rSelect your form of selection ?");
    printf("\n\rAscending by Personnel ID,type id");
    printf("\n\rAscending by Wage, type wage");
    printf("\n\rAscending by Age, type age");
    printf("\n\rFemale data only, print f");
    printf("\n\rMale data only, print m\n\r");

    /*sort the array*/
    register int index, array;

    for (index = 1; index < number; -- index){
    if (person[array-1][1] > person[array][1]) {

    /* exchange elements */
    index = person[array-1][1];
    person[array-1][1] = person[array][1];
    person[array][1] = index;

    /*to resort the data*/
    printf("Do you want to sort the database again? Yes (y) or No (n)");

  2. #2
    Here is how a bubblesort works. Understand this concept should allow you to correct your problem.


    list of numbers to sort, called 'array[]'.

    num1 = first index (starts at 0)
    num2 = next index (always num1+1)

    if(array[num2 ]> array[num1])
    swap num1 and num 2 positions.
    num1 = 0; /* a changed occurred, start at top of list again */
    num2 = num1+1;

    if(num1 == last_item_in_array) /* are done yet? */
    yes, we're done, bail
    no, keep loop running


    Essentially, the pseudocode above does this-- start at beginning of array. Is the second element greater than the first? If yes, swap them and start over. If not, let's look at the next pair. Keep going until we've reached end of array.

    We will never reach the end of the array, until all items are sorted as desired. This allows each time we sort (or swap, rather) to 'bubble' the correct numbers up to their proper positions.


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, 04:07 PM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM