Program for Bubble Sort in C++

In this article, you will get a program for bubble sort in C++.

bubble sort is an arranging strategy wherein each pair of contiguous components are looked at, in the event that they are in the wrong request we swap them. This calculation is named as air pocket sort since, same as like air pockets the littler or lighter components come up (at the start) and greater or heavier components goes down (at the end). Beneath I have shared a program for bubble sort in C++ which sorts a rundown of numbers in the climbing request.

Program for Bubble Sort in C++

#include<iostream>
 
using namespace std;
 
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;
}
Program for Bubble Sort in C++

Leave a Comment

error: Alert: Content is protected!!