zoukankan      html  css  js  c++  java
  • 并归排序(看别人的看不懂,自己写了一个),排序思想是一样的


    public int[] intArray = {8,5,10,55,88,22,14,36,82,54,10,74,22}; @RequestMapping(value="hello") public int[] getHello(int[] intArray1) { //获取最新排序 intArray1 = intArray; //计算排好序的元素个数,如果次数为数组长度,说明排序成功 int count = 0; int index0 = 0; int indexlast = intArray1.length; for(int i=0; i< indexlast-1; i++){ int start = intArray1[index0]; int next = intArray1[index0+1]; if(start <= next){ count++; index0++; }else{ //前后两元素互换 intArray1[index0] = next; intArray1[index0+1] = start; index0++; } } //把排序结果赋值给参数 ,排序成功则当做结果输出 intArray = intArray1; //如果目前的排好序的元素个数不等于数组长度,继续排序 if(count != indexlast-1){ getHello(intArray); } return intArray; }

    归并排序的基本思想

    将待排序序列R[0...n-1]看成是n个长度为1的有序序列,将相邻的有序表成对归并,得到n/2个长度为2的有序表;将这些有序序列再次归并,得到n/4个长度为4的有序序列;如此反复进行下去,最后得到一个长度为n的有序序列。

    综上可知:

    归并排序其实要做两件事:

    (1)“分解”——将序列每次折半划分

    (2)“合并”——将划分后的序列段两两合并后排序

  • 相关阅读:
    STL中string的源码解读
    Sublime插件:Terminal
    sublime text3安装Package Control
    [转]Sublime Text操作
    python itertools模块实现排列组合
    pandas 选择某几列
    更改pandas dataframe 列的顺序
    pandas之groupby分组与pivot_table透视表
    IPython notebook快捷键(Jupyter notebook)
    人生的意义
  • 原文地址:https://www.cnblogs.com/kkw105/p/10138487.html
Copyright © 2011-2022 走看看