Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,16 @@
|
|||
def bubbleSort[T](arr: Array[T])(implicit o: Ordering[T]) {
|
||||
import o._
|
||||
val consecutiveIndices = (arr.indices, arr.indices drop 1).zipped
|
||||
var hasChanged = true
|
||||
do {
|
||||
hasChanged = false
|
||||
consecutiveIndices foreach { (i1, i2) =>
|
||||
if (arr(i1) > arr(i2)) {
|
||||
hasChanged = true
|
||||
val tmp = arr(i1)
|
||||
arr(i1) = arr(i2)
|
||||
arr(i2) = tmp
|
||||
}
|
||||
}
|
||||
} while(hasChanged)
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
import scala.annotation.tailrec
|
||||
|
||||
def bubbleSort(xt: List[Int]) = {
|
||||
@tailrec
|
||||
def bubble(xs: List[Int], rest: List[Int], sorted: List[Int]): List[Int] = xs match {
|
||||
case x :: Nil =>
|
||||
if (rest.isEmpty) x :: sorted
|
||||
else bubble(rest, Nil, x :: sorted)
|
||||
case a :: b :: xs =>
|
||||
if (a > b) bubble(a :: xs, b :: rest, sorted)
|
||||
else bubble(b :: xs, a :: rest, sorted)
|
||||
}
|
||||
bubble(xt, Nil, Nil)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue