By using "Selection Sort " we can arrange the number in the order of "ascending" or "descending".
Selection Sort_ algorithm_ implementation in C |
|
1 | #include <stdio.h> |
2 | int main() |
3 | { |
4 | int array[100], n, c, d,swap,position; |
5 | printf("Enter_number_of_elements\n"); |
6 | scanf("%d", &n); |
7 | printf("Enter %d _integers\n", n); |
8 | for ( c = 0 ; c < n ; c++ ) |
9 | scanf("%d", &array[c]); |
10 | for ( c = 0 ; c < ( n - 1 ) ; c++ ) |
11 | { |
12 | position = c; |
13 | for ( d = c + 1 ; d < n ; d++ ) |
14 | { |
15 | if ( array[position] > array[d] ) |
16 | position = d; |
17 | } |
18 | if ( position != c ) |
19 | { |
20 | swap = array[c]; |
21 | array[c] = array[position]; |
22 | array[position] = swap; |
23 | } |
24 | } |
25 | printf("Sorted_list in ascending_order:\n"); |
26 | for ( c = 0 ; c < n ; c++ ) |
27 | printf("%d\n", array[c]); |
28 | return 0; |
29 | } |