C++ || Snippet – Sort An Integer Array Using Bubble Sort – Print Each Pass & Total Number Of Swaps
This is a program which has no functionality, but displays the sorting of an integer array through the use of the bubble sort algorithm.
This program sorts the values of a one-dimensional array in ascending order using bubble sort. It also prints the total number of passes thru each iteration
REQUIRED KNOWLEDGE FOR THIS SNIPPET
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
// ============================================================================ // Author: Kenneth Perkins // Date: Feb 7, 2012 // Taken From: http://programmingnotes.org/ // File: bubbleSort.cpp // Description: This program sorts the values of a one-dimensional array // in ascending order using the Bubble Sort Algorithm. It also prints // the total number of passes thru each iteration // ============================================================================ #include <iostream> #include <iomanip> using namespace std; // const int const int NUM_INTS = 12; // function prototype void BubbleSort(int arry[], int &totalSwaps, int &totalPasses); int main() { // Declarations int arry[NUM_INTS] = {0, 1, 3, 95, 2, 4, 6, 10, 15, 4, 17, 35}; int totalSwaps = 0; int totalPasses = 0; cout << "Original array values" << endl; // Output the original array values for (int i = 0; i < NUM_INTS; ++i) { cout << setw(4) << arry[i]; } // creates a line separator cout << "\n---------------------------------------------------------------\n\n"; BubbleSort(arry,totalSwaps,totalPasses); // creates a line separator cout << "---------------------------------------------------------------\n"; cout << "The current sorted array" << endl; // Output the current sorted for (int i = 0; i < NUM_INTS; ++i) { cout << setw(4) << arry[i]; } // creates a line separator cout << "\n---------------------------------------------------------------\n"; // Display some statistics to the user cout << "Total Swaps: " << totalSwaps << endl; cout << "Total Passes: " << totalPasses << "\n\n"; return 0; }// end of main void BubbleSort(int arry[], int &totalSwaps, int &totalPasses) { int currentSwaps = 0; // Loop to determine amount of passes for (int iteration = 1; iteration < NUM_INTS; ++iteration) { // Reset variable swaps to zero currentSwaps = 0; // Bubble Sort Algorithm // Sort numbers in ascending order for (int p = 0; p < NUM_INTS - iteration; ++p) { // if the previous value is bigger than the next // then swap places if (arry[p]> arry[p+1]) { int temp = arry[p]; arry[p] = arry[p+1]; arry[p+1]= temp; ++currentSwaps; } } // If no swaps were made in the last pass, // no need to continue the loop. Sorting complete. if (currentSwaps == 0) { break; } cout << "Array after bubble sort pass #" << iteration << endl; // Display values after each pass for (int x = 0; x < NUM_INTS; ++x) { cout << setw(4) << arry[x]; } cout << "\n\n"; // Keeps track of the amount of swaps and passes totalSwaps += currentSwaps; ++totalPasses; }// end of for loop }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output
Original array values
0 1 3 95 2 4 6 10 15 4 17 35
---------------------------------------------------------------
Array after bubble sort pass #1
0 1 3 2 4 6 10 15 4 17 35 95Array after bubble sort pass #2
0 1 2 3 4 6 10 4 15 17 35 95Array after bubble sort pass #3
0 1 2 3 4 6 4 10 15 17 35 95Array after bubble sort pass #4
0 1 2 3 4 4 6 10 15 17 35 95
---------------------------------------------------------------
The current sorted array
0 1 2 3 4 4 6 10 15 17 35 95
---------------------------------------------------------------
Total Swaps: 12
Total Passes: 4
Leave a Reply