您好,欢迎访问一九零五行业门户网

Selection Sort,selectionsort_PHP教程

selection sort,selectionsort
red is current min. yellow is sorted list. blue is current item. (picture from wikipedia, a little too fast)
10 numbers. sort as ascend.
1. find the min of the 10 and switch it with a[0], requires 9 times of compare
2. find the min of the rest 9 and switch it with a[1], requires 8 times of compare
. . .
1, 10, 9
2, 9, 8
3, 8, 7
4, 7, 6
5, 6, 5
6, 5, 4
7, 4, 3
8, 3, 2
9, 2, 1
in conclusion: for 10 numbers, we need 9 times of finding the min, each has one-short amount of numbers to compare.
implementation in php:
1 c语言 选择排序 输入k个数字,递减输出的selection_sort(array, k)程序
void selection_sort(int array[],int k)
{
int i,j,m,t;
for(i=0;im=i;
for(j=i+1;jif(array[j]m=j; //k记下目前找到的最小值所在的位置
if(m!=i){
t=array[i];
array[i]=array[m];
array[m]=t;
}
}
}
void main(){
int a[10];
for (int i=0;iscanf(%d,&a[i]);
selection_sort(a,10);
printf(排序结果为:);
for (i=0;iprintf(%d\n,a[i]);
}
c语言 选择排序 输入k个数字,递减输出的selection_sort(array, k)程序
void selection_sort(int array[],int k)
{
int i,j,m,t;
for(i=0;im=i;
for(j=i+1;jif(array[j]m=j; //k记下目前找到的最小值所在的位置
if(m!=i){
t=array[i];
array[i]=array[m];
array[m]=t;
}
}
}
void main(){
int a[10];
for (int i=0;iscanf(%d,&a[i]);
selection_sort(a,10);
printf(排序结果为:);
for (i=0;iprintf(%d\n,a[i]);
}
http://www.bkjia.com/phpjc/908123.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/908123.htmltecharticleselection sort,selectionsort red is current min. yellow is sorted list. blue is current item. (picture from wikipedia, a little too fast) 10 numbers. sort as ascend. 1. find the...
其它类似信息

推荐信息