Bubble Sort


Program Listing

BubbleSort.cpp

/************************************************************************
*                                                                       *
*   Program:    BubbleSort                                              *
*                                                                       *
*   Purpose:    To sort a list of numbers into ascending order          *
*                                                                       *
************************************************************************/

//  Header files

#include <iostream>
#include <string>

using namespace std;

//  Function prototypes

void BubbleSort(int list[], int size);

/************************************************************************
*                                                                       *
*   Function:   main                                                    *
*                                                                       *
*   Purpose:    To read a list of numbers and print them in ascending   *
*               order                                                   *
*                                                                       *
************************************************************************/

int main() {

//  Read a list of numbers

    const int maxSize = 100;
    int list[maxSize];
    
    cout << "Enter the size of the list (max of " << maxSize << "):  " << flush;
    int size;
    cin >> size;
    
    cout << "Enter a list of " << size << " integers:" << endl;
    
    for (int i = 0; i < size; i++)
        cin >> list[i];
    
//  Sort the list

    BubbleSort(list, size);
    
//  Print the sorted list

    cout << endl << "The sorted list:" << endl;
    for (int i = 0; i < size; i++)
        cout << list[i] << endl;
        
    return 0;
}

/************************************************************************
*                                                                       *
*   Function:   BubbleSort                                              *
*                                                                       *
*   Purpose:    To sort a list of numbers using the bubble sort         *
*               algorithm                                               *
*                                                                       *
************************************************************************/

void BubbleSort(int list[], int size) {

//  Do n - 1 passes

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

    //  Go down the list comparing adjacent elements
    
        for (int j = 0; j < size - i - 1; j++)
        
        //  If adjacent elements are out of order, then swap them
        
            if (list[j] > list[j + 1]) {
                int temp = list[j];
                list[j] = list[j + 1];
                list[j + 1] = temp;
            }
    return;
}

Sample Run

Enter the size of the list (max of 100):  10
Enter a list of 10 integers:
45 30 60 70 25 65 50 35 90 75

The sorted list:
25
30
35
45
50
60
65
70
75
90



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:57 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/BubbleSort.html

Return to Coms 261 home page