This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val aList = List(3,5,2,7) | |
| val sortedList = aList.sorted | |
| //if we want backward | |
| given decendingOrdering: Ordering[Int] = Ordering.fromLessThan(_ > _) //same as (a,b) => a > b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def binarySearch(keys: Seq[Int],timestamp:Int): Int = { | |
| val seq = keys.toSeq.sorted | |
| var l = 0 | |
| var r = seq.size - 1 | |
| while (l <= r) { | |
| val m = l + ((r - l) >> 1); | |
| val p = seq(m) | |
| if (p > timestamp) { | |
| r = m - 1 | |
| } else { |