diff options
Diffstat (limited to 'algo1w2/selection.c')
-rw-r--r-- | algo1w2/selection.c | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/algo1w2/selection.c b/algo1w2/selection.c new file mode 100644 index 0000000..ba706f0 --- /dev/null +++ b/algo1w2/selection.c @@ -0,0 +1,20 @@ +#include "main.h" +#include "selection.h" + +unsigned smallest_index(int* arr, unsigned size) { + if (size == 0) return 0; + unsigned min_idx = 0; + for (unsigned i = 0; i < size; i++) + if (arr[i] <= arr[min_idx]) + min_idx = i; + return min_idx; +} + +void selection_sort(int* arr, unsigned size) { + for (unsigned tail = 0; tail < size; tail++) { + unsigned smallest = tail + smallest_index(&arr[tail], size - tail); + swap(arr[tail], arr[smallest]); + printf("tail %d: ", tail); + print_arr(arr, size); + } +} |