0
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

Jurgen Cuschieri
  • 658
  • 14
  • 33
  • please check [Scala Standard Library API](http://www.scala-lang.org/api/current/index.html#scala.Array) for the meanings of the keywords, and [this](http://www.scala-lang.org/node/133) for an explanation of anonymous functions, which is what ` n=> bla bla` is. – Karthik T Jan 09 '13 at 03:03
  • You are welcome. the syntax of `pivot <` is new to me, but likely it is also a style of anonymous function – Karthik T Jan 09 '13 at 03:06
  • Selects all elements of this mutable indexed sequence which satisfy a predicate. I dont see the > and < making sense, but if swap them, the program is sorted the other way round so obviously they are right. im understanding if when a is filtered, predicate is satisfied, put items on left hand side of pivot. thus in the first case (pivot>) means that if array contents are smaller than the pivot they come before it. so forth for the others. Do u think im right? – Jurgen Cuschieri Jan 09 '13 at 03:28
  • Yes it looks like it, perhaps it is a shorthand for the shorthanded shorthand notation of `pivot > _` ( yup scala loves to make it easy for programmer to write anonymous functions) – Karthik T Jan 09 '13 at 03:30
  • thanks very much. in the main method the for each i understood what it does. but what does n do exactly? could it have been any other letter? does n represent any function? it could have been f? I tried it and that is the closest to justifying it that I could get – Jurgen Cuschieri Jan 09 '13 at 03:47
  • n Represents an element of your array. Basically its like for each element of this array, perform this action `print(x), print(" ")` – Karthik T Jan 09 '13 at 04:48

1 Answers1

2

This is written in a functional programming style where filter and foreach are higher-order functions, i.e. they take another function as input.

The part in the main function with n => (print ... ) is syntactic sugar for an anonymous function that would look like this:

def someName(n : String) = (print ...)

The foreach in turn applies this function to every argument of the list in turn. Note that the short way of writing it with n => ... allows you to save on the def someName as well as the need to type the argument n, because the compiler automatically derives that it must be a String argument.

Similarly, pivot >, pivot <, and pivot == are just syntactic sugar for writing functions of the form

def gt(arg : String) = pivot > arg

And once again, filter applies this function to each argument of a list and builds a result list from those arguments, for which this function returns true.

In other words, (a filter (pivot>)) does the following: the elements of list a are compared against the pivot element, and a filter (pivot>) then returns the list of all elements of a that are smaller than the pivot element.

A simpler example from the Scala REPL:

scala> List(1,2,3,4,5) filter (3>)
res0: List[Int] = List(1, 2)

I assume you know how quicksort works, so all you need to place the pieces of this puzzle together now is to know that ++ concatenates lists, so you perform the traditional quicksort in those lines: sort the elements that are smaller than the pivot, add the pivot elements, and add the sorted elements larger than the pivot.

Frank
  • 10,461
  • 2
  • 31
  • 46