views:

266

answers:

5

I'm making my way through "Programming in Scala" and wrote a quick implementation of the selection sort algorithm. However, since I'm still a bit green in functional programming, I'm having trouble translating to a more Scala-ish style. For the Scala programmers out there, how can I do this using Lists and vals rather than falling back into my imperative ways?

http://gist.github.com/225870

A: 

You may want to try replacing your while loops with recursion, so, you have two places where you can create new recursive functions.

That would begin to get rid of some vars.

This was probably the toughest lesson for me, trying to move more toward FP.

I hesitate to show solutions here, as I think it would be better for you to try first.

But, if possible you should be using tail-recursion, to avoid problems with stack overflows (if you are sorting a very, very large list).

James Black
Yeah, it's not so much the recursion as the literal implementation of the recursive function that I'm falling short on. I updated the Gist with pseudo code as well. Thanks for the advice, though... I'll keep plugging away.
Lytol
+1  A: 

You need a helper function which does the selection. It should return the minimal element and the rest of the list with the element removed.

starblue
That is exactly the part I'm kind of struggling with... I can think of very inefficient ways of doing this, but none of it seems ideal. I updated the Gist with some pseudo-code as well.
Lytol
+5  A: 

As starblue already said, you need a function that calculates the minimum of a list and returns the list with that element removed. Here is my tail recursive implementation of something similar (as I believe foldl is tail recursive in the standard library), and I tried to make it as functional as possible :). It returns a list that contains all the elements of the original list (but kindof reversed - see the explanation below) with the minimum as a head.

def minimum(xs: List[Int]): List[Int] =
  (List(xs.head) /: xs.tail) {
    (ys, x) =>
      if(x < ys.head) (x :: ys)
      else            (ys.head :: x :: ys.tail)
  }

This basically does a fold, starting with a list containing of the first element of xs If the first element of xs is smaller than the head of that list, we pre-append it to the list ys. Otherwise, we add it to the list ys as the second element. And so on recursively, we've folded our list into a new list containing the minimum element as a head and a list containing all the elements of xs (not necessarily in the same order) with the minimum removed, as a tail. Note that this function does not remove duplicates.

After creating this helper function, it's now easy to implement selection sort.

def selectionSort(xs: List[Int]): List[Int] =  
  if(xs.isEmpty) List()
  else {
    val ys = minimum(xs)
    if(ys.tail.isEmpty) 
      ys
    else
      ys.head :: selectionSort(ys.tail)
  }

Unfortunately this implementation is not tail recursive, so it will blow up the stack for large lists. Anyway, you shouldn't use a O(n^2) sort for large lists, but still... it would be nice if the implementation was tail recursive. I'll try to think of something... I think it will look like the implementation of a fold.

Tail Recursive!

To make it tail recursive, I use quite a common pattern in functional programming - an accumulator. It works a bit backward, as now I need a function called maximum, which basically does the same as minimum, but with the maximum element - its implementation is exact as minimum, but using > instead of <.

def selectionSort(xs: List[Int]) = {
  def selectionSortHelper(xs: List[Int], accumulator: List[Int]): List[Int] =
    if(xs.isEmpty) accumulator
    else {
          val ys = maximum(xs)
          selectionSortHelper(ys.tail, ys.head :: accumulator)
    }

  selectionSortHelper(xs, Nil) 
  }

EDIT: Changed the answer to have the helper function as a subfunction of the selection sort function.

It basically accumulates the maxima to a list, which it eventually returns as the base case. You can also see that it is tail recursive by replacing accumulator by throw new NullPointerException - and then inspect the stack trace.

Here's a step by step sorting using an accumulator. The left hand side shows the list xs while the right hand side shows the accumulator. The maximum is indicated at each step by a star.

64* 25 12 22 11  ------- Nil
11 22 12 25*     ------- 64
22* 12 11        ------- 25 64
11 12*           ------- 22 25 64
11*              ------- 12 22 25 64
Nil              ------- 11 12 22 25 64

The following shows a step by step folding to calculate the maximum:

maximum(25 12 64 22 11) 25 :: Nil /: 12 64 22 11 -- 25 > 12, so it stays as head 25 :: 12 /: 64 22 11 -- same as above 64 :: 25 12 /: 22 11 -- 25

Hope it helps :)

-- Flaviu Cipcigan

Flaviu Cipcigan
I recommend making the tail-recursive function a subfunction of the main one. Otherwise you have to be concerned about things like the class or method being final to ensure the optimization can be applied.
Daniel
Yeah, that's a good idea, thanks. I'll edit my answer to have it as a subfunction.
Flaviu Cipcigan
That is awesomely helpful not only in my specific question, but just as furthering my understanding of FP as well. Thanks, Flaviu!
Lytol
Great answer, if I could vote twice I would!
Lachlan
A: 

You should have problems doing selection sort in functional style, as it is an in-place sort algorithm. In-place, by definition, isn't functional.

The main problem you'll face is that you can't swap elements. Here's why this is important. Suppose I have a list (a0 ... ax ... an), where ax is the minimum value. You need to get ax away, and then compose a list (a0 ... ax-1 ax+1 an). The problem is that you'll necessarily have to copy the elements a0 to ax-1, if you wish to remain purely functional. Other functional data structures, particularly trees, can have better performance than this, but the basic problem remains.

Daniel
A: 

I think it's reasonably feasible to do a selection sort in a functional style, but as Daniel indicated, it has a good chance of performing horribly.

I just tried my hand at writing a functional bubble sort, as a slightly simpler and degenerate case of selection sort. Here's what I did, and this hints at what you could do:

define bubble(data)
  if data is empty or just one element: return data;
  otherwise, if the first element < the second,
    return first element :: bubble(rest of data);
    otherwise, return second element :: bubble(
      first element :: (rest of data starting at 3rd element)).

Once that's finished recursing, the largest element is at the end of the list. Now,

define bubblesort [data]
  apply bubble to data as often as there are elements in data.

When that's done, your data is indeed sorted. Yes, it's horrible, but my Clojure implementation of this pseudocode works.

Just concerning yourself with the first element or two and then leaving the rest of the work to a recursed activity is a lisp-y, functional-y way to do this kind of thing. But once you've gotten your mind accustomed to that kind of thinking, there are more sensible approaches to the problem.

I would recommend implementing a merge sort:

Break list into two sub-lists, 
either by counting off half the elements into one sublist 
  and the rest in the other,
or by copying every other element from the original list 
  into either of the new lists.

Sort each of the two smaller lists (recursion here, obviously).

Assemble a new list by selecting the smaller from the front of either sub-list
until you've exhausted both sub-lists.

The recursion is in the middle of that, and I don't see a clever way of making the algorithm tail recursive. Still, I think it's O(log-2) in time and also doesn't place an exorbitant load on the stack.

Have fun, good luck!

Carl Smotricz