object QuickSort {
def main(args: Array[String]) = {
val a = Array(5, 3, 2, 1, 20, 46, 9, 39 ,219)
sort(a).foreach(n=> (print(n), print (" " )))
}
def sort(a:Array[Int]): Array[Int] = {
if (a.length < 2) a
else {
val pivot = a(a.length / 2)
sort (a filter (pivot>)) ++ (a filter (pivot == )) ++
sort (a filter(pivot <))
}
}
}
First of all I am doing an assignment where I should explain various sorting algorithms in Scala. I am relatively new to Scala. I already left another post and I was answered and it helped me. My problem is with these 3 lines: These 2 lines in the sort method (I cant understand the use of filter, ++ and ==)
sort (a filter (pivot>)) ++ (a filter (pivot == )) ++
sort (a filter(pivot <))
and this line in the main method (I cant understand the user of n and the term foreach)
sort(a).foreach(n=> (print(n), print (" " )))
I tried googling these problems but got no help. Would appreciate a reply,
Thank you