zoukankan      html  css  js  c++  java
  • 归并排序

    归并排序:时间复杂度为~O(nlogn)--又称合并排序

    归并(Merge)排序法是将两个(或两个以上)有序表合并成一个新的有序表,

    即把待排序序列分为若干个有序的子序列,再把有序的子序列合并为整体有序序列。

    归并排序的一个缺点是它需要存储器有另一个大小等于数据项数目的数组。如果初始数组几乎占满整个存储器,那么归并排序将不能工作,但是如果有足够的空间,归并排序会是一个很好的选择。

    [php] view plain copy
     
    1. <?php  
    2. $arrStoreList = array(3,2,4,1,5);  
    3. $sort = new Merge_sort();  
    4. $sort->stableSort($arrStoreList, function ($a, $b) {    // function ($a, $b)匿名函数  
    5.             return $a < $b;  
    6. });  
    7. //静态调用方式也行  
    8. /*Merge_sort:: stableSort($arrStoreList, function ($a, $b) { 
    9.             return $a < $b; 
    10. });*/  
    11. print_r($arrStoreList);  
    12.   
    13. class Merge_sort{  
    14.   
    15.  public static function stableSort(&$array, $cmp_function = 'strcmp') {  
    16.         //使用合并排序  
    17.         self::mergeSort($array, $cmp_function);  
    18.         return;  
    19.     }  
    20.  public static function mergeSort(&$array, $cmp_function = 'strcmp') {  
    21.         // Arrays of size < 2 require no action.  
    22.         if (count($array) < 2) {  
    23.             return;  
    24.         }  
    25.         // Split the array in half  
    26.         $halfway = count($array) / 2;  
    27.         $array1 = array_slice($array, 0, $halfway);  
    28.         $array2 = array_slice($array, $halfway);  
    29.         // Recurse to sort the two halves  
    30.         self::mergeSort($array1, $cmp_function);  
    31.         self::mergeSort($array2, $cmp_function);  
    32.         // If all of $array1 is <= all of $array2, just append them.  
    33. //array1 与 array2 各自有序;要整体有序,需要比较array1的最后一个元素和array2的第一个元素大小  
    34.         if (call_user_func($cmp_function, end($array1), $array2[0]) < 1) {    
    35.             $array = array_merge($array1, $array2);  
    36.   
    37.             return;  
    38.         }  
    39.         // 将两个有序数组合并为一个有序数组:Merge the two sorted arrays into a single sorted array  
    40.         $array = array();  
    41.         $ptr1 = $ptr2 = 0;  
    42.         while ($ptr1 < count($array1) && $ptr2 < count($array2)) {  
    43.             if (call_user_func($cmp_function, $array1[$ptr1], $array2[$ptr2]) < 1) {  
    44.                 $array[] = $array1[$ptr1++];  
    45.             } else {  
    46.                 $array[] = $array2[$ptr2++];  
    47.             }  
    48.         }  
    49.         // Merge the remainder  
    50.         while ($ptr1 < count($array1)) {  
    51.             $array[] = $array1[$ptr1++];  
    52.         }  
    53.         while ($ptr2 < count($array2)) {  
    54.             $array[] = $array2[$ptr2++];  
    55.         }  
    56.         return;  
    57.     }  
    58. }  
    59. ?>  

    输出结果:Array ( [0] => 5 [1] => 4 [2] => 3 [3] => 2 [4] => 1)

    算法原理分析:关键是理解递归调用及其返回函数的原理

  • 相关阅读:
    idea 2019 版本的 破解方式
    php-fpm 服务的一些常见配置
    redis通过pipeline提升吞吐量
    redis 简易监控的几种方法
    mongodb查询操作分析
    mongodb监控常用方法
    Linux 系统监控常用命令
    mongodb 认证鉴权那点事
    使用H2数据库进行单元测试
    性能测试-Jmeter3.1 使用技巧
  • 原文地址:https://www.cnblogs.com/datang6777/p/7202300.html
Copyright © 2011-2022 走看看