## 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

```Integer Arrays Bubble Sort Setw ```

``` Bubble Sort - Int Array C++ // ============================================================================ // 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/ 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 // ============================================================================//    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 intconst int NUM_INTS = 12; // function prototypevoid 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 95```

``` Array after bubble sort pass #2 0 1 2 3 4 6 10 4 15 17 35 95 Array after bubble sort pass #3 0 1 2 3 4 6 4 10 15 17 35 95 ```

```Array 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 ```