Skip to content

Commit ede8c4b

Browse files
Add merge sort code
1 parent bc384b0 commit ede8c4b

File tree

28 files changed

+1970
-250
lines changed

28 files changed

+1970
-250
lines changed

.DS_Store

0 Bytes
Binary file not shown.

Algorithms/Sorting/mergeSort.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,33 @@
33
// Time Complexity - O(n log(n))
44
// Space Complexity - O(n)
55

6+
function merge(left, right) {
7+
let arr = []
8+
// Break out of loop if any one of the array gets empty
9+
while (left.length && right.length) {
10+
// Pick the smaller among the smallest element of left and right sub arrays
11+
if (left[0] < right[0]) {
12+
arr.push(left.shift())
13+
} else {
14+
arr.push(right.shift())
15+
}
16+
}
17+
18+
// Concatenating the leftover elements
19+
// (in case we didn't go through the entire left or right array)
20+
return [ ...arr, ...left, ...right ]
21+
}
622

7-
const merge = (arr1, arr2) => {
23+
const mergeSort = (array) => {
24+
const half = array.length / 2
825

26+
// Base case or terminating case
27+
if(array.length < 2){
28+
return array
29+
}
30+
31+
const left = array.splice(0, half);
32+
return merge(mergeSort(left),mergeSort(array))
933
};
1034

11-
console.log(merge([2, 5, 3, 0, 57], [9, 12, 13]));
35+
console.log(mergeSort([2, 5, 3, 0, 57, 9, 12, 13]));

Algorithms/Sorting/practice.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Practice Merge Sort
2+
3+
const mergeSort = (arr) => {
4+
let half = arr.length / 2;
5+
if(arr.length < 2) {
6+
return arr;
7+
}
8+
let left = arr.splice(0, half);
9+
return merge(mergeSort(left), mergeSort(arr));
10+
}
11+
12+
const merge = (left, right) => {
13+
let arr = [];
14+
while(left.length && right.length) {
15+
if(left[0] < right[0]) {
16+
arr.push(left.shift());
17+
} else {
18+
arr.push(right.shift())
19+
}
20+
}
21+
return [...arr, ...left, ...right]
22+
}
23+
24+
console.log(mergeSort([2, 5, 3, 0, 57, 9, 12, 13]));

Datastructure/BinaryTree/practice.js

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,33 @@ class Node {
3232
}
3333
}
3434
class BinarySearchTree {
35-
3635
constructor(value) {
37-
this.root = new Node(value);
38-
this.length = 1;
36+
this.root = new Node(value);
37+
this.length = 1;
3938
}
40-
4139
insert(value) {
42-
let newNode = new Node(value);
43-
44-
let traverse = node => {
40+
let currentNode = new Node(value);
41+
this.length++;
42+
let searchTree = node => {
4543
if(value < node.value) {
4644
if(!node.left) {
47-
node.left = newNode;
45+
node.left = currentNode;
4846
} else {
49-
traverse(node.left);
47+
searchTree(node.left)
5048
}
5149
} else if(value > node.value) {
5250
if(!node.right) {
53-
node.right = newNode;
51+
node.right = currentNode;
5452
} else {
55-
traverse(node.right);
53+
searchTree(node.right)
5654
}
5755
}
5856
}
57+
searchTree(this.root);
58+
}
5959

60-
traverse(this.root);
60+
size() {
61+
return this.length;
6162
}
6263

6364
min() {
@@ -79,19 +80,15 @@ class BinarySearchTree {
7980
lookup(value) {
8081
let currentNode = this.root;
8182
while(currentNode) {
82-
if(value === currentNode.value) {
83+
if(currentNode.value == value) {
8384
return true;
8485
} else if(value < currentNode.value) {
8586
currentNode = currentNode.left;
8687
} else {
87-
currentNode = currentNode.right;
88+
currentNode = currentNode.right
8889
}
8990
}
90-
return false;
91-
}
92-
93-
size() {
94-
return this.length;
91+
return false
9592
}
9693

9794
dfsInorder() {
@@ -107,9 +104,9 @@ class BinarySearchTree {
107104
// Output = 2,3,4,5,6,10,17,19,21
108105
let result = [];
109106
let searchTree = node => {
110-
if(node.left) searchTree(node.left);
111-
result.push(node.value);
112-
if(node.right) searchTree(node.right);
107+
if(node.left) searchTree(node.left)
108+
result.push(node.value)
109+
if(node.right) searchTree(node.right)
113110
}
114111
searchTree(this.root);
115112
return result;
@@ -125,9 +122,9 @@ class BinarySearchTree {
125122
// Output = 10,5,1,6,19,17,21
126123
let result = [];
127124
let searchTree = node => {
128-
result.push(node.value);
129-
if(node.left) searchTree(node.left);
130-
if(node.right) searchTree(node.right);
125+
result.push(node.value)
126+
if(node.right) searchTree(node.right)
127+
if(node.left) searchTree(node.left)
131128
}
132129
searchTree(this.root);
133130
return result;
@@ -143,9 +140,9 @@ class BinarySearchTree {
143140
// Output = 1,6,5,17,21,19,10
144141
let result = [];
145142
let searchTree = node => {
146-
if(node.left) searchTree(node.left);
147-
if(node.right) searchTree(node.right);
148-
result.push(node.value);
143+
if(node.right) searchTree(node.right)
144+
if(node.left) searchTree(node.left)
145+
result.push(node.value)
149146
}
150147
searchTree(this.root);
151148
return result;
@@ -158,19 +155,16 @@ class BinarySearchTree {
158155
// 1 6 17 21
159156

160157
// Output - 10,5,19,1,6,17,21
161-
let result = [];
162158
let queue = [];
159+
let result = [];
163160
queue.push(this.root);
164161
while(queue.length) {
165162
let currentNode = queue.shift();
166163
result.push(currentNode.value);
167-
168164
if(currentNode.left) {
169-
queue.push(currentNode.left);
170-
}
171-
172-
if(currentNode.right) {
173-
queue.push(currentNode.right);
165+
queue.push(currentNode.left)
166+
} if(currentNode.right) {
167+
queue.push(currentNode.right)
174168
}
175169
}
176170
return result;
@@ -239,6 +233,7 @@ function getLeafCountOfBinaryTree(node) {
239233
if(node.left == null && node.right == null) return 1;
240234
return getLeafCountOfBinaryTree(node.left) + getLeafCountOfBinaryTree(node.right);
241235
}
236+
242237
console.log("COUNT LEAF NODE: ", getLeafCountOfBinaryTree(bst.root));
243238

244239
function hightOfBinaryTree(node) {
@@ -247,7 +242,7 @@ function hightOfBinaryTree(node) {
247242
} else {
248243
let lTree = hightOfBinaryTree(node.left);
249244
let rTree = hightOfBinaryTree(node.right);
250-
if(lTree > rTree) {
245+
if(lTree < rTree) {
251246
return lTree + 1;
252247
} else {
253248
return rTree + 1;

Datastructure/Graphs/practice.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
class UndirectedGraph {
22
constructor() {
3-
this.noOfEdges = 0;
4-
this.adjacentList = {};
3+
this.numberOfNodes = 0;
4+
this.adjecentList = {};
55
}
6-
76
addVertices(node) {
8-
this.adjacentList[node] = [];
7+
this.adjecentList[node] = [];
8+
this.numberOfNodes++;
99
}
10-
1110
addEdges(node1, node2) {
12-
this.adjacentList[node1].push(node2);
13-
this.adjacentList[node2].push(node1);
11+
this.adjecentList[node1].push(node2);
12+
this.adjecentList[node2].push(node1);
1413
}
15-
1614
showConnections() {
17-
return console.log(this.adjacentList);
15+
return this.adjecentList;
1816
}
1917
}
2018
const undirectedGrph = new UndirectedGraph();
@@ -36,4 +34,4 @@ undirectedGrph.addEdges('1', '0');
3634
undirectedGrph.addEdges('0', '2');
3735
undirectedGrph.addEdges('6', '5');
3836

39-
undirectedGrph.showConnections();
37+
console.log(undirectedGrph.showConnections());

Datastructure/LinkedList/practice.js

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
class Node {
3636
constructor(value) {
3737
this.value = value;
38-
this.next = null;
3938
this.prev = null;
39+
this.next = null;
4040
}
4141
}
4242
class SinglyLinkedList {
@@ -45,44 +45,33 @@ class SinglyLinkedList {
4545
this.tail = this.head;
4646
this.length = 1;
4747
}
48-
append(value) {
49-
let newNode = new Node(value);
50-
newNode.next = this.head;
51-
this.head.prev = newNode; // Doubly LinkedList
52-
this.head = newNode;
53-
this.length++;
54-
return this;
55-
}
5648
prepand(value) {
5749
let newNode = new Node(value);
5850
this.tail.next = newNode;
59-
newNode.prev = this.tail; // Doubly LinkedList
51+
newNode.prev = this.tail.prev; //here
6052
this.tail = newNode;
6153
this.length++;
6254
return this;
6355
}
64-
insert(value, index) {
65-
if(index >= this.length) {
66-
return this.append(value);
67-
}
56+
append(value) {
6857
let newNode = new Node(value);
69-
let leaderNode = this.traverse(index - 1);
70-
let nextNode = leaderNode.next;
71-
leaderNode.next = newNode;
72-
newNode.prev = leaderNode; // Doubly LinkedList
73-
newNode.next = nextNode;
74-
nextNode.prev = newNode; // Doubly LinkedList
58+
newNode.next = this.head;
59+
this.head.prev = newNode; // here
60+
this.head = newNode;
7561
this.length++;
76-
return this.printList();
62+
return this;
7763
}
78-
remove(index) {
79-
if(index > this.length) {
80-
return "Invalid Input !!!"
64+
insert(index, value) {
65+
if(index >= this.length) {
66+
return this.prepand(index);
8167
}
68+
let newNode = new Node(value);
8269
let leaderNode = this.traverse(index - 1);
8370
let unwantedNode = leaderNode.next;
84-
leaderNode.next = unwantedNode.next;
85-
this.length--;
71+
leaderNode.next = newNode;
72+
newNode.prev = leaderNode; // here
73+
newNode.next = unwantedNode;
74+
unwantedNode.prev = newNode; //here
8675
return this.printList();
8776
}
8877
reverse() {
@@ -97,21 +86,29 @@ class SinglyLinkedList {
9786
}
9887
this.head.next = null;
9988
this.head = first;
89+
}
90+
remove(index) {
91+
if(index > this.length) {
92+
return "Invaild Input !!!"
93+
}
94+
let leader = this.traverse(index - 1);
95+
let unwantedNode = leader.next;
96+
leader.next = unwantedNode.next;
10097
return this.printList();
10198
}
10299
traverse(index) {
103-
let count = 0;
100+
let counter = 0;
104101
let currentNode = this.head;
105-
while(index != count) {
106-
count++;
102+
while(index != counter) {
103+
counter++;
107104
currentNode = currentNode.next;
108105
}
109106
return currentNode;
110107
}
111108
printList() {
112-
let result = [];
113109
let currentNode = this.head;
114-
while(currentNode != null) {
110+
let result = [];
111+
while(currentNode !== null) {
115112
result.push(currentNode.value);
116113
currentNode = currentNode.next;
117114
}

Interview-LeetCode/index.js

Whitespace-only changes.

0 commit comments

Comments
 (0)