Program to sort out an array using Bubble Sort
Friday, March 06, 2020//program to sort out an array using bubble sort
#include<iostream.h>
#include<conio.h>
void bubble_sort(int A[20],int N)
{ int i,j,p,temp;
for(i=0;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<<"\n Array after iteration "<<i+1<<" is: ";
for(p=0;p<N;p++)
cout<<A[p]<<"\t";
}
}
void main()
{ clrscr();
int a[20],n,i;
cout<<"\n Enter the number of elements:";
cin>>n;
cout<<"\n Enter the elements of array";
for(i=0;i<n;i++)
{ cout<<"\n Enter a["<<i<<"]:";
cin>>a[i];
}
cout<<"\n Array is:";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
bubble_sort(a,n);
cout<<"\n Sorted array is:";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
getch();
}
#include<iostream.h>
#include<conio.h>
void bubble_sort(int A[20],int N)
{ int i,j,p,temp;
for(i=0;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<<"\n Array after iteration "<<i+1<<" is: ";
for(p=0;p<N;p++)
cout<<A[p]<<"\t";
}
}
void main()
{ clrscr();
int a[20],n,i;
cout<<"\n Enter the number of elements:";
cin>>n;
cout<<"\n Enter the elements of array";
for(i=0;i<n;i++)
{ cout<<"\n Enter a["<<i<<"]:";
cin>>a[i];
}
cout<<"\n Array is:";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
bubble_sort(a,n);
cout<<"\n Sorted array is:";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
getch();
}
0 Comments