C语言实现选择法排序

版权声明:感谢观看我的博客https://me.csdn.net/weixin_43794314 https://blog.csdn.net/weixin_43794314/article/details/85220169

选择法排序:
依次使用当前取得的元素和其后面的元素进行比较。
在第一个元素和其后面的元素顺次比较时,可借助中间变量
对两个数进行交换。

void  fun(int a[], int n)
{ int p, temp;
  for (int i = 0; i < n-1; i++) 
  {
    p = i;
    for (int j = i; j < n; j++)
      if(a[p] > a[j])
        p = j;
    temp = a[p];
	a[p] = a[i]; 
	a[i] = temp;
  }
}
void main()
{
  int a[N];
  printf("输入数据:\n") ;
  for(int i = 0; i < N; i++) 
	  scanf("%d",&a[i]);
  fun(a,N);
  printf("排序后的数据:\n") ;
  for(int i = 0; i < N; i++) 
	  printf("%5d ",a[i]); 
  printf("\n");
}

猜你喜欢

转载自blog.csdn.net/weixin_43794314/article/details/85220169