Skip to content

Instantly share code, notes, and snippets.

@InvisibleTech
Last active August 29, 2015 14:27
Show Gist options
  • Save InvisibleTech/897439527b6f5263cbc0 to your computer and use it in GitHub Desktop.
Save InvisibleTech/897439527b6f5263cbc0 to your computer and use it in GitHub Desktop.

Revisions

  1. InvisibleTech revised this gist Aug 23, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion selectionsort.scala
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /*
    While refreshing hot to determin Orders of algorithms: O, Ω, atc. using an online tutorial, it covered selecton sort as an
    While refreshing how to determine orders of algorithms: O, Ω, atc. using an online tutorial, it covered selecton sort as an
    example. So, I decided to implment it in Scala to be a generic version, sort of.
    */
    import Ordering.Implicits._
  2. InvisibleTech created this gist Aug 23, 2015.
    32 changes: 32 additions & 0 deletions selectionsort.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    /*
    While refreshing hot to determin Orders of algorithms: O, Ω, atc. using an online tutorial, it covered selecton sort as an
    example. So, I decided to implment it in Scala to be a generic version, sort of.
    */
    import Ordering.Implicits._

    def indexOfMinimum[T:Ordering](a: Array[T], start: Int) : Int = {
    var minValue = a(start)
    var mindex = start

    for( i <- mindex+1 until a.length) {
    if (a(i) < minValue) {
    minValue = a(i)
    mindex = i
    }
    }

    return mindex
    }

    def swap[T](a: Array[T], to: Int, from: Int) : Unit = {
    val temp = a(to)
    a(to) = a(from)
    a(from) = temp
    }

    def selectionSort[T:Ordering](a: Array[T]) : Unit = {
    for (i <- 0 until a.length) {
    val mindex = indexOfMinimum(a, i)
    swap(a, i, mindex)
    }