Lytol (owner)

Forks

Revisions

gist: 225870 Download_button fork
public
Description:
Selection sort implementation in Scala
Public Clone URL: git://gist.github.com/225870.git
Embed All Files: show embed
selection_sort.scala #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Inmperative version
//
def selectionSort(list: Array[Int]): Unit {
  def swap(list: Array[Int], i: Int, j: Int) {
    var tmp = list(i)
    list(i) = list(j)
    list(j) = tmp
  }
  
  var i = 0
  while(i < (list.length - 1)) {
    var min = i
    var j = i + 1
    
    while (j < list.length) {
      if(list(j) < list(min)) {
        min = j
      }
      j += 1
    }
    
    swap(list, i, min)
    i += 1
  }
}
 
 
// Functional / Recursive version
//
def selectionSort2(list: List[Int]): List[Int] = {
  
  if(list.length == 1) list
  else {
    // Pseudo code
    // list.min :: selectionSort2(list.everything_but_min)
  }
}