排序算法: 选择排序法

选择排序详解
本文详细介绍了选择排序算法的基本原理和实现步骤。通过实例演示了选择排序的过程,并提供了完整的代码实现。

选择排序法:在排序组中,选出最小(或者最大)的个数与第1个位置的数交换;然后在剩下的数当中再找最小(或者最大)的与第2个位置的数交换,依次类推,直到第n-1个元素(倒数第二个数)和第n个元素(最后个数)比较为止。

实现思路:

1,每次先找到最小数(最大数);

2,第i趟找到最小数和第i个数组互换;

3,重复(1)(2),直到最后排序成功;

 

 01234567
 610490527783
0 3 1090  5277 
1 310 90 52 77 
2 3 4 9052 77 10 
3 3 4 6 8 52 77 90 10
4 3 48 10 77 90 52
5 3 4 6 8

 10

 52 90 77
6 3 4 6 8 10 52 77

 90

 

 

 

 

 

 

 

 

 

 

代码实现:

/**************************************************************************************
 *  Description:
 *   Input Args:
 *  Output Args:
 * Return Value:
 *************************************************************************************/
int select_sort (int array[], int n)
{
    int i, j;
    int temp, little;

    for(i=0; i<n-1; i++)
    {
        /* find the Min one */
        little=i;
        for(j=i+1; j<n; j++)
        {
            if(array[little] > array[j]) //小到大
                little=j;
        }
        
    /* exchange the postion if not the Min one */ if(little != i) { temp = array[little]; array[little] = array[i]; array[i] = temp; } } return 0; } /* ----- End of select_sort() ----- */

 

 

参考链接:http://blog.youkuaiyun.com/caryaliu/article/details/7438592

转载于:https://www.cnblogs.com/xiaoxing/p/3981624.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值