codescracker
c++

C++ Program for Bubble Sort



« Previous Program Next Program »

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:

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.

// C++ Program to Implement Bubble Sort
// ----codescracker.com----

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

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.

// Print Array after Each Sort
// ----codescracker.com----

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

// Bubble Sort in C++ using Function
// ----codescracker.com----

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



© Copyright 2021. All Rights Reserved.

CodesCracker