Skip to content

Commit

Permalink
Adding merge sort in scala
Browse files Browse the repository at this point in the history
  • Loading branch information
alanfgn committed Nov 1, 2022
1 parent 6c5a341 commit 69f4171
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions sort/merge_sort/scala/MergeSort.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
object MergeSort {

def merge(leftArr: Array[Int], rightArr: Array[Int]) = {
var leftIndex = 0
var rightIndex = 0

var sortedArr = Array.empty[Int]

while (leftIndex < leftArr.length && rightIndex < rightArr.length) {
if (leftArr(leftIndex) < rightArr(rightIndex)) {
sortedArr = sortedArr.appended(leftArr(leftIndex))
leftIndex += 1
} else {
sortedArr = sortedArr.appended(rightArr(rightIndex))
rightIndex += 1
}
}

val restLeft = leftArr.slice(leftIndex, leftArr.length)
val restRight = rightArr.slice(rightIndex, rightArr.length)

sortedArr ++ restLeft ++ restRight
}

def mergeSort(arr: Array[Int]): Array[Int] = {
if (arr.length <= 1) return arr

val mid = arr.length / 2
var (left, right) = arr.splitAt(mid)

left = mergeSort(left)
right = mergeSort(right)

merge(left, right)
}


def main(args: Array[String]): Unit = {
val arr = Array(11, 3, 1, 13, 4, 5, 12, 9, 2, 6, 8, 7, 10)
val sortedArray = mergeSort(arr)
println(sortedArray.mkString(", "))
}

}

0 comments on commit 69f4171

Please sign in to comment.