#include <stdio.h>
#include "tool.h"
/*
选择排序算法:
1.从第一个元素开始,从后续待排序序列中选择最小的元素放在第一个位置
2.然后第二个元素,从后续待排序序列中选择最小的元素放在第二个位置
3.依次类推
算法复杂度:O(N^2)
*/
void simpleSelctionSort(int a[],int len) {
int minIndex;
int i, j;
for (i = 0; i < len-1; i++) {
minIndex = i;
for (j = i + 1; j < len; j++) {
if (a[j] < a[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
swap(&a[i], &a[minIndex]);
}
}
}
int main() {
int a[10] = { 9,1,8,5,7,6,4,3,10,2 };
int len = sizeof(a) / sizeof(a[0]);
printf("排序前: ");
printfArray(a, len);
simpleSelctionSort(a, len);
printf("排序前: ");
printfArray(a, len);
return 0;
}
来源:oschina
链接:https://my.oschina.net/u/568675/blog/4330127