C++ Program for Bubble Sort

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

But before starting the program, if you're not aware of it, you can follow bubble sort to understand its logic with an example.

Bubble Sort in C++

To sort an array in ascending order using bubble sort in C++ programming, you have to ask the user to enter the array size and its elements. Now, use the bubble sort method to sort the array elements and show the sorted array on the screen, as shown in the next 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 built and runs under the Code::Blocks IDE. Here is the initial snapshot of the sample run:

C++ program bubble sort

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

bubble sort in c++

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

After each sort, print the array

Let's make another program that will dispel almost all of your concerns about bubble sort. This program prints the array after each sort. Take a deep look at 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 them 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 10 elements, press ENTER to get the output as given in the final snapshot here:

c++ implement bubble sort

Note: The last array, or the array at the last step (step no. 9), is a sorted array.

Bubble Sort in C++ using a Function

This is the last program on bubble sort. This program is created using the function bubbleSort(). The function receives the array as its argument and sorts it in ascending order.

#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

The same program in different languages

C++ Quiz


« Previous Program Next Program »