OBJECT:- To Sort The Array of Given Numbers

Learn and Trouble Shoot Problems Related to C-Language here.

OBJECT:- To Sort The Array of Given Numbers

Unread postby rajeevmahura » Thu May 22, 2008 6:24 pm

/* Program to Sort The Given Array */
#include<stdio.h>
#include<conio.h>
void main()
{
int num[50],cho,i,j;
clrscr();
printf("How Many Numbers Do You Want to Sort:- ");
scanf("%d",&cho);

for(i=1;i<=cho;i++)
{
printf("Enter Any Number for num[%d]",i);
scanf("%d",&num[i]);
}

for(i=1;i<=cho;i++)
{
for(j=i;j<=cho;j++)
{
if(num[i]>num[j])
{
num[i]=num[i]+num[j];
num[j]=num[i]-num[j];
num[i]=num[i]-num[j];
}
}
}

printf("After Sorting ");

for(i=1;i<=cho;i++)
{
printf("\n%d",num[i]);
}
getch();
}


----------------------------------------------------------
Input:- How Many Numbers Do You Want to Sort:- 5
Enter Any Number for num[1] 6
Enter Any Number for num[2] 8
Enter Any Number for num[3] 5
Enter Any Number for num[4] 3
Enter Any Number for num[5] 1

Output:- After Sorting
1
3
5
6
8
--Regards--
Rajeev Mahura
Servers & Storage Professional
http://about.me/rajeevmahura
User avatar
rajeevmahura
 
Posts: 144
Joined: Tue May 20, 2008 5:22 pm
Location: Bangalore | New Delhi
Full Name: Rajeev Mahura
Date Of Birth: 15 Jan 1984
Address: New Delhi
Profession: DOEACC Trainer
DOEACC Center: DOEACC DELHI
Mobile No: 0

Re: OBJECT:- To Sort The Array of Given Numbers

Unread postby manojthesingham » Tue Sep 01, 2009 7:08 am

/*
The above is the best approach to sort 2 numbers
let a =5 & b=3
a = a+b // a = 5+3
b = a-b // b = 8-3=5
a = a-b // a = 8-5=3
this type of sort belongs to O(n square) complexity
we need to traverse i=0 to n
j=0 to n
but best sorting algorithm is HEAP & QUICK SORT

*/
If world is running, then I am idle..
If I am idle, then the world is running
User avatar
manojthesingham
 
Posts: 15
Joined: Tue Sep 01, 2009 5:39 am
Location: Coimbatore,Tamilnadu
Full Name: manoj
Date Of Birth: 31 Jul 1989
Address: Coimbatore18Tamilnadu
Mobile No: 0


Return to C-Language

Who is online

Users browsing this forum: No registered users and 1 guest

cron