write a program in c++ to sort an integer array by using bubble sort?


#include <iostream.h>
#include<conio.h>
void bubble(int a[],int n)
{
  for(int i=0;i<n-1;i++)
       for(int j = n-1 ; i<j;j--)
                   if(a[j] < a[j-1])
 {
                       swap(a[j] < a[j-1]);
 }
}
void swap(int(int &a,int &b)
{
int temp = a;
a = b;
b = temp;
}
void main()
{
clrscr();
int x[5] = {10,50,30,40,20};
bubble(x,5);
cout<<"Sorted x-array:";
for(int i =0;i<5;i++)
cout<<endl;
getch();
}

Output :
Sorted x-array : 10 20 30 40 50

Copy and run it on - https://www.codechef.com/ide



0 Comments