Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function mergeSort(arr) {
- if (arr.length === 1) {
- return arr
- }
- center = Math.floor(arr.length / 2)
- left = arr.slice(0, center)
- right = arr.slice(center)
- return merge(mergeSort(left), mergeSort(right))
- }
- function merge(left, right) {
- const results = []
- while (left.length && right.length) {
- if (left[0] < right[0]) {
- results.push(left.shift())
- } else {
- results.push(right.shift())
- }
- }
- return [...results, ...left, ...right]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement