C program for finding the kth smallest element in an array

				
					#include<stdio.h> 
#include<conio.h> 
void main() 
{ 
int *ar, i, j, n, t, k; 
clrscr(); 
printf(“enter the number of elements in the array”);
scanf(“%d”, &n); 
ar=(int*)malloc(sizeof(int)*n); 
for(i=0;i<n;i++) 
{ 
printf(“Enter a number”); 
scanf(“%d”, &ar[i]); 
} 
for(j=i+1; j<n; j++) 
{ 
if(ar[i]<ar[j]) 
{
t=ar[i];
ar[i]=ar[j]; 
ar[j]=t;
} 
} 
} 
printf(“Elements of array after sorting”); 
for(i=0; i<n; i++) 
printf(“%d”, ar[i]); 
printf(“Which smallest element do you want to determine”); 
scanf(“%d”, &k); 
if(k<=n) 
printf(“Desired smallest element is %d”, arr[k-1]); 
else 
printf(“Please enter a valid value for finding the particular smallest element”); 
getch(); 
}
				
			
Output: 
Enter the number of elements in the array:5
Enter a number: 33
Enter a number: 34
Enter a number: 46
Enter a number: 68
Enter a number: 47
Elements of array after sorting:
32
34
46
47
68
Which smallest element do you want to determine?
3
Desired smallest element is
46

Leave a Reply