/* Bubble Sort*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5],temp,n = 4,k;
cout << "enter an array number and it will be sorted"<<endl;
for (int i = 0; i<5; i++)
{
cin >> a[i];
}
//sorting begins here
for (int j = 0; j<=n;j++)
{
if (a[j]>= a[j+1])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
k = a[j];
}
else
{
k = a[j+1];
}
if (a[n] == k)
{
n = n - 1;
}
}
for (i = 0; i<5; i++)
{
cout << a[i]<<" ";
}
cout << endl;
getch();
}
Okay the problem I am getting here is that the program does not give the right output for eg if u enter
3,4,1,5,2
you will get 2,3,1,4,0

New Topic/Question
Reply



MultiQuote







|