-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmerge.js
337 lines (300 loc) · 13 KB
/
merge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* Merge Sort (Jon von Neumann)
* ➤ Divide and Conquer
* ➤ Recursive
*
* Steps:
* → Divide an array into 2 halves.
* → Recursively sort 2 halves.
* → Merge 2 halves.
*
* How to find the mid of the array?
* ➤ mid = low + (high - low) / 2
*
* SORTING HAPPENS IN THE MERGE OPERATION ONLY!
* ➤ Since it's a Divide and Conquer method(Recursive), we break down our array into sub arrays till
* each subarray is has length equal to 1. (N single item sub-arrays)
* Consider this array: [E, A, M, A, R, X, C, K, O, L]
* After we are done with 'sub-arraying' this array, we get:
* [E] [A] [M] [A] [R] [X] [C] [K] [O] [L]
*
* We take 1st two element -> Sort and Merge.
* If you read 'HOW MERGE OPERATION WORKS', you see we have 3 conditions. That's were you sort them. And,
* when you put them in the arr (See, arr[k] = aux[i or j]) => That's where you merge them.
*
* Let's see this in more detail now :-
*
* So, How MERGE OPERATION works?
* Consider this array: [E, E, G, M, R, A, C, E, R, T]
* → Break it in 2 part by finding the 'mid' of the array. (mid = low + (high - low) / 2)
* → Initialize an auxilliary array (aux) and copy the content of 'arr' into 'aux'
* → Let consider these 2 sub arrays which we got after finding the 'mid' to be sorted sub-arrays.
* In this case if you notice, two sub-arrays are sorted.
*
* Objective: Merge this 2 sorted sub-arrays.
* arr = [E, E, G, M, R, A, C, E, R, T]
* k ==> pointer 'k' which operates on our original array and mutate it
* based on the conditions described below
* aux = [E, E, G, M, R, A, C, E, R, T]
* i 🔺j ==> 2 pointers i & j. Both starts from the 1st element of two sub-arrays.
* I've used 🔺 to show the separation of sub-arrays.
*
* Conditions:
* 1. aux[i] > aux[j] ➤ arr[k] = aux[j] and also increment 'k' and 'j'
* 2. aux[i] < aux[j] ➤ arr[k] = aux[i] and also increment 'k' and 'i'
* 3. aux[i] === aux[j] ➤ arr[k] = aux[i] and also increment 'k' and 'i'
*
* Let's start:
* → compare aux[i] and aux[j]
* ➤ aux[i], which is E, is GREATER than aux[j] which is A. This satisfies our Condition 1.
* Resulting arrays:
* arr = [A, E, G, M, R, A, C, E, R, T]
* k => arr[k] = aux[j] & 'k' is incremented by 1
* aux = [E, E, G, M, R, A, C, E, R, T]
* i 🔺 j => 'i' remains the same, 'j' is incremented
*
* ➤ aux[i], which is E, is GREATER than aux[j] which is C. This satisfies our Condition 1.
* Resulting arrays:
* arr = [A, C, G, M, R, A, C, E, R, T]
* k => arr[k] = aux[j] & 'k' is incremented by 1.
* aux = [E, E, G, M, R, A, C, E, R, T]
* i 🔺 j => 'i' remains the same, 'j' is incremented.
*
* ➤ aux[i], which is E, is EQUAL to aux[j] which is also E. This satisfies our Condition 3.
* Resulting arrays:
* arr = [A, C, E, M, R, A, C, E, R, T]
* k => arr[k] = aux[i] & 'k' is incremented by 1
* aux = [E, E, G, M, R, A, C, E, R, T]
* i 🔺 j => 'i' is incremented, 'j' remains the same.
*
* ➤ Again, aux[i], which is E, is EQUAL to aux[j] which is also E. This satisfies our Condition 3.
* Resulting arrays:
* arr = [A, C, E, E, R, A, C, E, R, T]
* k => arr[k] = aux[i] & 'k' is incremented by 1
* aux = [E, E, G, M, R, A, C, E, R, T]
* i 🔺 j => 'i' is incremented, 'j' remains the same.
*
* ➤ aux[i], which is G, is GREATER than aux[j] which is E. This satisfies our Condition 1.
* Resulting arrays:
* arr = [A, C, E, E, E, A, C, E, R, T]
* k => arr[k] = aux[j] & 'k' is incremented by 1.
* aux = [E, E, G, M, R, A, C, E, R, T]
* i 🔺 j => 'i' remains the same, 'j' is incremented.
*
* ➤ aux[i], which is G, is SMALLER than aux[j] which is R. This satisfies our Condition 2.
* Resulting arrays:
* arr = [A, C, E, E, E, G, C, E, R, T]
* k => arr[k] = aux[i] & 'k' is incremented by 1.
* aux = [E, E, G, M, R, A, C, E, R, T]
* i 🔺 j => 'i' is incremented, 'j' remains the same.
*
* ➤ aux[i], which is M, is SMALLER than aux[j] which is R. This satisfies our Condition 2.
* Resulting arrays:
* arr = [A, C, E, E, E, G, M, E, R, T]
* k => arr[k] = aux[i] & 'k' is incremented by 1.
* aux = [E, E, G, M, R, A, C, E, R, T]
* i🔺 j => 'i' is incremented, 'j' remains the same.
*
* ➤ aux[i], which is R, is EQUAL to aux[j] which is R. This satisfies our Condition 3.
* Resulting arrays:
* arr = [A, C, E, E, E, G, M, R, R, T]
* k => arr[k] = aux[i] & 'k' is incremented by 1.
* aux = [E, E, G, M, R, A, C, E, R, T]
* 🔺 i j => 'i' is incremented, 'j' remains the same.
*
* Since our 1st sub array is exhausted, we can simply merge the remaining part of the 2nd subarray
* to our MUTATING ORIGINAL ARRAY(arr)
*
* How can we simple merge the remaining part without comparing? Because 2 sub arrays were already sorted
* before we started merging two sub arrays.
*
* Since one of our array is exhausted, we don't need to check/compare anything.
*
* Do we really need to merge the remaining part? No! Because we copied all the content before we started
* merging, so it's already there if you notice. In this case, R & T are already present in 'arr' because of
* the copy OPERATION we performed in the beginning. 😅
*
* arr = [A, C, E, E, E, G, M, R, R, T] is our SORTED ARRAY after MERGE OPERATION.
*
*
* --------------- T H A T 'S H O W M E R G E O P E R A T I O N W O R K S -----------------
*
* Complexity: N Log N
* Stability: Stable. Why?
* ➤ Unlike Quick Sort, there are no long exchanges.
* ➤ If two items are equal we choose from 1st array thereby preserving the order in original array.
*
* Improvements:
* ➤ Use insertion sort for small arrays.
* ➤ Don't perform MERGE OPERATION if last item of the 1st sub array is smaller than or equal to the first item of the 2nd subarray.
*
* Problem: Extra space for auxilliary array, N.
*
*/
// METHOD 1
class Merge {
constructor (arr) {
this.arr = arr
this.aux = arr.slice()
}
merge (low, mid, high) {
let arr = this.arr
let aux = this.aux
// copy all content of arr to aux
for (let k = low; k <= high; k++) {
aux[k] = arr[k]
}
// merge operation
let i = low
let j = mid + 1
for (let k = low; k <= high; k++) {
if (i > mid) { // check if our 1st sub array is exhausted
arr[k] = aux[j++]
} else if (j > high) { // check if our 2nd sub array is exhausted
arr[k] = aux[i++]
} else if (aux[i] > aux[j]) {
arr[k] = aux[j++]
} else {
arr[k] = aux[i++]
}
}
}
sort (low, high) {
let arr = this.arr
if (high <= low) return
let mid = Math.floor(low + (high - low)/2)
this.sort(low, mid)
this.sort(mid+1, high)
this.merge(low, mid, high)
}
}
const arr = [21, 92, 4, 22, 1, 67, 99, 102, 76, 43, 11]
const low = 0
const high = arr.length - 1
const merge = new Merge(arr)
merge.sort(low, high)
/**
STACK TRACE OF MERGE SORT FOR ABOVE GIVEN ARRAY:
Since we know that MERGE OPERATION takes place on TWO SORTED SUB-ARRAYS,
I have used these symbols to denote:
🔺 : left sorted subarray
▲ : right sorted subarray
LOW -> : 0
MID : -> 0
HIGH : -> 1
indexes: 0 1 2 3 4 5 6 7 8 9 10
AUX : (11) [21, 92, 4, 22, 1, 67, 99, 102, 76, 43, 11]
🔺 ▲
arr : (11) [21, 92, 4, 22, 1, 67, 99, 102, 76, 43, 11]
----------------------------
LOW -> : 0
MID : -> 1
HIGH : -> 2
indexes: 0 1 2 3 4 5 6 7 8 9 10
AUX : (11) [21, 92, 4, 22, 1, 67, 99, 102, 76, 43, 11]
🔺 🔺 ▲
arr : (11) [4, 21, 92, 22, 1, 67, 99, 102, 76, 43, 11]
----------------------------
LOW -> : 3
MID : -> 3
HIGH : -> 4
indexes: 0 1 2 3 4 5 6 7 8 9 10
AUX : (11) [21, 92, 4, 22, 1, 67, 99, 102, 76, 43, 11]
🔺 ▲
arr : (11) [4, 21, 92, 1, 22, 67, 99, 102, 76, 43, 11]
----------------------------
LOW -> : 3
MID : -> 4
HIGH : -> 5
indexes: 0 1 2 3 4 5 6 7 8 9 10
AUX : (11) [21, 92, 4, 1, 22, 67, 99, 102, 76, 43, 11]
🔺 🔺 ▲
arr : (11) [4, 21, 92, 1, 22, 67, 99, 102, 76, 43, 11]
----------------------------
LOW -> : 0
MID : -> 2
HIGH : -> 5
indexes: 0 1 2 3 4 5 6 7 8 9 10
AUX : (11) [4, 21, 92, 1, 22, 67, 99, 102, 76, 43, 11]
🔺 🔺 🔺 ▲ ▲ ▲
arr : (11) [1, 4, 21, 22, 67, 92, 99, 102, 76, 43, 11]
----------------------------
LOW -> : 6
MID : -> 6
HIGH : -> 7
indexes: 0 1 2 3 4 5 6 7 8 9 10
AUX : (11) [4, 21, 92, 1, 22, 67, 99, 102, 76, 43, 11]
🔺 ▲
arr : (11) [1, 4, 21, 22, 67, 92, 99, 102, 76, 43, 11]
----------------------------
LOW -> : 6
MID : -> 7
HIGH : -> 8
indexes: 0 1 2 3 4 5 6 7 8 9 10
AUX : (11) [4, 21, 92, 1, 22, 67, 99, 102, 76, 43, 11]
🔺 🔺 ▲
arr : (11) [1, 4, 21, 22, 67, 92, 76, 99, 102, 43, 11]
----------------------------
LOW -> : 9
MID : -> 9
HIGH : -> 10
indexes: 0 1 2 3 4 5 6 7 8 9 10
AUX : (11) [4, 21, 92, 1, 22, 67, 99, 102, 76, 43, 11]
🔺 ▲
arr : (11) [1, 4, 21, 22, 67, 92, 76, 99, 102, 11, 43]
----------------------------
LOW -> : 6
MID : -> 8
HIGH : -> 10
indexes: 0 1 2 3 4 5 6 7 8 9 10
AUX : (11) [4, 21, 92, 1, 22, 67, 76, 99, 102, 11, 43]
🔺 🔺 🔺 ▲ ▲
arr : (11) [1, 4, 21, 22, 67, 92, 11, 43, 76, 99, 102]
----------------------------
LOW -> : 0
MID : -> 5
HIGH : -> 10
indexes: 0 1 2 3 4 5 6 7 8 9 10
AUX : (11) [1, 4, 21, 22, 67, 92, 11, 43, 76, 99, 102]
🔺 🔺 🔺 🔺 🔺 🔺 ▲ ▲ ▲ ▲ ▲
arr : (11) [1, 4, 11, 21, 22, 43, 67, 76, 92, 99, 102]
----------------------------
*/
// METHOD 2
// Logic is same but implementation is more readable, imo!
// Also, this implementation uses JavaScript's native functions.
class Merge {
constructor (arr) {
this.arr = arr
this.sortedArray = this.sort(arr)
}
getSortedArray () {
return this.sortedArray
}
merge (left, right) {
let i = 0
let j = 0
let output = []
while (i < left.length && j < right.length) {
if (left[i] > right[j]) {
output.push(right[j++])
} else {
output.push(left[i++])
}
}
let unprocessedArray = (i < left.length ? left.slice(i) : right.slice(j))
return output.concat(unprocessedArray)
}
sort (arr) {
if (arr.length > 1) {
const mid = arr.length >> 1
const leftSubarray = this.sort(arr.slice(0, mid))
const rightSubarray = this.sort(arr.slice(mid))
arr = this.merge(leftSubarray, rightSubarray)
}
return arr
}
}
const arr = [21, 92, 4, 22, 1, 67]
const merge = new Merge(arr)
merge.getSortedArray()