Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Help with a bubble sort

  • 22-12-2010 2:09pm
    #1
    Registered Users, Registered Users 2 Posts: 1,595 ✭✭✭


    Hi guys,

    Hoping someone can look at my code and tell me what im doing wrong, program compiles but dosnt work. Basically ive written a function to bubble sort an array of floats.

    void bubble_sort(float *distances, int num_of_els)
    {
    int num_swaps=0;
    int i;
    float temp;
    do
    {


    for(i=0; i<num_of_els-1; i++)
    {
    if(distances<distances[i+1])
    {
    temp=distances;
    distances=distances[i+1];
    distances[i+1]=temp;
    }
    num_swaps++;

    }
    }
    while(num_swaps!=0);


    {
    for(i=0; i<num_of_els; i++)
    printf("%f\n", distances);
    }

    }


    I think its just a syntax issue but I cant spot it

    Appreciate any help you guys can offer.
    Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 4,277 ✭✭✭km991148


    Gaz wrote: »
    Hi guys,

    Hoping someone can look at my code and tell me what im doing wrong, program compiles but dosnt work. Basically ive written a function to bubble sort an array of floats.

    void bubble_sort(float *distances, int num_of_els)
    {
    int num_swaps=0;
    int i;
    float temp;
    do
    {


    for(i=0; i<num_of_els-1; i++)
    {
    if(distances<distances[i+1])
    {
    temp=distances;
    distances=distances[i+1];
    distances[i+1]=temp;
    }
    num_swaps++;

    }
    }
    while(num_swaps!=0);


    {
    for(i=0; i<num_of_els; i++)
    printf("%f\n", distances);
    }

    }


    I think its just a syntax issue but I cant spot it

    Appreciate any help you guys can offer.
    Thanks

    Havent really looked at the algorithm (and been a good while sinceI even thought about a bubble sort) but did you mean to do:

    do{
    //
    //

    }
    while(num_swaps != 0)

    as num_swaps is always gonna be zero?

    What ide are you using, are you able to debug/ step through code?


    EDIT: sorry missed the num_swaps++
    BUT - I see your issue now - you will still be stuck in a loop...!


  • Registered Users, Registered Users 2 Posts: 2,426 ✭✭✭ressem


    You need to reset num_swaps to zero each time the do-while loop restarts.


  • Registered Users, Registered Users 2 Posts: 4,277 ✭✭✭km991148


    Also - why this part?

    {
    for(i=0; i<num_of_els; i++)
    printf("%f\n", distances);
    }

    The brackets don't add value here - did you intend to place an if before?


  • Registered Users, Registered Users 2 Posts: 1,595 ✭✭✭Gaz


    Thats to print the sorted elements once the bubble sort is finished.
    Dosnt feckin work though.


  • Registered Users, Registered Users 2 Posts: 4,277 ✭✭✭km991148


    Gaz wrote: »
    Thats to print the sorted elements once the bubble sort is finished.
    Dosnt feckin work though.

    ok 2 points - did you try and fix the loop as ressem and I suggested?

    and 2. - I realise its to print the results, but you have additional braces ({}) around the for which are not needed - why did you add them?

    Also - what environment are you using? Visual Studio, eclipse etc? you should be able to step through code/ debug this quite easily.

    Why is it 'not working' The code you posted will never reach the print statement as the code will never leave the loop. This can be easily seen in a decent ide. If you are doing this via notepad etc, then you could easily have moved the print statement to before the sort and you would see this.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    Wow. So much wrong :)

    This:
    for(i=0; i<num_of_els-1; i++)

    should be:
    for(i=0; i<num_of_els-2; i++)

    for starters. Otherwise you're baling off the end of the array.

    Plus, as km said, you need to reset num_swaps (at the start of the do while block). Plus the num_swaps++ needs to be inside the if(distances<distances[i+1]) block.


  • Registered Users, Registered Users 2 Posts: 4,277 ✭✭✭km991148


    carveone wrote: »
    Wow. So much wrong :)

    yep - I was going on the basis of one at a time while trying to encourage use of the available tools ;)

    Let us know how you go,
    km


  • Registered Users, Registered Users 2 Posts: 1,595 ✭✭✭Gaz


    Solved !

    I took a different approach and used a nested for loop >>

    void bubble_sort(float *distances, int num__of_els)
    {
    int i;
    int j;
    float temp;


    for(i=0; i<num_of_els; i++)
    for(j=num_of_els-1; j>=i; j--)
    {
    if(distances[j-1]>distances[j])
    {
    temp=distances[j-1];
    distances[j-1]=distances[j];
    distances[j]=temp;
    }
    }

    Then a printf printed them in order.

    this made much more sense in my head.

    thanks guys :)


  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    Glad you sorted it but
    I think its just a syntax issue but I cant spot it

    as it compiles it is not really a syntax issues but a logic issue


  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    Interesting move straight to the optimised case for bubble sort :p

    For stuff like this you are almost always better writing pseudo code on a piece of paper or something and then translating that to code. Otherwise you tend to flail and type things you hope will work but probably won't.

    Of course bubble sort isn't exactly a good algorithm. It's pretty appalling in fact!


  • Advertisement
Advertisement