编译环境:gcc4.4.3
#include <stdio.h> #define swap(a,b) {if(a!=b){(a)^=(b); (b)^=(a); (a)^=(b);}} #define MAX 10 void sort(int *num) { int key, i, j; for (i = 0; i < MAX-1; i++) { key = i; for (j = i; j < MAX; j++) { if (num[j] < num[key]) { key = j; } } swap(num[i], num[key]); } } void prtarry(int* sum) { int i; printf("array: "); for(i=0; i<MAX; i++) { printf("%2d ", sum[i]); } printf("\n"); } int main(int argc, const char *argv[]) { int num[MAX] = {5,7,8,2,4,6,9,0,1,3}; prtarry(num); sort(num); prtarry(num); return 0; }
转载于:https://www.cnblogs.com/tonykong/archive/2013/02/05/selection_sort.html
相关资源:数据结构—成绩单生成器