-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
10 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,23 @@ | ||
import scala.annotation.tailrec | ||
import scala.util.Random | ||
|
||
|
||
@tailrec | ||
def isSorted(data: Seq[Int]): Boolean = { | ||
if (data.size < 2) true | ||
else if (data(0) > data(1)) false | ||
else isSorted(data.tail) | ||
if (data.size < 2) true | ||
else if (data(0) > data(1)) false | ||
else isSorted(data.tail) | ||
} | ||
|
||
|
||
@tailrec | ||
def bogosort(data: Seq[Int]): Seq[Int] = { | ||
val result: Seq[Int] = Random.shuffle(data) | ||
if (isSorted(result)) result | ||
else bogosort(data) | ||
val result: Seq[Int] = Random.shuffle(data) | ||
if (isSorted(result)) result | ||
else bogosort(data) | ||
} | ||
|
||
|
||
object Main extends App { | ||
val data: Seq[Int] = Seq.fill(10)(Random.nextInt(10)) | ||
println(s"Unsorted data: $data") | ||
println(s"Sorted data: ${bogosort(data)}") | ||
val data: Seq[Int] = Seq.fill(10)(Random.nextInt(10)) | ||
println(s"Unsorted data: $data") | ||
println(s"Sorted data: ${bogosort(data)}") | ||
} | ||
|