We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2bf0ccf commit 51f80feCopy full SHA for 51f80fe
Sorts/MergeSort.js
@@ -36,15 +36,18 @@
36
37
function merge (list1, list2) {
38
const results = []
39
+ let i = 0
40
+ let j = 0
41
- while (list1.length && list2.length) {
- if (list1[0] <= list2[0]) {
42
- results.push(list1.shift())
+ while (i < list1.length && j < list2.length) {
43
+ if (list1[i] < list2[j]) {
44
+ results.push(list1[i++])
45
} else {
- results.push(list2.shift())
46
+ results.push(list2[j++])
47
}
48
- return results.concat(list1, list2)
49
+
50
+ return results.concat(list1.slice(i), list2.slice(j))
51
52
53
/**
0 commit comments