Last active
October 25, 2017 03:40
-
-
Save sasaki-shigeo/e273651ae94a63fe5f4c4027baf5e93d to your computer and use it in GitHub Desktop.
Selection sort program in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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