From fb0c97cb976b4d2f14fc5dea9ffbb35fa3149b81 Mon Sep 17 00:00:00 2001 From: Hyder Date: Mon, 18 Aug 2025 19:08:30 +0300 Subject: I have sorted numbers using selection sort :D --- SelectionSort/SelectionSort.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 SelectionSort/SelectionSort.c (limited to 'SelectionSort/SelectionSort.c') diff --git a/SelectionSort/SelectionSort.c b/SelectionSort/SelectionSort.c new file mode 100644 index 0000000..e9845c6 --- /dev/null +++ b/SelectionSort/SelectionSort.c @@ -0,0 +1,35 @@ +#include + +void selection_sort(int *Array, int length) { + for (int i = 0;i < length;i++) { + int current = Array[i]; + int smallest = Array[i]; + int tmp = i; + for (int j = i + 1;j < length;j++) { + if (Array[j] < smallest) { + smallest = Array[j]; + tmp = j; + } + } + Array[i] = smallest; + Array[tmp] = current; + } +} + +int main() { + + int Array[] = {95, 12, 84, 23, 7, 66, 41, 59, 18, 32, 73, 5, 91, 46, 27, 63, 14, 39, 100, 2, 77, 50, 35, 9, 81, 3, 45, 22, 60, 88, 11, 36, 71, 25, 49, 16, 68, 1, 53, 29, 86, 19, 43, 30, 62, 8, 79, 4, 21, 98}; + int ArrayLength = sizeof(Array) / sizeof(Array[0]); + printf("Not Sorted: "); + for (int i = 0;i < ArrayLength;i++) { + printf("%d ", Array[i]); + } + printf("\n\n"); + selection_sort(Array, ArrayLength); + printf("Sorted: "); + for (int i = 0;i < ArrayLength;i++) { + printf("%d ", Array[i]); + } + printf("\n"); + return 0; +} \ No newline at end of file -- cgit v1.2.3