Scala algorithm: Median of two sorted arrays
Published
Algorithm goal
The median value of a sorted list is the value right in the middle. In case the list is even, the average of the two middle elements is taken. For example, the median of [1,2,9] is 2, and the median of [1,2,3,9] is 2.5 (= (2 + 3) / 2).
Efficiently compute the median of two sorted arrays.
Test cases in Scala
Algorithm in Scala
40 lines of Scala (compatible versions 2.13 & 3.0).
Explanation
The brute-force solution of adding two arrays to each other requires a sort, which is O(nlogn). The efficient solution is O(n).
In order to approach this problem, first we must consider the length of both arrays, L(A) and L(B): The median of the two lists is at the half-way of L(A)+L(B) combined (≈(L(A)+L(B))/2). So the length of each side must be the same (assuming even case, and one-off otherwise). (this is © from www.scala-algorithms.com)
Assume the largest value of A is smaller than the largest value of B (this is the reason behind doing 'notRightEnd', and 'defRightEnd'). Then we are certain that the last element of the final output is coming from B.
Scala concepts & Hints
Class Inside Def
Like Def Inside Def, classes can also be defined in defs.
This is particularly useful when defining temporary data types, in particular groupings.
Def Inside Def
A great aspect of Scala is being able to declare functions inside functions, making it possible to reduce repetition.
It is also frequently used in combination with Tail Recursion.
For-comprehension
The for-comprehension is highly important syntatic enhancement in functional programming languages.
Option Type
The 'Option' type is used to describe a computation that either has a result or does not. In Scala, you can 'chain' Option processing, combine with lists and other data structures. For example, you can also turn a pattern-match into a function that return an Option, and vice-versa!
Range
The
(1 to n)
syntax produces a "Range" which is a representation of a sequence of numbers.