progs/lecture3.scala
changeset 343 c8fcc0e0a57f
parent 335 7e00d2b13b04
child 364 f1a6fa599d26
--- a/progs/lecture3.scala	Sat Aug 29 16:05:59 2020 +0100
+++ b/progs/lecture3.scala	Tue Oct 13 10:21:21 2020 +0100
@@ -34,6 +34,23 @@
 
 l.map(test).flatten
 
+// naive quicksort with "On" function
+
+def sortOn(f: Int => Int, xs: List[Int]) : List[Int] = {
+  if (xs.size < 2) xs
+  else {
+   val pivot = xs.head
+   val (left, right) = xs.partition(f(_) < f(pivot))
+   sortOn(f, left) ::: pivot :: sortOn(f, right.tail)
+  }
+} 
+
+sortOn(identity, List(99,99,99,98,10,-3,2)) 
+sortOn(n => - n, List(99,99,99,98,10,-3,2))
+
+
+
+
 // Recursion Again ;o)
 //====================