Skip to content

Commit 2aa36c4

Browse files
committed
add merge_sort.js, fix README layout
1 parent 5413663 commit 2aa36c4

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@
9898
||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***|
9999
||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***|
100100
||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)|
101-
||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|
101+
||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|
102102
||Bucket sort| [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/bucket_sort.py) | | |AGAIN|
103103
||Quick sort| [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/quick_sort.py)| | | AGAIN***|
104104
||Heap sort| [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/heap_sort.py)| | | AGAIN**|
105-
||Merge sort|[Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/merge_sort.py) | | | OK* (2)|
105+
||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)|
106106
||Pancake sort| [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/pancake_sort.py) | | | AGAIN|
107-
||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|
107+
||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|
108108
||Topological sort| [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/topological_sort.py) | | | AGAIN|
109109
||md5 | [Python](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/md5.py) | | | AGAIN|
110110
||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|

algorithm/js/merge_sort.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//https://repl.it/@aneagoie/mergeSort
2+
3+
function mergeSort (array) {
4+
if (array.length === 1) {
5+
return array
6+
}
7+
// Split Array in into right and left
8+
const length = array.length;
9+
const middle = Math.floor(length / 2)
10+
const left = array.slice(0, middle)
11+
const right = array.slice(middle)
12+
// console.log('left:', left);
13+
// console.log('right:', right);
14+
15+
16+
return merge(
17+
mergeSort(left),
18+
mergeSort(right)
19+
)
20+
}
21+
22+
function merge(left, right){
23+
const result = [];
24+
let leftIndex = 0;
25+
let rightIndex = 0;
26+
while(leftIndex < left.length &&
27+
rightIndex < right.length){
28+
if(left[leftIndex] < right[rightIndex]){
29+
result.push(left[leftIndex]);
30+
leftIndex++;
31+
} else{
32+
result.push(right[rightIndex]);
33+
rightIndex++
34+
}
35+
}
36+
// console.log(left, right)
37+
return result.concat(left.slice(leftIndex)).concat(right.slice(rightIndex));
38+
}
39+
40+
const numbers = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];
41+
const answer = mergeSort(numbers);
42+
console.log(answer);

0 commit comments

Comments
 (0)