summaryrefslogtreecommitdiff
path: root/algo1w2/selection.c
blob: ba706f00dabef99fc31d9e3fa848a2d9e4c074e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
	}
}