Merge sort is a recursive sorting algorithm. If you don't understand recursion, I recommend finding a resource to learn it. In brief, recursion is the act of a function calling itself. Thus, merge sort is accomplished by the algorithm calling itself to provide a solution.

Merge sort divides the given array into two halves, a left half and a right half. We call merge sort on these sub-arrays. We continue to split our sub-arrays until we get arrays whose length is less than two. We then begin to stitch our small arrays back together, sorting them on the way up.

This is an efficient algorithm because we start by sorting very small arrays. By the time we reach our larger ones, they are already mostly sorted, saving us the need for expensive loops. To create our algorithm, we'll actually need two functions, our mergeSort function and a merge function that does the combining and sorting of our sub-arrays.

Utilize Javascript LIFO (last in first our queue stack to do the traverse)

For example:

[10, 5, 6, 3, 2, 8, 9, 4, 7, 1]
left [ 10 ] right [ 5 ] reuslts [ 5, 10 ]
left [ 3 ] right [ 2 ] reuslts [ 2, 3 ]
left [ 6 ] right [ 2, 3 ] reuslts [ 2, 3, 6 ]
left [ 5, 10 ] right [ 2, 3, 6 ] reuslts [ 2, 3, 5, 6, 10 ]
 
left [ 8 ] right [ 9 ]reuslts [ 8, 9 ]
left [ 7 ] right [ 1 ] reuslts [ 1, 7 ]
left [ 4 ] right [ 1, 7 ] reuslts [ 1, 4, 7 ]
left [ 8, 9 ] right [ 1, 4, 7 ] reuslts [ 1, 4, 7, 8, 9 ]

left [ 2, 3, 5, 6, 10 ] right [ 1, 4, 7, 8, 9 ] reuslts [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

function mergeSort (array) {
// if array is length less than two items, no need to sort
if ( array.length < 2 ) {
return array;
} // find the middle point of the array to split it into two
const middle = Math.floor(array.length / 2);
const left = array.slice(0, middle);
const right = array.slice(middle); return merge(
mergeSort(left),
mergeSort(right)
)
} function merge(left, right) {
let sorted = [];
console.log('left', left)
console.log('right', right)
while (left.length && right.length) {
if (left[0] < right[0]) {
sorted.push(left.shift())
} else {
sorted.push(right.shift())
}
} const reuslts = [...sorted, ...left, ...right]; console.log('reuslts', reuslts)
return reuslts;
} let numbers = [10, 5, 6, 3, 2, 8, 9, 4, 7, 1] mergeSort(numbers) exports.mergeSort = mergeSort

[Algorithms] Divide and Recurse Over an Array with Merge Sort in JavaScript的更多相关文章

  1. Divide and Conquer.(Merge Sort) by sixleaves

    algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...

  2. Summary: Merge Sort of Array && 求逆序对

    常用算法(后面有inplace版本): package ArrayMergeSort; import java.util.Arrays; public class Solution { public ...

  3. geeksforgeeks@ Sorting Elements of an Array by Frequency (Sort)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=493 Sorting Elements of an Array by Frequ ...

  4. Array类的Sort()方法

    刚复习了Array类的sort()方法, 这里列举几个常用的,和大家一起分享. Array类实现了数组中元素的冒泡排序.Sort()方法要求数组中的元素实现IComparable接口.如System. ...

  5. [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)

    Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...

  6. [Algorithms] Sort an Array with a Nested for Loop using Insertion Sort in JavaScript

    nsertion sort is another sorting algorithm that closely resembles how we might sort items in the phy ...

  7. Javascript判断object还是list/array的类型(包含javascript的数据类型研究)

    前提:先研究javascript中的变量有几种,参考: http://www.w3school.com.cn/js/js_datatypes.asp http://glzaction.iteye.co ...

  8. .NET中string[]数组和List<string>泛型的相互转换以及Array类的Sort()方法(转)

    从string[]转List<string>: " }; List<string> list = new List<string>(str); 从List ...

  9. js中的数组Array定义与sort方法使用示例

    Array的定义及sort方法使用示例 Array数组相当于java中的ArrayList  定义方法:  1:使用new Array(5  )创建数组 var ary = new Array(5): ...

随机推荐

  1. [SCOI2005]互不侵犯 (状压$dp$)

    题目链接 Solution 状压 \(dp\) . \(f[i][j][k]\) 代表前 \(i\) 列中 , 已经安置 \(j\) 位国王,且最后一位状态为 \(k\) . 然后就可以很轻松的转移了 ...

  2. Semaphore built from mutex in C++11

    #include <mutex> #include <condition_variable> using namespace std; class semaphore{ pri ...

  3. clips 前端 js 单选按钮与输入框 的配合变化

    情形1: 一对单选按钮 一个输入框组 输入框组随单选按钮的改变而替换文字或执行其它 片段属于 介绍单选框的基本使用方式  : 1.单选框是有分类的,每个单选框有自己所属的组 从而一个页面可以拥有多组单 ...

  4. Sublime Text3配置SublimeREPL快捷键的方法(Python)

    因为用sublime运行python,如果有input()函数,ctrl+b是不能输入数据的,所以下载安装了sublimeREPL进行调试. 但是sublimeREPL没有自定义快捷键,所以只有自己设 ...

  5. Topcoder SRM548 Div 1

    1. KingdomAndTrees 给出n个数a[1..n],求一个数组b[1..n]满足b严格递增,且b[1]>=1. 定义代价为W = max{abs(a[i]-b[i])},求代价最小值 ...

  6. 装多个版本jdk后,eclipse无法正常启动

    需要在eclipse.ini中plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar后面指定jdk的路径. 指定jdk所在路径 -v ...

  7. InnoDB的多版本并发控制(MMVC)

    InnoDB的MVCC之(乐观锁),是通过在每行记录保存两个隐藏列来实现的.这两个列,一个是存创建时间,一个是删除时间,这里的时间指的是,系统版本号,并不是真正的时间值. 每开始一个新的事务,系统版本 ...

  8. memcached 实现读锁

    memcached锁,网上大多就介绍乐观锁(cas)[1.2.4以上版本,telnet连接上memcache使用status可以查看版本号].核心就是每次写入数据的时候使用 cas($cas_toke ...

  9. for...in与点语法

    语法 for...in语句循环一个指定的变量来循环一个对象所有可枚举的属性.如下所示 for (variable in object){ statements } 问题 在实际的使用过程中发现,在fo ...

  10. UBI 文件系统移植 sys 设备信息【转】

    转自:http://blog.chinaunix.net/uid-25304914-id-3058647.html cat /sys/class/misc/ubi_ctrl/dev --------- ...