二元選擇排序,每一趟排序選出最大值和最小值柴信,只需要進(jìn)行n/2躺的排序眼耀。
void selectSortMinMax(long a[],long n){
long i,j,max,min,temp;
for (i = 0; i < n/2; i ++) {
max =i;
min =i;
for (j=i+1; j< n-i; j++) {
if (a[j]>a[max]){
max = j;//每趟排序找出最大值
continue;
}
if (a[j]< a[min]) {
min = j;//每趟排序找出最小值
}
}
temp = a[i];
a[i] =a[min];//找到最小的數(shù)據(jù)交換
a[min] = temp;
if (max ==i) {
//此時(shí) a[i]已經(jīng)被a[min]替換了
temp = a[n-i-1];
a[n-i-1] = a[min];
a[min] = temp;
}
else
{
temp = a[n-i-1];
a[n-i-1] = a[max];//找到最大的數(shù)據(jù)交換
a[max] = temp;
}
}
}
看起來二元選擇排序應(yīng)該比簡單選擇排序效率高,其實(shí)一樣改化,個(gè)人認(rèn)為沒有記錄的"跳躍式"移動