diff options
Diffstat (limited to 'rushs/tinyprintf/selection_sort')
| -rw-r--r-- | rushs/tinyprintf/selection_sort/selection_sort.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/rushs/tinyprintf/selection_sort/selection_sort.c b/rushs/tinyprintf/selection_sort/selection_sort.c new file mode 100644 index 0000000..98adc7e --- /dev/null +++ b/rushs/tinyprintf/selection_sort/selection_sort.c @@ -0,0 +1,30 @@ +#include <stddef.h> + +void swap(int *a, int *b) +{ + int tmp = *a; + *a = *b; + *b = tmp; +} + +unsigned array_min(const int arr[], unsigned start, unsigned size) +{ + unsigned min = start; + for (; start < size; start++) + { + if (arr[min] > arr[start]) + { + min = start; + } + } + return min; +} + +void selection_sort(int arr[], unsigned size) +{ + for (size_t i = 0; i < size; i++) + { + unsigned j = array_min(arr, i, size); + swap(&(arr[i]), &(arr[j])); + } +} |
