This article describes bubble sort program in c/c++. Let us discuss about Bubble sort.
What is Bubble Sort?
Bubble sort is the simplest sorting algorithm. Starting from the beginning of the list it’s compared every adjacent pair and swap their position if they are not in the right order (The latter one is smaller than the former one). This process is repeated as many times as necessary until the array is sorted. It takes O(n) time.
Bubble Sort Algorithm
The basic code for bubble sort looks like this, for sorting an integer array:
for(int x=0; x<n; x++)
for(int y=0; y<n-1; y++)
int temp = array[y+1];
array[y+1] = array[y];
array[y] = temp;
Program for Bubble Sort
cout<<" \" This program will arrange some data in Ascending order (using bouble sort). \""<<endl<<endl;
cout<<"How many numbers ?. Give a integer number."<<endl;
cout<<"More than 20 is not allow .Please give a number less than 20."<<endl;
cout<<"Negative numver not allow, Please give a integer number."<<endl;
cout<<"0 is not valid,Please give more."<<endl;
cout<<"What are the elements ?. Give some data."<<endl;
cout<<"You have given the following numbers :"<<endl;
cout<<"In Ascending order they are : "<<endl;