Thursday 9 May 2013

Sorting a given list of numbers in ascending order.


Sorting a given list of numbers in ascending order.

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int x[20],n;

cout<<"How many Numbers you want to enter:";
cin>>n;

cout<<"\nEnter Values:";
for(int i=0;i<n;i++)
cin>>x[i];

int temp;
for(int i=0;i<n-1;i++) // for controling passes
{
for(int j=0;j<n-1;j++) // for making comparisons
{
if(x[j]>x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}
}
cout<<"\nSorted Elements in ascending order:\n";

for(int k=0;k<n;k++) // for printing numbers
cout<<x[k]<<"\t";

getch();
}


No comments:

Post a Comment