Search code examples
c++bubble-sort

Bubble sort doesnt sort last number with this algorithm


I am having issues the last number in this code is not being sorted.

   // This is the more advanced optimzed version of bubble sort
        int modifiedBubbleSortArray(int array[])
        {

        int swapped = 0;

         do {
            swapped = false;
            // We specify a loop here for the sorting
            for(int i=0;i<NUM_ARRAYS;i++)
            {
                // We specify aother loop here for the sorting
                for(int j=0;j< i - 1 /* <<< here we make less comparisns on each pass each time */ ;j++)
                {
                    // If the array i is better than j we enter this swap
                    if (array[j]<array[j+1])  
                    {
                        // We swap here for the functions
                        swap(array[j], array[j+1]);
                        // We measure the amount of times we swap
                        amountOfSwaps += 1;
                        // If we swapped we break out of the loop
                        swapped = true;
                    }
                }
            }
             } while (swapped);
        printArray(array);
        return amountOfSwaps;

        }

        // Here we swap values to sort them
        void swap(int & value1, int & value2)
        {
            // We specify a temp and use it to swap
            int temp=value1; 
            value1=value2;
            value2=temp;

        }

Solution

  • The inner loop should be:

    if (array[j]>array[j+1]) 
    { 
      // We swap here for the functions 
      swap(array[j], array[j+1]); 
      // We measure the amount of times we swap 
      amountOfSwaps += 1; 
    } 
    

    Give that a shot and see if your variant on bubble sort sorts correctly.

    To sort in descending order, just change the if condition:

    if (array[j]<array[j+1]) 
    

    One more bug is the inner for loop. Change that to:

    for(int j=0;j<=i-1;++j)
    

    Update (based on last change with swap test added):

    bool swapped = false; 
    
    // We specify a loop here for the sorting 
    for(int i=0;i<NUM_ARRAYS;i++) 
    { 
        // We specify aother loop here for the sorting 
        for(int j=0;j<=i-1;++j)
        { 
            // If the array i is better than j we enter this swap 
            if (array[j]<array[j+1])   
            { 
                // We swap here for the functions 
                swap(array[j], array[j+1]); 
                // We measure the amount of times we swap 
                amountOfSwaps += 1; 
                // If we swapped we break out of the loop 
                swapped = true; 
            } 
        } 
        // If no swaps this iteration, break out
        if (!swapped)
            break;
    } 
    

    If you really want a good sort, look into introspection sort - a variant on quick sort. The algorithm is more complicated, but the sort is also more efficient, being O(N log N) instead of O(N^2) which is the complexity of bubble sort.