Implement Selection Sort Method using Functions in C

In this Searching and Sorting in C, we will have a look at how to write a C Program to implement selection sort using functions in C Programming Language.

You can also have a look at the C Program to Implement Selection Sort Recursively.

C Program to Implement the Selection Sort Method using Functions

#include <stdio.h>
int findmax(int b[10], int k);
void exchang(int b[10], int k);
void main()
{
   int array[10];
   int i, j, n, tempvar;
   printf("Please enter the value of n \n");
   scanf("%d", &n);
   printf("Please enter the elements one by one \n");
   for (i = 0; i < n; i++)
   {
       scanf("%d", &array[i]);
   }
   printf("Input array elements \n");
   for (i = 0; i < n ; i++)
   {
       printf("%d\n", array[i]);
   }
   /*  Selection sorting begins */
   exchang(array, n);
   printf("Sorted array is...\n");
   for (i = 0; i < n; i++)
   {
       printf("%d\n", array[i]);
   }
}
/*  function to find the maximum value */
int findmax(int b[10], int k)
{
   int max = 0, j;
   for (j = 1; j <= k; j++)
   {
       if (b[j] > b[max])
       {
           max = j;
       }
   }
   return(max);
}
void exchang(int b[10], int k)
{
   int  tempvar, big, j;
   for (j = k - 1; j >= 1; j--)
   {
       big = findmax(b, j);
       tempvar = b[big];
       b[big] = b[j];
       b[j] = tempvar;
   }
   return;
}

Leave a Comment

Your email address will not be published. Required fields are marked *