From bbdf3b3586edadab79b3ea3a97ab0e9964301a93 Mon Sep 17 00:00:00 2001 From: James Mc Dermott Date: Sat, 22 Apr 2017 14:59:27 +0100 Subject: [PATCH] Create SelectionSort.c --- SelectionSort.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 SelectionSort.c diff --git a/SelectionSort.c b/SelectionSort.c new file mode 100644 index 00000000..9f5f0dc4 --- /dev/null +++ b/SelectionSort.c @@ -0,0 +1,40 @@ + + +#include + +int main() +{ + int array[100], n, i, j, position, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for ( i = 0 ; i < n ; i++ ) + scanf("%d", &array[i]); + + for ( i = 0 ; i < ( n - 1 ) ; i++ ) + { + position = i; + + for ( j = i + 1 ; j < n ; j++ ) + { + if ( array[position] > array[j] ) + position = j; + } + if ( position != i ) + { + swap = array[i]; + array[i] = array[position]; + array[position] = swap; + } + } + + printf("Sorted list in ascending order:\n"); + + for ( i = 0 ; i < n ; i++ ) + printf("%d\n", array[i]); + + return 0; +}