• 归并排序法


    1.之前在网上看到一个很有思想的算法,学习一下,然后代码保存下来,已经忘了出处,有知道的可以提示我。

     1 <!DOCTYPE html>
     2 <html>
     3     <head>
     4         <meta charset="UTF-8">
     5         <title></title>
     6     </head>
     7     <body>
     8         <script>
     9 var mergeSort = (function() {
    10     // 合并
    11     var _merge = function(left, right, prop) {
    12         var result = [];
    13  
    14         // 对数组内成员的某个属性排序
    15         if (prop) {
    16             while (left.length && right.length) {
    17                 if (left[0][prop] <= right[0][prop]) {
    18                     result.push(left.shift());
    19                 } else {
    20                     result.push(right.shift());
    21                 }
    22             }
    23         } else {
    24             // 数组成员直接排序
    25             while (left.length && right.length) {
    26                 if (left[0] <= right[0]) {
    27                     result.push(left.shift());
    28                 } else {
    29                     result.push(right.shift());
    30                 }
    31             }
    32         }
    33  
    34         while (left.length)
    35             result.push(left.shift());
    36  
    37         while (right.length)
    38             result.push(right.shift());
    39  
    40         return result;
    41     };
    42  
    43     var _mergeSort = function(arr, prop) { // 采用自上而下的递归方法
    44         var len = arr.length;
    45         if (len < 2) {
    46             return arr;
    47         }
    48         var middle = Math.floor(len / 2),
    49             left = arr.slice(0, middle),
    50             right = arr.slice(middle);
    51         return _merge(_mergeSort(left, prop), _mergeSort(right, prop), prop);
    52     };
    53  
    54     return function(arr, prop, order) {
    55         var result = _mergeSort(arr, prop);
    56         if (!order || order.toLowerCase() === 'asc') {
    57             // 升序
    58             return result;
    59         } else {
    60             // 降序
    61             var _ = [];
    62             result.forEach(function(item) {
    63                 _.unshift(item);
    64             });
    65             return _;
    66         }
    67     };
    68 })();
    69 
    70                var arr = [{"name":"zhangsan","age":19},{"name":"lisi","age":20},{"name":"wangwu","age":15},{"name":"wangwu","age":14},{"name":"wangwu","age":17}];
    71               console.log(mergeSort(arr,'age'));
    72         </script>
    73     </body>
    74 </html>
  • 相关阅读:
    Understanding identities in IIS
    Name your feature branches by convention
    Branch policies on Azure Repos
    Use Git Credential Managers to Authenticate to Azure Repos
    How do I force my .NET application to run as administrator?
    UML的类型
    ASP.NET Error Handling
    通过泛型,将string转换为指定类型
    Spring Session + Redis实现分布式Session共享
    MongoDB中的数据导出为excel CSV 文件
  • 原文地址:https://www.cnblogs.com/chengyunshen/p/7277228.html
Copyright © 2020-2023  润新知