Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Urgent help need Bubble sort

  1. Apr 2, 2009 #1
    Urgent help need !! Bubble sort !!

    hi all, I was asked to built a C programme using bubble sort. here is the Question.
    TASK



    A PCB manufacturing company requires a software program to convert a series of a table of drill data to minimise the movement of the drill bed to speed up production i.e.to place all the holes near each other row by row. This requires sorting out a series of X coordinates points in ascending/descending order.
    You are required to write an ANSI C visual studio program to provide a numerical bubble sort, to a set of data. The user should be able to select from a menu system the type of bubble sort (ascending or descending), data input and final data display i.e. four options. The program should be user friendly and coded in best programming style: no global data, good use of functions and structured commenting. You are required to submit a word document report comprising of the final source code, sample test screen dumps and indications of limitations/enhancements of your program. Below is shown an example of basic pseudo code to help with the bubble sort approach to be used.
    Pseudo code

    Main
    Define storage array input and output
    Define menu
    Option 1: Call array fill for input
    Option 2: Call descending bubble sort on input for X data only
    Option 3: Call ascending bubble sort on input for X data only
    Option 4: Call display array output
    Array fill
    Input: Array size, Input arrays
    Output: Array full of real numbers i.e. floats
    Function: Single dimension loop using stdio to input data
    Sort
    Input: Array size, Input array, Output array, type of sort a or d
    Output: Output array with sorted data
    Function: Principle used bubble sort
    Compare current value in array to array+1 element
    Sort A: If value is greater than value+1 then swap over elements else carry on to next element
    Sort D: If value is less than value+1 then swap over elements else carry on to next element
    Repeat till you have compared all elements and no swaps have occurred and then return back the final output array

    Can someone please check if the undermentioned answer is correct for the above question and can you simplify more if it is possible; any new answers are wellcome !

    Work Done So far;

    #include<stdio.h>
    #define NUM 4
    void array_direction(int ar[],char direction);
    int main (void)
    {
    int n;
    int x[NUM];
    char order;
    int y;

    printf("This is the assignment 2 of Softwares for engineers \n");

    printf("Welcome to the PCB Company :\n");
    printf("Please enter the values of x coordinate to be sorted:\n");
    for(n=0;n< NUM;n++)
    {
    printf(" please enter X Value %d:",n+1);
    scanf("%3d",&x[n]);

    }
    printf(" How do you want your values of X coordinate sorted\n"); fflush(stdin);
    printf("Pease enter A for ascending order or D for descending order\n:");
    scanf("%c",&order);
    array_direction(x, order);
    printf(" The order is:\n");
    for(y=0;y< NUM;y++)
    {
    printf("%3d, ",x[y]);
    }
    fflush(stdin);
    getchar();
    return 0;
    }
    void array_direction(int ar[],char direction)
    {
    int outer;
    int inner;
    int hold;
    /*by using bubble Sort*/

    /*For Ascending Order*/
    if( direction == 'a' || direction == 'A')
    {
    for (outer = 0;outer < NUM; outer++)
    {
    for(inner = 0;inner < NUM;inner++)
    {
    if (ar[inner] > ar[inner+1])
    {

    //swap
    hold = ar[inner];
    ar[inner] = ar[inner+1];
    ar[inner+1] = hold;
    }
    }
    }
    }







    /* For Descending Order */
    if(direction == 'd' || direction == 'D')
    {
    for (outer = 0;outer < NUM; outer++)
    {
    for(inner = 0;inner < NUM;inner++)
    {
    if(ar[inner] < ar[inner+1])
    {
    //swap
    hold = ar[inner];
    ar[inner] = ar[inner+1];
    ar[inner+1] = hold;
    }
    }
    }
    }
    }
     
  2. jcsd
  3. Apr 2, 2009 #2
    Re: Urgent help need !! Bubble sort !!

    Hello,

    Here is a link to one of the older C books from Numerical Recipes. Chapter 8 contains sorting algorithms. Check it out. If it is to complicated I can help you with some other code for a bubble sort.

    http://www.nrbook.com/a/bookcpdf.php

    Thanks
     
  4. Apr 2, 2009 #3
    Re: Urgent help need !! Bubble sort !!

    Thanks 4 da reply. i am new to this programming and i am not a Computer Student.

    Can someone let me know the indications of limitations/enhancements of the above program...?

    atleast 1 is enough thanks !
     
  5. Apr 2, 2009 #4

    rcgldr

    User Avatar
    Homework Helper

    Re: Urgent help need !! Bubble sort !!

    You might want to input the data from a file instead of user input. It will be easier to debug the program this way. Also, it's likely that the array will contain both x and y values. You could make an array of structures (since these can be assigned directly instead of having to use memcpy to move a pair of numbers).
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook