C++ Examples
C++ ExamplesPrograms with output

C++ Program for Bubble Sort


Example:
#include<iostream>
using namespace std;
int main()
{
    int n, i, arr[50], j, temp;
    cout << "Enter Size of List: ";
    cin >> n;
    
    cout << "\nEnter " << n << " Numbers: ";
    for(i=0; i<n; i++)
        cin>>arr[i];
        
    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 << "\nSorted List: \n";
    for(i=0; i<n; i++)
        cout << arr[i] << "\n";
    
    return 0;
}
Output:
Enter Size of List:
Enter 5 Numbers:
5
50
40
30
10
20
Sorted List:
10
20
30
40
50

Share this page on:

Was this page helpful ?

Let us know how we did!

Subscribe Email Updates

to get latest update