经典排序之选择排序


#include <stdio.h>
int main()
{
int i,j,index,k;
int tem,length;
int a[10]={2,24,3,19,45,12,1,66,34,7};
// length = a.length();
length = 10;
printf("Before ordered:\n");
for(i = 0; i < length; i++)
printf("%d ",*(a+i));
printf("\n\n");
for(i = 0; i < length; i++)
{
index = i;
for(j = i+1; j < length; j++)
{
if(a[j]>a[index])
index = j;
}
if(index != i)
{
tem = a[index];
a[index] = a[i];
a[i] = tem;
}
printf("The %d time select order:\n",i);
for(k = 0; k < length; k++)
printf("%d ",*(a+k));
printf("\n\n");
}
printf("After ordered:\n");
for(i = 0; i < length; i++)
printf("%d ",*(a+i));
printf("\n");
return 0;
}


选择排序类似于冒泡排序,但是不同之处是,每次找到最大(或最小)的下标然后放在前边,其复杂度o(n)^2

posted on 2012-03-25 12:27  NewPanderKing  阅读(203)  评论(0编辑  收藏  举报

导航