C++ Program for Bubble Sort

In this article, you will learn and get code on implementing bubble sort technique in a C++ program. That is, you'll get the code on how to sort an array in ascending order using bubble sort technique. Bubble sort program is created with following approaches:

  • Simple Bubble Sort Program
  • Prints Array after each Sort using Bubble Sort Algorithm
  • Bubble Sort Program using user-defined Function

But before starting the program, if you're not aware about it, you can follow bubble sort to understand about its logic with example. That how a bubble sort technique actually works into the program.

Bubble Sort in C++

To sort an array in ascending order using bubble sort in C++ programming, you have to ask from user to enter the array size and its elements. Now start sorting the array elements using the bubble sort technique and display the sorted array on the screen as shown here in the following program.

#include<iostream>
using namespace std;
int main()
{
    int n, i, arr[50], j, temp;
    cout<<"Enter the Size (max. 50): ";
    cin>>n;
    cout<<"Enter "<<n<<" Numbers: ";
    for(i=0; i<n; i++)
        cin>>arr[i];
    cout<<"\nSorting the Array using Bubble Sort Technique..\n";
    for(i=0; i<(n-1); i++)
    {
        for(j=0; j<(n-i-1); j++)
        {
            if(arr[j]>arr[j+1])
            {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
    cout<<"\nArray Sorted Successfully!\n";
    cout<<"\nThe New Array is: \n";
    for(i=0; i<n; i++)
        cout<<arr[i]<<" ";
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is the initial snapshot of sample run:

C++ program bubble sort

Now enter the size say 5 and then its elements as 5, 1, 4, 2, 3. Press ENTER key to the list gets sorted in ascending order as shown in the final snapshot of sample run given below:

bubble sort in c++

If user enters the size of array as 5, and its elements as 5, 1, 4, 2, 3, then dry run of above program goes like:

  • Because size is 5. Therefore, n=5
  • And all the 5 elements gets initialized to the array arr[] in a way that arr[0]=5, arr[1]=1, arr[2]=4, arr[3]=2, and arr[4]=3
  • Now using for loop, initially i=0 and checks the condition, whether it is less than n-1 or not
  • Because the condition (i<(n-1) or 0<(5-1) or 0<4) evaluates to be true, therefore program flow goes inside the loop
  • Inside the loop, there is another for loop, and there j=0 (initially) and the condition j<(n-i-1) or j<(5-0-1) or j<4 evaluates to be true. Therefore program flow goes inside this loop
  • And checks the condition of if block, that is arr[j]>arr[j+1] or arr[0]>arr[1] or 5>1 evaluates to be true, therefore program flow goes inside the if block and executes all the three statements
  • After executing all the three statements, the element at index number j gets swapped with element at index number j+1
  • Now arr[0]=1 and arr[1]=5 and rest of the three elements will be at same index.
  • Now program flow goes to the updatation part of the inner for loop and increments the value of j. Now j=1
  • Again checks the condition, that is j<(n-i-1) or 1<(5-0-1) or 1<4 evaluates to be true, therefore program flow again goes inside the loop and checks the condition of if block
  • If the condition evaluates to be true, then process the three statements, otherwise update the value of j and checks the condition
  • Continue the process in similar with updated value, until the condition of outer for loop evaluates to be false.
  • If the condition of outer for loop evaluates to be false, then program flow exits the loop
  • Now the array arr[] gets sorted, just print it

Print Array after Each Sort

Let's create another program that clears almost all of your doubt about bubble sort. This program prints the array after each sort. Take a deep look on all the arrays after each sort.

#include<iostream>
using namespace std;
int main()
{
    int i, arr[10], j, temp;
    cout<<"Enter 10 Elements: ";
    for(i=0; i<10; i++)
        cin>>arr[i];
    cout<<endl;
    for(i=0; i<9; i++)
    {
        for(j=0; j<(10-i-1); j++)
        {
            if(arr[j]>arr[j+1])
            {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
        cout<<"Step "<<i+1<<": ";
        for(j=0; j<10; j++)
            cout<<arr[j]<<" ";
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

Here is the initial snapshot of the sample run:

bubble sort program c++

Now enter any 10 elements in random order to sort it in ascending order. Here I've entered 10 elements in descending order say 10, 9, 8, 7, 6, 5, 4, 3, 2, 1. After supplying all the 10 elements, press ENTER to the output as given in the final snapshot here:

c++ implement bubble sort

Note - The last array or the array at last step (step no.9) is sorted array.

Bubble Sort in C++ using Function

This is the last program on bubble sort. This program is created using a function bubbleSort(). The function receives the array as its argument and sorts it in ascending order. To learn about function in detail, then you can refer to the separate tutorial on it.

#include<iostream>
using namespace std;
void bubbleSort(int []);
int main()
{
    int i, arr[10];
    cout<<"Enter 10 Elements: ";
    for(i=0; i<10; i++)
        cin>>arr[i];
    bubbleSort(arr);
    cout<<"\nThe New Sorted Array is: \n";
    for(i=0; i<10; i++)
        cout<<arr[i]<<" ";
    cout<<endl;
    return 0;
}
void bubbleSort(int arr[])
{
    int i, j, temp;
    for(i=0; i<9; i++)
    {
        for(j=0; j<(10-i-1); j++)
        {
            if(arr[j]>arr[j+1])
            {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}

Here is its sample run:

bubble sort c++ using function

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »