mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 01:18:23 +08:00
fix: Fix MergeSort conflict
This commit is contained in:
@ -36,15 +36,18 @@
|
|||||||
|
|
||||||
function merge (list1, list2) {
|
function merge (list1, list2) {
|
||||||
const results = []
|
const results = []
|
||||||
|
let i = 0
|
||||||
|
let j = 0
|
||||||
|
|
||||||
while (list1.length && list2.length) {
|
while (i < list1.length && j < list2.length) {
|
||||||
if (list1[0] <= list2[0]) {
|
if (list1[i] < list2[j]) {
|
||||||
results.push(list1.shift())
|
results.push(list1[i++])
|
||||||
} else {
|
} else {
|
||||||
results.push(list2.shift())
|
results.push(list2[j++])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return results.concat(list1, list2)
|
|
||||||
|
return results.concat(list1.slice(i), list2.slice(j))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user