Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Putting numbers in an ascending order (http://www.go4expert.com/forums/putting-ascending-t1960/)

 nathaniel 22Nov2006 00:16

Putting numbers in an ascending order

Hey ppl

I need some help

I want to know what is the correct source code for sorting numbers

say I entered num1,num2,num3,num4 and num5 what would the correct source code be to sort them in ascending order

help plz

Re: Putting numbers in an ascending order

Well there are lots of sorting algorithms, the easiest being Bubble Sort, which is easy to implement in any programming language.
In case, you have some trouble, G4E help is just a post away.

 Aztec 23Nov2006 08:36

Re: Putting numbers in an ascending order

Quote:
 Originally Posted by pradeep Well there are lots of sorting algorithms, the easiest being Bubble Sort, which is easy to implement in any programming language.
....the easiest being Bubble Sort( but worse time complexity), which is easy to implement in any programming language.

Re: Putting numbers in an ascending order

That's right! But, for someone who is new to sorting algorithms Bubble Sort is the easiest to understand and implement.

 friendsforniraj 24Nov2006 18:44

Re: Putting numbers in an ascending order

Quote:
 Originally Posted by pradeep That's right! But, for someone who is new to sorting algorithms Bubble Sort is the easiest to understand and implement.

well i think selection sort isbest for beginners in dat u dont have to bother to check which is smallest
i m giving code for it
if the nos are stored in an array' a'
a[i]=num[k]
i varies from 0 to 4 nd k varies from 1 to 5
Code:

#include<stdio.h>
#include<conio.h>
main()
{
int a[5],t,min,i,j;  \\considering all no. entered to be integer type
for( i=0;i<5;i++)
scanf("%d",&a[i]);
\\ now the sorting part

for(i=0;i<5;i++)
for(j=i+1;j<5;j++)
if(a[i]>a[j]){t=a[i];
a[i]=a[j];
a[j]=t;
}
\\ now to print the sorted array
for(i=0;i<5;i++)
printf("\n %d"a[i]);

getch();
}

\\ yah dats it

 friendsforniraj 27Nov2006 18:48

Re: Putting numbers in an ascending order

if neone has better idea please letme no

 All times are GMT +5.5. The time now is 08:47.