zoukankan      html  css  js  c++  java
  • Write a merge sort program

    Merge Sort- Recursion

    Write a merge sort program in JavaScript.

    Sample array : [34, 7, 23, 32, 5, 62]
    Sample output : [5, 7, 23, 32, 34, 62]

    Pictorial Presentation:

    Sample Solution:

    Array.prototype.merge_Sort = function () {
      if (this.length <= 1) 
      {
        return this;
      }
    
      let half = parseInt(this.length / 2);
      let left = this.slice(0, half).merge_Sort();
      let right = this.slice(half, this.length).merge_Sort();
      let merge = function (left, right) {
          let arry = [];
    	  while (left.length > 0 && right.length > 0) {
              arry.push((left[0] <= right[0]) ? left.shift() : right.shift());
          }
          return arry.concat(left).concat(right);
      };
    
      return merge(left, right);
    };
    
    let arr = [34,7,23,32,5,62];
    console.log(arr.merge_Sort());
    

    Flowchart:

  • 相关阅读:
    Celery详解
    JWT详解
    进程及进程池
    多线程详解
    python常用模块之os模块的用法
    python常用模块之paramiko与ssh
    reflect 反射
    http 静态文件
    模板渲染语言
    http web 开发
  • 原文地址:https://www.cnblogs.com/PrimerPlus/p/12920577.html
Copyright © 2011-2022 走看看