/** * bubblesort.cpp * 30apr07/sklar * * this program demonstrates the bubble sort algorithm. * */ #include #include #include using namespace std; // declare constants const int NUM_DICE = 5; // declare global variable int dice[NUM_DICE]; // declare function prototypes void initDice(); void printDice( int d[], int n ); void swapDice( int a, int b ); void bubbleSort(); /** * initDice() * * this function initializes the values in the array of dice to * integers between 1 and 6 * */ void initDice() { for ( int i=0; i dice[i+1] ) { swapDice( i, i+1 ); } } // end for i num_passes++; cout << "after pass#" << num_passes << ": "; printDice( dice, NUM_DICE ); } // end for pass cout << "TOTAL number of passes = " << num_passes << endl; } // end of bubbleSort() /** * main() * */ int main() { // initialize random number seed srand( time( NULL )); // initialize the array of dice initDice(); // print the array before sorting it cout << "before sorting:"; printDice( dice, NUM_DICE ); // perform bubble sort bubbleSort(); // print the array after sorting it cout << "after sorting:"; printDice( dice, NUM_DICE ); } // end of main()