In this program, we will share the selection sort program in c programming language. Selection sort is a sorting algorithm, which is specifically known for its simplicity, and it has performance advantages.
In this tutorial, we will sort the four elements (6, 9, 2, 8) with the help of selection sort.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #include <stdio.h> int main() { int i, j, num, arr[250], pos, temp; printf("Enter number of elements\n"); scanf("%d", &num); printf("Enter %d integers\n", num); for ( i = 0 ; i < num ; i++ ) scanf("%d", &arr[i]); for ( i = 0 ; i < ( num - 1 ) ; i++ ) { pos = i; for ( j = i + 1 ; j < num ; j++ ) { if ( arr[pos] > arr[j] ) pos = j; } if ( pos != i ) { temp = arr[i]; arr[i] = arr[pos]; arr[pos] = temp; } } printf("Selection sorting in ascending order:\n"); for ( i = 0 ; i < num ; i++ ) printf("%d\n", arr[i]); return 0; } |
Enter number of elements
4
Enter 4 integers
6
9
2
8
Selection sorting in ascending order:
2
6
8
9
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: c program, c program examples, c program examples with output, c program for selection sort, c program for selection sorting, program for selection sort, program for selection sort in c, program of selection sort in c