Skip to content

Commit 692bcd6

Browse files
committed
chore: merge sort
1 parent 6308091 commit 692bcd6

File tree

2 files changed

+97
-13
lines changed

2 files changed

+97
-13
lines changed

README.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
- [Insert at tail](#insert-at-tail)
1818
- [Reverse a Singly Linked List](#reverse-a-singly-linked-list)
1919
- [Stack](#stack)
20+
- [Popping element from stack](#popping-element-from-stack)
2021
- [Queue](#queue)
22+
- [Dequeue an element from Queue](#dequeue-an-element-from-queue)
23+
- [Enqueue an element in Queue](#enqueue-an-element-in-queue)
2124
- [Tree](#tree)
2225
- [Binary Tree](#binary-tree)
2326
- [Binary Search Tree (BST)](#binary-search-tree-bst)
@@ -119,6 +122,7 @@ Data-structure represents how data will be stored in memory.
119122
Arrays can store a fixed number of elements, whereas a collection stores object dynamically so there is no size restrictions it grows and shrinks automatically.
120123

121124
- **Insert** at the end (push) is efficient.
125+
- **Insert** at in between is not efficient.
122126

123127
#### Implement Binary Search on a Sorted Array
124128

@@ -206,10 +210,40 @@ Running program output
206210

207211
Depth First Search (DFS) uses a `stack` for storing the nodes that it is visiting.
208212

213+
#### Popping element from stack
214+
215+
```ts
216+
var stack = [1, 2, 3, 4];
217+
stack.pop(); // 4 , stack [1,2,3]
218+
stack.pop(); // 3 , stack [1,2]
219+
stack.pop(); // 2 , stack [1]
220+
stack.pop(); // 1 , stack []
221+
```
222+
209223
### Queue
210224

211225
Breadth First Search (BFS) uses a `queue` for storing the nodes that it is visiting.
212226

227+
#### Dequeue an element from Queue
228+
229+
```ts
230+
var queue = [1, 2, 3, 4];
231+
queue.shift(); // 1 , queue [2,3,4]
232+
queue.shift(); // 2 , queue [3,4]
233+
queue.shift(); // 3 , queue [4]
234+
queue.shift(); // 4 , queue []
235+
```
236+
237+
#### Enqueue an element in Queue
238+
239+
```ts
240+
var queue = [];
241+
242+
queue.unshift(1); // queue [1]
243+
queue.unshift(2); // queue [2,1]
244+
queue.unshift(3); // queue [3,2,1]
245+
```
246+
213247
### Tree
214248

215249
A tree has hierarchical data and it has nodes.
@@ -308,20 +342,22 @@ Example: Suppose you have given a tree structure and asked to calculate the aver
308342
| Time complexity: Fast | Time complexity: Slow |
309343
| Where to use: if you can find at root or leaf, find connected components. | Where to use: Find shortest path,find connected components. When you think you have less data go for it. |
310344

311-
312-
## Sorting
345+
## Sorting
313346

314347
### Merge Sort
315348

316349
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.
317350

351+
[Exercise File](src/sorting/merge-sort.mjs)
352+
353+
## Math.floor
318354

319-
## Math.floor
320355
2.5 = 2
321356
2.8 = 2
322357
2.4 = 2
323358

324359
## Math.round
360+
325361
2.5 = 3
326362
2.8 = 3
327-
2.4 = 2
363+
2.4 = 2

src/sorting/merge-sort.mjs

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,63 @@
11
/**
2-
* merge sort
3-
* Runtime complexity
4-
* Space / Memory complexity
2+
*
3+
* [34,3,99,43,23]
4+
*
5+
*/
6+
/**
7+
* merge sort
8+
* Runtime complexity: O(n logn)
9+
* Space / Memory complexity: O(n)
510
* @param nums array of numbers
611
* @returns array of numbers
712
*/
8-
export function mergeSort(nums) {
9-
if (nums.length < 2) {
10-
return nums;
13+
export function mergeSort(nums = []) {
14+
if (nums.length < 2) {
15+
return nums;
16+
}
17+
18+
const length = nums.length;
19+
const middle = Math.floor(length / 2);
20+
const left = nums.slice(0, middle);
21+
const right = nums.slice(middle, length);
22+
23+
return stitch(mergeSort(left), mergeSort(right));
24+
}
25+
26+
function stitch(left, right) {
27+
const results = [];
28+
29+
while (left.length && right.length) {
30+
if (left[0] <= right[0]) {
31+
results.push(left.shift());
32+
} else {
33+
results.push(right.shift());
1134
}
35+
}
36+
37+
return results.concat(left, right);
38+
}
39+
40+
// Test
41+
console.log(
42+
'given:[' + [34, 3, 99, 43, 23] + ']',
43+
'result:[' + mergeSort([34, 3, 99, 43, 23]) + ']'
44+
);
45+
console.log(
46+
'given:[' + [10, 5, 3, 8, 2, 6, 4, 7, 9, 1] + ']',
47+
'result:[' + mergeSort([10, 5, 3, 8, 2, 6, 4, 7, 9, 1]) + ']'
48+
);
49+
/*
50+
L = [34, 3, 99]
51+
L = [34, 3]
52+
L=[34]
53+
R=[3]
54+
result =[3,34]
55+
R = [99]
56+
results = [3,34,99]
57+
R = [43,23];
58+
L = [43]
59+
R = [23]
60+
result [23,43]
61+
results =[3, 23,34,43,99]
1262
13-
const length = nums.length;
14-
const middle = Math.floor
15-
}
63+
*/

0 commit comments

Comments
 (0)