Thread: help with my bubble sorting of arrays

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    4

    help with my bubble sorting of arrays

    Hi, I really need help with my bubble sorting code, i know the bubble sort is not a good one but i'm a beginner to this.
    if anyone can get the code to actually sort the code or at least tell me how to change the code so it works, it's my assignment which has to be next week sometime.. help
    #include<stdio.h>
    void datain(int, int[10000][4]);
    void dataout(int, int[10000][4]);
    void sortid( int, int[10000][4], int);
    void main()

    {
    int down, person[10000][4];

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

    /*validate user input*/
    while ((down <=0) || (down > 100000))
    {
    printf("please re-enter the number of people required:\n\r");
    scanf("%d",&down);
    }

    /*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[10000][4])
    {
    /*load up person arrays*/
    int array1;


    for (array1 = 1; array1 <= numberofpeople; array1 ++)
    {
    printf("\n\rPlease input the person identification: ");
    scanf("%d",&person[array1][1]);

    printf("Please input the person Wage: ");
    scanf("%d",&person[array1][2]);

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

    printf("Please input the person Age: ");
    scanf("%d",&person[array1][4]);
    /*validate user input*/
    while ((person[array1][4] < 16) || (person[array1][4] > 65))
    {
    printf("please re-enter the age data\n\r");
    scanf("%d",&person[array1][4]);
    }
    }

    }
    /*print out database unsorted*/
    void dataout (int number,int person[10000][4])
    {
    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 ++)
    {

    printf("%10d%10d%10d%10d\n\r",person[array][1],person[array][2],person[array][3],person[array][4]);

    }

    }
    void sort( int number, int person[10000][1], 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");
    scanf("\n%c",&words);

    /*sort the array*/ /*here is the bit that doesn't work*/
    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)");
    scanf("%d",&nochmal);
    }
    }
    }
    }
    }

  2. #2
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Hi!

    I've taken a peek at your code, and I noticed a few things:

    1 - validation of user input checks for the wrong range (unimportant, of course)

    2 - array indices in C start at 0; in this case you keep referencing things like person[array1][4], which doesn't exist.

    3a - void sortid( int, int[10000][4], int); is declared (and called upon in the program), but the function you supply is void sort( int number, int person[10000][1], int array): wrong name and the "1" should be "4" (?)

    3b - sortid has only INPUT parameters and no pointers or return value, so it can only "communicate" through global variables. You should change the declaration, for example: void sortid( int number, int *(person[4]), int array);, and change the body of the function accordingly.

    3c - your datain function has the same problem

    4 - you need two loops to implement the bubble sort algorithm.

    5 - you sort by exchanging elements of the array (of arrays) person... You should also exchange the satellite data (i.e. keep ID, sex, wage and age together).

    Sorry, too many errors to give real help...

    alex

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. bubble sorting in linked list
    By anu in forum C Programming
    Replies: 0
    Last Post: 10-17-2001, 03:21 PM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM