如题
#include<stdio.h>
void SelectSort(int a[],int N);
int main(){
int a[10] = {3,7,4,9,1,0,2,5,6,8};
//InsertSort(a,10);
SelectSort(a,10);
return 0;
}
//选择排序
void SelectSort(int a[],int N){
int min = 9999;
int temp,count;
for (int i = 0; i < N; ++i) {
min = a[i];
for (int j = i + 1; j < N; ++j) {
if (a[j] < min){
min = a[j];
count = j;
}
}
temp = a[i];
a[i] = a[count];
a[count] = temp;
}
for (int k = 0; k < N; ++k) {
printf("%d ",a[k]);
}
}
选择排序思想很简单,每次找出待排元素中最小的元素放到待排序列的最前面即可。
然后就排好了!