Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def minimum(xs: List[Int]): List[Int] =
- (List(xs.head) /: xs.tail) {
- (ys, x) =>
- if(x < ys.head) (x :: ys)
- else (ys.head :: x :: ys.tail)
- }
- def selectionSort(xs: List[Int]): List[Int] =
- if(xs.isEmpty) List()
- else {
- val ys = minimum(xs)
- if(ys.tail.isEmpty)
- ys
- else
- ys.head :: selectionSort(ys.tail)
- }
- def selectionSort(xs: List[Int]) = {
- def selectionSortHelper(xs: List[Int], accumulator: List[Int]): List[Int] =
- if(xs.isEmpty) accumulator
- else {
- val ys = maximum(xs)
- selectionSortHelper(ys.tail, ys.head :: accumulator)
- }
- selectionSortHelper(xs, Nil)
- }
- define bubble(data)
- if data is empty or just one element: return data;
- otherwise, if the first element < the second,
- return first element :: bubble(rest of data);
- otherwise, return second element :: bubble(
- first element :: (rest of data starting at 3rd element)).
- define bubble(data)
- if data is empty or just one element: return data;
- otherwise, if the first element < the second,
- return first element :: bubble(rest of data);
- otherwise, return second element :: bubble(
- first element :: (rest of data starting at 3rd element)).
- define bubblesort [data]
- apply bubble to data as often as there are elements in data.
- Break list into two sub-lists,
- either by counting off half the elements into one sublist
- and the rest in the other,
- or by copying every other element from the original list
- into either of the new lists.
- Sort each of the two smaller lists (recursion here, obviously).
- Assemble a new list by selecting the smaller from the front of either sub-list
- until you've exhausted both sub-lists.
- def min(x: Int,y: Int) = if (x<y) x else y
- def min(xs: List[Int], accu: Int): Int = xs match {
- case Nil => accu
- case x :: ys => min(ys, min(accu, x))
- }
- def minl(xs: List[Int]): List[Int] = minl(xs, List(Int.MaxValue))
- def minl(xs: List[Int],accu:List[Int]): List[Int] = xs match {
- // accu always contains min as head
- case Nil => accu take accu.length-1
- case x :: ys => minl(ys,
- if (x<accu.head) x::accu else accu.head :: x :: accu.tail )
- }
- def ssort(xs: List[Int], accu: List[Int]): List[Int] = minl(xs) match {
- case Nil => accu
- case min :: rest => ssort(rest, min::accu)
- }
Add Comment
Please, Sign In to add comment