selection sort


See On Github

Data

Source Code

/* SCALA Implementation of Selection Sort.

  Though the corner cases are covered. But still if you find any additions to it,
  please do add a test for it.

  Any improvements/tests in the code is highly appreciated.
 */

class SelectionSort {

  def swapNumbers(numbersList: Array[Int], i1: Int, i2: Int) = {

    val temp = numbersList(i1)
    numbersList(i1) = numbersList(i2)
    numbersList(i2) = temp
  }

  def selectionSort(inputElements: Array[Int]): Array[Int] = {
    for (i <- 0 until inputElements.size - 1)
      swapNumbers(inputElements, i, (i + 1 until inputElements.size).foldLeft(i)((currentMinimum, currentIndex) =>
        if (inputElements(currentIndex) < inputElements(currentMinimum))
          currentIndex
        else
          currentMinimum
      ))
    inputElements
  }
}


import org.scalatest.{Matchers, FunSuite}

class SelectionSortTest extends FunSuite with Matchers {

  test("Selection Sort should sort") {
    val objectForSelectionSort = new SelectionSort

    objectForSelectionSort.selectionSort(Array(0,1,2,3,4,5)) should be(Array(0,1,2,3,4,5))
    objectForSelectionSort.selectionSort(Array(33,22,25,31)) should be(Array(22,25,31,33))
    objectForSelectionSort.selectionSort(Array(0)) should be(Array(0))
    objectForSelectionSort.selectionSort(Array(1,1,0,0)) should be(Array(0,0,1,1))
  }

}