fork download
  1. import io.Source
  2.  
  3. object MSort {
  4. def main(args:Array[String]) {
  5. val fileWithNumbers = "/Users/tmorrow/Documents/IntegerArray.txt"
  6. val inversions: BigInt = numberOfInversions(Source.fromFile(new java.io.File(fileWithNumbers)).getLines().map(Integer.parseInt).toList)
  7. println(inversions)
  8. }
  9.  
  10. def numberOfInversions(collection: List[Int]): BigInt = {
  11. var count: BigInt = 0
  12. def inversionsInner(innerCollection: List[Int]): List[Int] = {
  13. def merge(left: List[Int], right: List[Int]): Stream[Int] = (left, right) match {
  14. case (x :: xs, y :: ys) if x < y=> { Stream.cons(x, merge(xs, right)) }
  15. case (x :: xs, y :: ys) => { count = count + left.length; Stream.cons(y, merge(left, ys)) }
  16. case _ => if (left.isEmpty) right.toStream else left.toStream
  17. }
  18. val n = innerCollection.length / 2
  19. if (n == 0) innerCollection
  20. else {
  21. val (lowerHalf, upperHalf) = innerCollection splitAt n
  22. merge(inversionsInner(lowerHalf), inversionsInner(upperHalf)).toList
  23. }
  24. }
  25.  
  26. inversionsInner(collection)
  27. count
  28. }
  29. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
spoj: The program compiled successfully, but Main.class was not found.
      Class Main should contain method: def main(args: Array[String]).
stdout
Standard output is empty