Skip to content

Instantly share code, notes, and snippets.

@sasaki-shigeo
Last active October 25, 2017 03:40
Show Gist options
  • Save sasaki-shigeo/e273651ae94a63fe5f4c4027baf5e93d to your computer and use it in GitHub Desktop.
Save sasaki-shigeo/e273651ae94a63fe5f4c4027baf5e93d to your computer and use it in GitHub Desktop.
Selection sort program in C
#include <stdlib.h>
#include <stdio.h>
typedef int T;
T data[] = { 2, 9, 4, 8, 5, 6, 1 };
const int N = sizeof(data)/sizeof(T);
void ssort(int (* comp)(int, int), int a[]) {
for (int k = 1; k < N; k++) {
for (int i = 0; i < k; i++) {
if (comp(a[i], a[k]) > 0) {
int tmp = a[i];
a[i] = a[k];
a[k] = tmp;
}
}
}
}
int compareTo(T x, T y) {
return y - x;
}
int main(int argc, char *argv[]) {
ssort(compareTo, data);
for (int i = 0; i < N; i++) {
printf("%d\n", data[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment