diff options
| author | Hyder <hyder@hyderhadi.xyz> | 2025-08-18 19:08:30 +0300 |
|---|---|---|
| committer | Hyder <hyder@hyderhadi.xyz> | 2025-08-18 19:08:30 +0300 |
| commit | fb0c97cb976b4d2f14fc5dea9ffbb35fa3149b81 (patch) | |
| tree | 774514c8b5c44276e1e869bfecaeff323bb30e25 /SelectionSort/SelectionSort.c | |
| parent | 06810e8041b75d888615b73592215fe92088f0a7 (diff) | |
I have sorted numbers using selection sort :D
Diffstat (limited to 'SelectionSort/SelectionSort.c')
| -rw-r--r-- | SelectionSort/SelectionSort.c | 35 |
1 files changed, 35 insertions, 0 deletions
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 <stdio.h> + +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 |
