write a program to sort an integer of size 5 in ascending order using a burble sort algorithm
using namespace std;
// write a program to sort an integer of size 5 in ascending order using a burble sort algorithm
void swap(int *p,int *q)
{
unsigned int t;
t=*p;
*p=*q;
*q=t;
}
void sort(int a[],int n)
{
int i,j;
int temp;
for(i = 0;i < n-1;i++)
{
for(j = 0;j < n-i-1;j++)
{
if(a[j] > a[j+1]) swap(&a[j],&a[j+1]);
}
}
cout<<"\nThe sorted numbers are: ";
for(i = 0;i < n;i++) cout<<a[i]<<", ";
// for(i = n-1;i >=0;i--) cout<<a[i]<<", ";
}
main(void)
{
int Num[5];
int n=0,a;
cout<<"\nEnter five integers separated by space: ";
for(n=0;n<5;n++) cin>>Num[n];
sort(&Num[0],5);
return(0);
}
Comments
Leave a comment