codescracker


c++

C++ Program Bubble Sort



« Previous Program Next Program »


Bubble Sort in C++

To sort an array in ascending order using bubble sort in C++ programming, you have to ask to the user to enter the array size then ask to enter array 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++ Programming Code for Bubble Sort

Following C++ program sort the array using bubble sort technique:

/* C++ Program - Bubble Sort */
		
#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int n, i, arr[50], j, temp;
	cout<<"Enter total number of elements :";
	cin>>n;
	cout<<"Enter "<<n<<" numbers :";
	for(i=0; i<n; i++)
	{
		cin>>arr[i];
	}
	cout<<"Sorting 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<<"Elements sorted successfully..!!\n";
	cout<<"Sorted list in ascending order :\n";
	for(i=0; i<n; i++)
	{
		cout<<arr[i]<<" ";
	}
	getch();
}

When the above C++ program is compile and executed, it will produce the following result:

C++ program bubble sort

Same Program in Other Language

You may like the same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test