We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 1f23206 commit 4d1c6eaCopy full SHA for 4d1c6ea
1 file changed
Sorts/MergeSort.js
@@ -35,16 +35,19 @@
35
*/
36
37
function merge (list1, list2) {
38
- var results = []
+ 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