选择排序:简单选择排序,堆排序
解释说明:
简单选择排序:
解释说明:
稳定性:简单选择排序是不稳定的
时间复杂度: T(n) = O(n^2)
适于元素数目较少或不需完全排序的请况,比如,选出第i小的元素
Java代码:
class selectSort{
public static void main(String args[]){
int a[]={73,43,23,12,32};
int position=0;
for(int i=0;i<a.length;i++){
int j=i+1;
position=i;
int temp=a[i];
for(;j<a.length;j++)
if(a[j] < temp){
temp = a[j];
position = j;
}
a[position] = a[i];
a[i] = temp;
}
for(int i=0;i<a.length;i++){
System.out.println(a[i]);
}
}
}
C语言代码:
#include <stdio.h>
void SelectSort(int a[],int length){
int i,j,temp,position=0;
for(i = 0;i < length;i++){
j = i + 1;
position = i;
temp = a[i];
for(;j < length;j++)
if(a[j] < temp){
temp = a[j];
position = j;
}
a[position] = a[i];
a[i] = temp;
}
}
int main(){
int a[10] = {6,4,7,2,5,8,3,1,9,10},i;
SelectSort(a,10);
for(i = 0;i < 10;i++)
printf("%d ",a[i]);
return 0;
}