You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Htu1/二元选择排序

32 lines
863 B

//二元选择法排序
#include<stdio.h>
int main()
{
int i,j,y,k,c,max,min,temp,a[]={5,2,1,4,3,6,9,7,8,0};
int length=sizeof(a)/sizeof(a[0]);
for(i=0;i<length/2;i++)
{
max=i;
min=i;
for(j=i+1;j<length-i;j++)
{
if(a[max]<a[j])
max=j;
if(a[min]>a[j])
min=j;
}
k=a[max];
y=a[min];
a[min]=a[i];
a[max]=a[length-1-i];
a[i]=y;
a[length-1-i]=k;
}
for(i=0;i<length;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
//运行原理:同时查找最大值和最小值,先是把头尾的值放到最大最小的那两个位置那里,在把最小和最大值分别放到头尾。
//运行结果:Process exited after 0.05125 seconds with return value 0