case class BubbleSort[T< % Ordered[T]]( val array : Array[T]){ |
private [ this ] var lastIndex = array.length- 1 |
private [ this ] var notFinish = false |
private [ this ] def sortFromIndex(index : Int) : Unit = index match { |
case last if last == lastIndex = > if (lastIndex> 1 && notFinish){ |
lastIndex = lastIndex- 1 |
notFinish = false |
sortFromIndex( 0 ) |
} |
case otherIndex = > { |
if (array(otherIndex)>array(otherIndex+ 1 )) swap(otherIndex, otherIndex+ 1 ) |
sortFromIndex(otherIndex+ 1 ) |
} |
} |
private [ this ] def swap(index : Int, nextIndex : Int) = { |
val temp = array(nextIndex) |
array(nextIndex) = array(index) |
array(index) = temp |
} |
def bubbleSort = sortFromIndex( 0 ) |
} |
object BubbleSort{ |
implicit def arrToBubbleSort[T < % Ordered[T]](arr : Array[T]) : BubbleSort[T] = BubbleSort(arr) |
} |