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. 优化web前端性能的几个方法

    1 减少http请求, a. 合并脚本跟样式文件,如可以把多个 CSS 文件合成一个,把多个 JS 文件合成一个. b. CSS Sprites 利用 CSS background 相关元素进行背景图 ...

  2. Cocoa Pods 'No such file or Directory' Error

    http://stackoverflow.com/questions/27727998/cocoa-pods-no-such-file-or-directory-error 0down votefav ...

  3. Golang/Go语言/Go IDE/Go windows环境搭建/Go自动提示编译器/GoSublime

    Go是Google开发的一种编译型,并发型,并具有垃圾回收功能的编程语言. 罗伯特·格瑞史莫(Robert Griesemer),罗勃·派克(Rob Pike)及肯·汤普逊于2007年9月开始设计Go ...

  4. zlib、libzip、 libzippp 库编译(windows + cmake + vs2013)

    "libzipp" 这库是基于 "libzip" 之上封装的,而 "libzip" 又是基于 "zlib"库封装的,所以 ...

  5. 第二步:开发工具Eclipse安装并汉化

    打开下载官网:www.eclipse.org.点击下载(download英文)然后就是安装步骤了,还是一样一直的点击下一步,默认安装到C盘.如下图: 汉化步骤: 1.打开www.eclipse.org ...

  6. 正则表达式之Regex.Replace()用法

    正则表达式替换匹配到的字符串 string txt = "AAA12345678AAAA"; //匹配到的连续数字的前4位用*替换 string m =Regex.Replace( ...

  7. Delphi中获取文件大小

    大概有这些方法可以获得文件大小FileSizeByName(需要引用IdGlobal单元)GetFileSizeFileSize(不能获得正在使用的文件大小)FileSeekTFileStream.S ...

  8. aliyun

    阿里云启动不了网站 1  将网站的目录属性-安全中加入IUSER_计算机名字的访问权限     和  加入NER SERVICE的访问权限 2 IIS打开网站属性--目录--执行权限改为顺脚本 3  ...

  9. Codeforces 920 E Connected Components?

    Discription You are given an undirected graph consisting of n vertices and  edges. Instead of giving ...

  10. SPOJ705 SUBST1 - New Distinct Substrings(后缀数组)

    给一个字符串求有多少个不相同子串. 每一个子串一定都是某一个后缀的前缀.由此可以推断出总共有(1+n)*n/2个子串,那么下面的任务就是找这些子串中重复的子串. 在后缀数组中后缀都是排完序的,从sa[ ...