C Program to Find kth Smallest Element in Array using Partitioning

This is a C Program to find kth smallest element by method of partitioning. The same partitioning function which is used for Quicksort algorithm can be used.

Here is source code of the C Program to Find kth Smallest Element by the Method of Partitioning the Array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<time.h>
  4. #include<stdlib.h>
  5.  
  6. int N = 20;
  7. int A[20];
  8.  
  9. void swap(int dex1, int dex2) {
  10.     int temp = A[dex1];
  11.     A[dex1] = A[dex2];
  12.     A[dex2] = temp;
  13. }
  14.  
  15. int partition(int start, int end) {
  16.     int i = start + 1;
  17.     int j = i;
  18.     int pivot = start;
  19.     for (; i < end; i++) {
  20.         if (A[i] < A[pivot]) {
  21.             swap(i, j);
  22.             j++;
  23.         }
  24.     }
  25.     if (j <= end)
  26.         swap(pivot, (j - 1));
  27.  
  28.     return j - 1;
  29. }
  30.  
  31. void quick_sort(int start, int end, int K) {
  32.     int part;
  33.     if (start < end) {
  34.         part = partition(start, end);
  35.         if (part == K - 1)
  36.             printf("kth smallest element : %d ", A[part]);
  37.         if (part > K - 1)
  38.             quick_sort(start, part, K);
  39.         else
  40.             quick_sort(part + 1, end, K);
  41.     }
  42.     return;
  43. }
  44.  
  45. int main(int argc, char **argv) {
  46.     int i;
  47.     time_t seconds;
  48.     time(&seconds);
  49.     srand((unsigned int) seconds);
  50.  
  51.     for (i = 0; i < N; i++)
  52.         A[i] = rand() % (1000 - 1 + 1) + 1;
  53.  
  54.     printf("The original sequence is:  ");
  55.     for (i = 0; i < N; i++)
  56.         printf("%d ", A[i]);
  57.  
  58.     printf("\nEnter the Kth smallest you want to find: ");
  59.     int k;
  60.     scanf("%d", &k);
  61.     quick_sort(0, N, k);
  62. }

Output:

$ gcc KthSmallestUsingPartitioning.c
$ ./a.out
 
The original sequence is:  909 967 552 524 735 383 616 718 904 945 730 173 143 954 482 307 228 35 224 703 
Enter the Kth smallest you want to find: 3
kth smallest element : 173

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.