Thread: Modified bubble sort

  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,229
    so what is your question?...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Chappell Hill, Texas
    Posts
    2,332
    See this post. If you understand that algorithm, it will get you to the answer you need. http://cboard.cprogramming.com/showp...03&postcount=4

    Todd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. help with debug (bubble sort algorithm)
    By lbraglia in forum C Programming
    Replies: 5
    Last Post: 10-19-2007, 05:24 PM
  3. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04: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, 02:32 PM