-
Re: question of kth smallest number
"alix" <a160978@yahoo.com> wrote:
>
>I need a code to find the kth smalest number in an unsorted array.
dear alix,
since ur question was not really clear.i think kth smallest element in any
unsorted array can be found by first sorting it in ascending order and reading
then reading the kth element of the array.as under
void main()
{
int *a;
int k,n;
printf("enter the kth position u want and the array length");
scanf("%d %d",&k,&n);
printf("enter the elements of array");
for(int i=0;i<=n;i++)
scanf("%d",(a+i));
for(i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(*(a+i)>*(a+j))
{
temp=*(a+i);
*(a+i)=*(a+j);
*(a+j)=temp;
}
}
}
printf("the %d smallest element is %d",k,*(a+k));
}
\\i think this should serve ur purpose,if not mail me at my e-mail.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks