nie-ii-year

lab stuff from undergrad second year.
git clone http://git.hanabi.in/repos/nie-ii-year.git
Log | Files | Refs | LICENSE

commit 807fdf5c0d55f7206c4a7b780ef747d664fc5d71
parent eee8e61572eff1a02e4eb07ef16fd4d2bcbb91c8
Author: Agastya Chandrakant <acagastya@outlook.com>
Date:   Wed,  7 Feb 2018 09:13:21 +0530

Create recursive.js
Diffstat:
As4/algorithms/mergesort/recursive.js | 18++++++++++++++++++
1 file changed, 18 insertions(+), 0 deletions(-)

diff --git a/s4/algorithms/mergesort/recursive.js b/s4/algorithms/mergesort/recursive.js @@ -0,0 +1,18 @@ +function mergeSort(array) { + if(array.length < 2) + return array; + var middle = array.length >> 1; + var left = array.slice(0, middle); + var right = array.slice(middle); + + return merge(mergeSort(left), mergeSort(right)); +} + +function merge(left, right) { + var result = []; + while (left.length > 0 && right.length > 0) + result.push(left[0] < right[0]? left.shift() : right.shift()); + return result.concat(left.length? left : right); +} +// remember the function call should be SortedArr = mergeSort(arr); +// mergeSort(arr) would do no change to arr