C Program to Implement Selection Sort using Recursion

This C Program implements a Selection sort. Selection sort works by finding the smallest unsorted item in the list and swapping it with the item in the current position. It is used for sorting unsorted list of elements.

Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Implement Selection Sort Recursively
  3.  */
  4. #include <stdio.h>
  5.  
  6. void selection(int [], int, int, int, int);
  7.  
  8. int main()
  9. {
  10.     int list[30], size, temp, i, j;
  11.  
  12.     printf("Enter the size of the list: ");
  13.     scanf("%d", &size);
  14.     printf("Enter the elements in list:\n");
  15.     for (i = 0; i < size; i++)
  16.     {
  17.         scanf("%d", &list[i]);
  18.     }
  19.     selection(list, 0, 0, size, 1);
  20.     printf("The sorted list in ascending order is\n");
  21.     for (i = 0; i < size; i++)
  22.     {
  23.         printf("%d  ", list[i]);
  24.     }
  25.  
  26.     return 0;
  27. }
  28.  
  29. void selection(int list[], int i, int j, int size, int flag)
  30. {
  31.     int temp;
  32.  
  33.     if (i < size - 1)
  34.     {
  35.         if (flag)
  36.         {
  37.             j = i + 1;
  38.         }
  39.         if (j < size)
  40.         {
  41.             if (list[i] > list[j])
  42.             {
  43.                 temp = list[i];
  44.                 list[i] = list[j];
  45.                 list[j] = temp;
  46.             }
  47.             selection(list, i, j + 1, size, 0);
  48.         }
  49.         selection(list, i + 1, 0, size, 1);
  50.     }
  51. }

$ cc pgm18.c
$ a.out
Enter the size of the list: 5
Enter the elements in list:
23
45
64
12
34
The sorted list in ascending order is
12  23  34  45  64

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

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

If you wish to look at other example programs on Searching and Sorting, go to C Programming Examples on Searching and Sorting. If you wish to look at programming examples on all topics, go to C Programming Examples.

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.