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.
/*
* C program for SELECTION sort which uses following functions
* a) To find maximum of elements
* b) To swap two elements
*/
#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, temp;
printf("Enter the value of n \n");
scanf("%d", &n);
printf("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 temp, big, j;
for (j = k - 1; j >= 1; j--)
{
big = findmax(b, j);
temp = b[big];
b[big] = b[j];
b[j] = temp;
}
return;
}
$ 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
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.
Related Posts:
- Watch Advanced C Programming Videos
- Check C Books
- Check Computer Science Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship