The bubble sort algorithm - C++ source code - Learning C++



++++

Precondition:  the function accepts an unsorted array and integer size that is the size of the array:

Post condition: Sorted array

void BubbleSort ( int size, int array[])
{
    int current = 0;
    bool sorted = false;

            while (( current < size)&&(sorted = = false))
            {
                int walker = size-1;
                sorted = true;

                        while (walker > current)
                        {
                              if ( array[walker] < array[walker - 1] )
                                {
                                    sorted = false;
                                    exchange( array, walker, walker-1);
                                }
                      walker--;
                     }
                current++;
            }
}

void exchange (int array[], int a, int b)
{
           int temp=array[a];
           array[a]=array[b];
           array[b]=temp;
}

Demo Applet

Did you find information useful? Send to your friend a link to this page

Please rate the tutorial

1 2 3 4 5 6 7 8 9 10



Comments
Register to add comments ( 5000 chars ) for bubsortcpp.php.
Are you human? Please select two the same numbers