From e0a7951f65bd367da06e9cc901fa3e989e045c3b Mon Sep 17 00:00:00 2001 From: Isuru Perera Date: Sun, 10 Oct 2021 07:41:31 +0530 Subject: [PATCH] Added SelectionSort.c --- SelectionSort.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 SelectionSort.c diff --git a/SelectionSort.c b/SelectionSort.c new file mode 100644 index 0000000..7883baa --- /dev/null +++ b/SelectionSort.c @@ -0,0 +1,37 @@ +#include +int main() +{ + int array[100], n, c, d, position, t; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0; c < (n - 1); c++) // finding minimum element (n-1) times + { + position = c; + + for (d = c + 1; d < n; d++) + { + if (array[position] > array[d]) + position = d; + } + if (position != c) + { + t = array[c]; + array[c] = array[position]; + array[position] = t; + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} \ No newline at end of file