Skip to content

Commit

Permalink
merge sort
Browse files Browse the repository at this point in the history
  • Loading branch information
collegewap committed May 1, 2024
1 parent e69b837 commit 8babb03
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions mergeSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
console.clear()

function mergeSort(arr) {
if (arr.length <= 1) {
// console.log(arr)
return arr
}
const mid = Math.floor(arr.length / 2)

const left = mergeSort(arr.slice(0, mid))
const right = mergeSort(arr.slice(mid))



const returnArray = []

while (left.length !== 0 && right.length !== 0) {
if (left[0] < right[0]) {
returnArray.push(left.shift())
} else {
returnArray.push(right.shift())
}
}

if (left.length > 0) {
returnArray.push(...left)
} else {
returnArray.push(...right)

}
console.log(returnArray)
return returnArray
}

const arr = [8, 4, 3, 1, 6, 7, 2, 5]
console.log(mergeSort(arr))

// O(n logn)

0 comments on commit 8babb03

Please sign in to comment.