Skip to content

Commit 94db6ee

Browse files
committed
CHORE: FIX MERGE SORT
1 parent ebb6493 commit 94db6ee

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ Example:
147147

148148
Learn Big O. Make sure you give what would be the `runtime complexity` and `memory complexity`.
149149

150+
151+
152+
- Array.push() is O(1) constant time complexity
153+
- string.indexOf() search is O(n) linear time complexity
154+
- for loop O(n) linear time complexity
155+
156+
150157
### Big O Space complexity
151158

152159
`Iterative functions` take no extra memory and therefore, `memory complexity` is `constant` O(1).
@@ -425,6 +432,10 @@ Below image showing how to add `[3, 7, 4, 6, 5, 1, 10, 2, 9, 8]` in BST.
425432
- [Binary Search Tree Implementation Question](https://codepen.io/roopkt/pen/RwpJBOw?editors=0010)
426433
- [Binary Search Tree Implementation Answer](https://codepen.io/roopkt/pen/LYWBYMM?editors=0010)
427434

435+
**Binary Search Algorithm**
436+
437+
![](https://i.imgur.com/WYh0bug.png)
438+
428439
Binary Search can be done both in iterative or recursive way.
429440

430441
![](https://i.imgur.com/PWjbCaZ.png)
@@ -516,12 +527,22 @@ Find all possible routes from `PHX` to `BKK`. Also then you can decide which pat
516527

517528
Browser's JavaScript Engine (`Array.prototype.sort`) uses merge sort maximum time. Runtime complexity O(n logn), Memory complexity O(n) because we have to create new list. It uses divide-and-conquer algorithm! and also it is recursive.
518529

530+
519531
https://www.youtube.com/watch?v=UxnyImctVzg
520532

521533
![ ](https://i.imgur.com/YpQSB5J.png)
522534

523535
**Merge Sort Algorithm**
524536

537+
538+
![](https://i.imgur.com/5a8joFC.png)
539+
540+
541+
![](https://i.imgur.com/yGrnQrB.png)
542+
543+
544+
![](https://i.imgur.com/uu5aDiG.png)
545+
525546
![Merge Sort Algorithm](https://i.imgur.com/DIvX2OX.png)
526547

527548
**Merge Sort Algorithm Simulation**
@@ -959,7 +980,7 @@ Try `Binary Search`.
959980

960981
#### Reverse linked list
961982

962-
![](https://i.imgur.com/eNRiftX.png)
983+
![](https://i.imgur.com/SEXPIuq.png)
963984

964985
<p class="codepen" data-height="300" data-theme-id="dark" data-default-tab="js,result" data-slug-hash="dyvwPej" data-user="roopkt" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;">
965986
<span>See the Pen <a href="https://codepen.io/roopkt/pen/dyvwPej">
@@ -989,3 +1010,4 @@ Try `Binary Search`.
9891010

9901011
<script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>
9911012
<script data-ad-client="ca-pub-1700383344966810" async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" data-checked-head="true"></script>
1013+

src/data-structure/1-arrays/binary-search.mjs

+6
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,9 @@ log(iterativeBinarySearch([10, 20, 20, 20, 40, 50], 40), 4);
122122
for (var i = 0; i < inputs.length; i++) {
123123
log(iterativeBinarySearch(arr, inputs[i][0]), inputs[i][1]);
124124
}
125+
126+
127+
128+
129+
130+
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// O(length log(length)) time | O(1) space
2+
function mergeSort(array) {
3+
const length = array.length;
4+
if (length < 2) {
5+
return;
6+
}
7+
const mid = length / 2;
8+
const left = array.slice(0, mid);
9+
const right = array.slice(mid, length);
10+
mergeSort(left);
11+
mergeSort(right);
12+
stitch(left, right, array);
13+
}
14+
15+
function stitch(left, right, array) {
16+
let leftIdx = 0;
17+
let rightIdx = 0;
18+
let arrayIdx = 0;
19+
let leftLength = left.length;
20+
let rightLength = right.length;
21+
22+
while (leftIdx < leftLength && rightIdx < rightLength) {
23+
if (left[leftIdx] <= right[rightIdx]) {
24+
array[arrayIdx] = left[leftIdx];
25+
leftIdx++;
26+
} else {
27+
array[arrayIdx] = right[rightIdx];
28+
rightIdx++;
29+
}
30+
arrayIdx++;
31+
}
32+
33+
while (leftIdx < leftLength) {
34+
array[arrayIdx] = left[leftIdx];
35+
leftIdx++;
36+
arrayIdx++;
37+
}
38+
while (rightIdx < rightLength) {
39+
array[arrayIdx] = right[rightIdx];
40+
rightIdx++;
41+
arrayIdx++;
42+
}
43+
}

0 commit comments

Comments
 (0)