bogosort


See On Github

Data

Source Code

class BogoSort {

  def isSorted(l:List[Int]): Boolean = l match {
    case Nil => true
    case x :: Nil => true
    case x :: xs => x <= xs.head && isSorted(xs)
  }
  
  def bogoSortMethod(listForShuffle: List[Int]):List[Int] = {

    val shuffledList = util.Random.shuffle(listForShuffle)

    if(isSorted(shuffledList)) shuffledList
    else
      bogoSortMethod(shuffledList)
  }
}


import org.scalatest.{Matchers, FunSuite}

class BogoSortTest extends FunSuite with Matchers {

  test("Bogosort should sort") {
    val objectForBogoSort = new BogoSort

    objectForBogoSort.bogoSortMethod(List(1,2,3,4,5)) should be(List(1,2,3,4,5))
    objectForBogoSort.bogoSortMethod(List(11,4,2,6,1)) should be(List(1,2,4,6,11))
    objectForBogoSort.bogoSortMethod(List(55,44,33,22,11,0)) should be(List(0,11,22,33,44,55))
  }

}