选择排序思想
一次选择最小的与最前面i的进行交换。一共需要n-1次循环
选择排序性能
时间复杂度O(n^2)
空间负责度O(1)
不稳定
选择排序代码完整版(c++)
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
const int NUM = 100+5;
int num[NUM];
//选择排序
void SelectionSort(int num[],int len){
for(int i=0;i<len-1;i++){
int temp = 0;
int index = i;
//找最小的
for(int j=i+1;j<len;j++){
if(num[j]<num[index]){
index = j;
}
}
//与最小的进行交换
temp = num[index];
num[index] = num[i];
num[i]=temp;
for(int j=0;j<len;j++){
cout<<num[j]<<" ";
}
cout<<endl;
}
}
int main(int argc, char *argv[]) {
int len;
cin>>len;
for(int i=0;i<len;i++){
cin>>num[i];
}
SelectionSort(num,len);
return 0;
}