选择法排序

选择法排序

原理示意:

{2 4 3 6 1 8 7 9 0 5}    //初始
                             //在第1个到第10个元素间找到最小的, 与第1个元素交换
 0{ 4 3 6 1 8 7 9 2 5}
                             //在第2个到第10个元素间找到最小的, 与第2个元素交换
 0 1{ 3 6 4 8 7 9 2 5}
                             //如此继续下去
.....

===================选择法排序 by claydodo======================
  1. #include <stdio.h>

  2. #define ARRAY_SIZE 10

  3. int main()
  4. {
  5.     //Input the array
  6.     int array[ARRAY_SIZE];
  7.     printf("Please input %d integer numbers: ", ARRAY_SIZE);
  8.     int i;
  9.     for(i=0;i<ARRAY_SIZE;i++)
  10.       scanf("%d", &array[i]);

  11.     int index_now;
  12.     for(index_now=0; index_now < ARRAY_SIZE-1; index_now++)
  13.     {
  14.         int index_min=index_now;
  15.         int index_searching;
  16.         //Find the minimal one in { array[index_now] ~ array[ARRAY_SIZE-1] }
  17.         for(index_searching=index_now; index_searching < ARRAY_SIZE; index_searching++)
  18.         {
  19.             if(array[index_searching]<array[index_min])
  20.               index_min=index_searching;
  21.         }

  22.         //Swap array[index_now] and array[index_min]
  23.         if(index_min != index_now)
  24.         {
  25.             int temp;
  26.             temp=array[index_now];
  27.             array[index_now]=array[index_min];
  28.             array[index_min]=temp;
  29.         }
  30.     }
  31.    
  32.     //Output the result
  33.     for(i=0;i<ARRAY_SIZE;i++)
  34.       printf("%d ",array[i]);

  35.     printf("/n");

  36.     return 0;
  37. }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值