Modified bubble sort

This is a discussion on Modified bubble sort within the C++ Programming forums, part of the General Programming Boards category; ok, so i have to do this "after the first pass, the largest number is guaranteed to be in the ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    2

    Modified bubble sort

    ok, so i have to do this

    "after the first pass, the largest number is guaranteed to be in the highest-numbered element of the array; after the second pass, the two highest numbers are "in place," and son. Instead of making nine comparisons on every pass, modify the bubble sort to make eight comparisons on the second pass, seven on the third pass, and so on"

    here is the program...just looking to modify my bubblesort function.

    insert
    Code:
    #include <iostream>
    using::cout;
    using::endl;
    
    #include <iomanip>
    using::setw;
    
    int bubblesort(int[], int);
    void display(int[], int);
    
    int main()
    {
       const int arraySize = 10; // size of array a
       int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
    
    	cout << "Data items in original order\n";
    
    	display (a, arraySize);
    
       bubblesort(a, arraySize);
    
       cout << "\nData items in ascending order\n";
    
       display (a, arraySize);
    
       cout << endl;
       return 0; // indicates successful termination
    } // end main
    
    void display(int a[], int arraySize)
    {
    	for ( int k = 0; k < arraySize; k++ )
          cout << setw( 4 ) << a[ k ];
    }
    
    int bubblesort(int a[], int arraySize)
    {
    int hold; // temporary location used to swap array elements
    	for ( int pass = 0; pass < arraySize - 1; pass++ )
       {
          // loop to control number of comparisons per pass
          for ( int j = 0; j < arraySize - 1; j++ )
          {
             // compare side-by-side elements and swap them if
             // first element is greater than second element
             if( a[j] > a[j+1] )
             {
             hold = a[j];
             a[j] = a[j+1];
             a[j+1] = hold;
             }
          }
       }
    }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    so what is your question?...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Good to see that you are posting code with code tags and have made some attempt to indent your code, but please try to keep your indentation consistent

    Anyway, this is the crux of the matter:
    Code:
    int bubblesort(int a[], int arraySize)
    {
        int hold; // temporary location used to swap array elements
        for ( int pass = 0; pass < arraySize - 1; pass++ )
        {
            // loop to control number of comparisons per pass
            for ( int j = 0; j < arraySize - 1; j++ )
            {
                // compare side-by-side elements and swap them if
                // first element is greater than second element
                if( a[j] > a[j+1] )
                {
                    hold = a[j];
                    a[j] = a[j+1];
                    a[j+1] = hold;
                }
            }
        }
    }
    Clearly, if you want to reduce the number of comparisons, you need to modify the number of times the inner loop is run. Currently, the inner loop always runs (arraySize - 1) - 0 + 1 = arraySize times. You need to reduce this by one after each iteration of the outer loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    2
    so how would i change the bubblesort function....i really don't understand arrays

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    See this post. If you understand that algorithm, it will get you to the answer you need. detecting numbers in a code of line

    Todd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 04:25 PM
  2. help with debug (bubble sort algorithm)
    By lbraglia in forum C Programming
    Replies: 5
    Last Post: 10-19-2007, 06:24 PM
  3. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 05:48 AM
  4. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM
  5. Help with Bi-Directional Bubble Sort in C
    By cunninglinguist in forum C Programming
    Replies: 0
    Last Post: 04-19-2002, 03:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21