Skip to content

Instantly share code, notes, and snippets.

@pedrominicz
Last active March 27, 2020 22:37
Show Gist options
  • Save pedrominicz/33a998df34728062a3939c4188f5afe9 to your computer and use it in GitHub Desktop.
Save pedrominicz/33a998df34728062a3939c4188f5afe9 to your computer and use it in GitHub Desktop.
Select sort.
#include <stdio.h>
#define swap(x, y) { int tmp = x; x = y; y = tmp; }
void select_sort(int n, int* l) {
for(int i = 0; i < n; ++i) {
int min = i;
for(int j = i; j < n; ++j)
if(l[j] < l[min]) min = j;
swap(l[i], l[min]);
}
}
#define test(...) { \
int l[] = { __VA_ARGS__ }; \
int n = sizeof(l) / sizeof(int); \
select_sort(n, l); \
for(int i = 0; i < n; ++i) \
printf("%d ", l[i]); \
putchar('\n'); \
}
int main(void) {
test(1, 2, 6, 9, 8, 3, 10, 7, 5, 4);
test(7, 10, 2, 9, 1, 8, 5, 4, 6, 3);
test(5, 10, 3, 8, 6, 4, 7, 1, 9, 2);
test(3, 8, 10, 1, 9, 5, 7, 6, 2, 4);
test(2, 1, 8, 5, 4, 10, 7, 6, 9, 3);
test(6, 8, 3, 9, 5, 7, 2, 1, 10, 4);
test(1, 2, 6, 5, 9, 4, 8, 3, 7, 10);
test(1, 2, 8, 6, 5, 7, 10, 4, 3, 9);
test(3, 5, 4, 6, 8, 2, 10, 9, 7, 1);
test(9, 4, 3, 6, 1, 7, 2, 10, 8, 5);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment