Bubble sort in Single dimensional array

Bubble sort method is one of the must know the sorting algorithm for each and every computer science student.

In this sorting program, we are sorting an integer array of size 10 using bubble sort method.  This method is sorting this array into ascending order.

Bubble sorting on a single dimensional array using functions.

/* Bubble sort program using function and array
*/
#include<iostream>
#include<stdlib.h>
#include<iomanip>
#include “io.cpp”
using namespace std;

void output(int a[],int n){
for(int i=0;i<n;i++)
cout<<setw(10)<<a[i];
}

void input(int x[],int n){
for(int i=0;i<n;i++)
{
cout<<“Enter x[“<<i+1<<“] number : “;
cin>>x[i];
}
}

void bubble(int a[],int n){
int i,j,temp;
for(i=0;i<n-1;i++) // steps
{
for(j=0;j<n-1-i;j++)
if(a[j]>a[j+1])
{
temp = a[j];
a[j]= a[j+1];
a[j+1]=temp;
}
}
}

int main(){
int a[10];
//input phase
input(a,10);
//processing phase
bubble(a,10);
// output phase
cout<<“n Sorted array :”;
output(a,10);
return 0;
}

Other useful sorting algorithm are

Selection sort method program using single dimensional array

Insertion sort method program using single dimensional array

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.