-
- Notifications
You must be signed in to change notification settings - Fork 359
Bubble sort in scala implementation #417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
22b71ee
e4f9cfe
d8fa52d
ea1368d
068c756
d74dc85
36e6d4a
241d7c7
ce50882
fa0a7d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,23 @@ | ||
object BubbleSort { | ||
def bubbleBiggest(unprocessed: List[Int], notBiggest: List[Int] = List()): List[Int] = | ||
unprocessed match { | ||
case a :: b :: tail => | ||
if (a > b) bubbleBiggest(a :: tail, b :: notBiggest) | ||
else bubbleBiggest(b :: tail, a :: notBiggest) | ||
case a :: Nil => a :: notBiggest | ||
case Nil => unprocessed | ||
| ||
def bubbleUpLargestToTheEnd(list: List[Int]): List[Int] = | ||
list match { | ||
case first :: second :: tail if first > second => second :: bubbleUpLargestToTheEnd(first :: tail) | ||
case first :: second :: tail => first :: bubbleUpLargestToTheEnd(second :: tail) | ||
case _ => list | ||
} | ||
| ||
def bubbleSort(unsorted: List[Int], sorted: List[Int] = List()): List[Int] = | ||
bubbleBiggest(unsorted) match { | ||
case m :: us => bubbleSort(us, m :: sorted) | ||
case _ => sorted | ||
def bubbleSort(list: List[Int]): List[Int] = | ||
bubbleUpLargestToTheEnd(list) match { | ||
case unsorted :+ largest => bubbleSort(unsorted) :+ largest | ||
| ||
case _ => list | ||
} | ||
| ||
def main(args: Array[String]): Unit = | ||
println(bubbleSort(List(9, 2, 0, 5, 3, 8, 1, 9, 4, 0, 7, 0, 9, 9, 0))) | ||
} | ||
| ||
def main(args: Array[String]): Unit = { | ||
val unsorted = List(9, 2, 0, 5, 3, 8, 1, 9, 4, 0, 7, 0, 9, 9, 0) | ||
| ||
println("Unsorted list is " + unsorted) | ||
println(" Sorted list is " + bubbleSort(unsorted)) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.