C++ Program to sort array using bubble sorting

In this article you will get how to perform the bubble sorting using c++.

Bubble sorting is technique in which we compare repeatedly each adjacent elements. if they arranged in wrong order, then we swap them and arrange in an order. the first and second elements are compared and swapped if out of order.

then the second and third elements are compared and swapped if out of order. this process until the last two elements are compared and swapped if out of order


ALGORITHM:-

Step 1: Enter the length of array
Step 2: Insert the elements in array
Step 3: compare the first and second element
Step 4: if they out of order then arrange its in order
Step 5: Continues the 3 and 4 step until last two elements are swapped
Step 6: Print the sorted list
Step 7: Stop the Execution

Program to sort the array using bubble sorting algorithm


PROGRAM CODE:-

#include<iostream.h>
#include<conio.h> 
 
int main()
{
    int a[50],n,i,j,temp;
    cout<<"Enter the size of array: ";
    cin>>n;
    cout<<"Enter the array elements: ";     
    for(i = 0; i < n; i++)
    {
        cin>>a[i];
    }    
    for(i = 1; i < n; i++)
    {
        for(j = 0;j < (n-i); ++j)
        {
            if(a[j] > a[j+1])
            {
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
         }
    }    
    cout<<"Array after bubble sort:";
    for(i = 0; i < n; i++)
    {
        cout<<" "<< a[i];
    }    
    return 0;
}


OUTPUT:-

Enter the size of array: 6
Enter the array element: 4 6 3 1 9 0

Array after bubble sort: 0 1 3 4 6 9
Bubble sorting algorithm in c++
Perform the bubble sorting in c++
Perform the bubble sorting in c
How to arrange the element using bubble sorting in c++
Bubble sorting algorithm using c++