• 排序算法: 选择排序法


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

    实现思路:

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

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

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

      0 1 2 3 4 5 6 7
      6 10 4 90 52 77 8 3
    0  3  10 90   52 77 
    1  3 10  90  52  77 
    2  3  4  90 52  77  10 
    3  3  4  6  8  52  77  90  10
    4  3  4 8  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.csdn.net/caryaliu/article/details/7438592

  • 相关阅读:
    DFS 算法总结
    拆分Cocos2dx渲染部分代码
    [OpenGL]纹理贴图实现 总结
    [LeetCode] Minimum Size Subarray Sum 最短子数组之和
    LRU Cache 题解
    Substring with Concatenation of All Words 题解
    multimap和multiset 认知和使用
    OpenGL ES 3.0 基础知识
    Cocos2dx坐标转换
    视图
  • 原文地址:https://www.cnblogs.com/xiaoxing/p/3981624.html
Copyright © 2020-2023  润新知