C Program to Implement Selection Sort using Functions

This C Program implements selection sort method using functions. Selection sort is among the simplest of sorting techniques. It works as follows: first find the smallest in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted.

Here is source code of the C program to implement selection sort method using functions. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program for SELECTION sort which uses following functions
  3.  * a) To find maximum of elements
  4.  * b) To swap two elements
  5.  */
  6. #include <stdio.h>
  7.  
  8. int findmax(int b[10], int k);
  9. void exchang(int b[10], int k);
  10. void main()
  11. {
  12.     int array[10];
  13.     int i, j, n, temp;
  14.  
  15.     printf("Enter the value of n \n");
  16.     scanf("%d", &n);
  17.     printf("Enter the elements one by one \n");
  18.     for (i = 0; i < n; i++)
  19.     {
  20.         scanf("%d", &array[i]);
  21.     }
  22.     printf("Input array elements \n");
  23.     for (i = 0; i < n ; i++)
  24.     {
  25.         printf("%d\n", array[i]);
  26.     }
  27.     /*  Selection sorting begins */
  28.     exchang(array, n);
  29.     printf("Sorted array is...\n");
  30.     for (i = 0; i < n; i++)
  31.     {
  32.         printf("%d\n", array[i]);
  33.     }
  34. }
  35. /*  function to find the maximum value */
  36. int findmax(int b[10], int k)
  37. {
  38.     int max = 0, j;
  39.     for (j = 1; j <= k; j++)
  40.     {
  41.         if (b[j] > b[max])
  42.         {
  43.             max = j;
  44.         }
  45.     }
  46.     return(max);
  47. }
  48. void exchang(int b[10], int k)
  49. {
  50.     int  temp, big, j;
  51.     for (j = k - 1; j >= 1; j--)
  52.     {
  53.         big = findmax(b, j);
  54.         temp = b[big];
  55.         b[big] = b[j];
  56.         b[j] = temp;
  57.     }
  58.     return;
  59. }

$ cc pgm33.c
$ a.out
Enter the value of n
4
Enter the elements one by one
57
90
34
78
Input array elements
57
90
34
78
Sorted array is...
34
57
78
90

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.

If you find any mistake above, kindly email to [email protected]

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.