Skip to content

Commit

Permalink
add merge_sort.js, fix README layout
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Jan 31, 2020
1 parent 5413663 commit 2aa36c4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@
||Breadth-first search (BFS)| [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/bfs.py), [JS](https://github.com/yennanliu/CS_basics/blob/master/algorithm/js/bfs.js)|`FIND SHORTEST PATH`|| AGAIN***|
||Depth-first search (DFS) |[Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/dfs.py), [JS](https://github.com/yennanliu/CS_basics/blob/master/algorithm/js/dfs.js)| `TO CHECK IF SOMETHING EXIST`| `inorder`, `postorder`, `postorder (can recreate a tree)`| AGAIN***|
||Bubble sort| [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/bubble_sort.py), [JS](https://github.com/yennanliu/CS_basics/blob/master/algorithm/js/bubble_sort.js) , [C](https://github.com/yennanliu/CS_basics/blob/master/algorithm/c/bubble_sort.c)| | | OK* (3)|
||Insertion sort| [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/insertion_sort.py), [JS](https://github.com/yennanliu/CS_basics/blob/master/algorithm/js/insertion_sort.js) | | |AGAIN|
||Insertion sort| [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/insertion_sort.py), [JS](https://github.com/yennanliu/CS_basics/blob/master/algorithm/js/insertion_sort.js) || work very fast for `nearly sorted` array|AGAIN|
||Bucket sort| [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/bucket_sort.py) | | |AGAIN|
||Quick sort| [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/quick_sort.py)| | | AGAIN***|
||Heap sort| [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/heap_sort.py)| | | AGAIN**|
||Merge sort|[Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/merge_sort.py) | | | OK* (2)|
||Merge sort|[Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/merge_sort.py), [JS](https://github.com/yennanliu/CS_basics/blob/master/algorithm/js/merge_sort.js) | | | OK* (2)|
||Pancake sort| [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/pancake_sort.py) | | | AGAIN|
||Selection sort| [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/selection_sort.py), [js](https://github.com/yennanliu/CS_basics/blob/master/algorithm/js/selection_sort.js) | | | AGAIN|
||Selection sort| [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/selection_sort.py), [JS](https://github.com/yennanliu/CS_basics/blob/master/algorithm/js/selection_sort.js) | | | AGAIN|
||Topological sort| [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/topological_sort.py) | | | AGAIN|
||md5 | [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/md5.py) | | | AGAIN|
||Union Find | [Python union find](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/union_find.py), [Python union find check cyclic](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/union_find_if_cyclic.py) | | | AGAIN|
Expand Down
42 changes: 42 additions & 0 deletions algorithm/js/merge_sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//https://repl.it/@aneagoie/mergeSort

function mergeSort (array) {
if (array.length === 1) {
return array
}
// Split Array in into right and left
const length = array.length;
const middle = Math.floor(length / 2)
const left = array.slice(0, middle)
const right = array.slice(middle)
// console.log('left:', left);
// console.log('right:', right);


return merge(
mergeSort(left),
mergeSort(right)
)
}

function merge(left, right){
const result = [];
let leftIndex = 0;
let rightIndex = 0;
while(leftIndex < left.length &&
rightIndex < right.length){
if(left[leftIndex] < right[rightIndex]){
result.push(left[leftIndex]);
leftIndex++;
} else{
result.push(right[rightIndex]);
rightIndex++
}
}
// console.log(left, right)
return result.concat(left.slice(leftIndex)).concat(right.slice(rightIndex));
}

const numbers = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];
const answer = mergeSort(numbers);
console.log(answer);

0 comments on commit 2aa36c4

Please sign in to comment.