排序算法2----选择法

选择法的本质:不想冒泡法一个一个的交换,选择法,是先找出i小的数字找出来,然后,跟第i个数交换一下。一轮子循环顶多值交换一次

如:

6 3 1 4 2 

i=0 ,找出最小的数,再跟第0个数交换 如1和6交换 1 3 6 4 2

i=1,找出第二小的数,再跟第1个数交换,如3和2交换 1 2 5 4 3

i=3,找出第三小的数,在跟第2个数交换,如5和3 交换 1 2 3 4 5

i=4 第四小的数字已经成立,不需要交换

void exchang_sort(int a[],int n)
{
   int i=0,j=0;
   int temp,Min_indext;
   for (i=0;i<n-1,i++)
   	{
   	 index =i;
   	for(j=i+1;j<n;j++)
   	   if(a[Min_index]>a[j])
   		{
   		 
   		  Min_index=j ;
   		}
	   if(i!=Min_index)
	   	{
	          temp=a[i];
	          a[i]=a[Min_index];
		  a[Mind_dex]=temp;
	   	}
   }
}


猜你喜欢

转载自blog.csdn.net/fengliang191/article/details/38639499