Guest User

Untitled

a guest
Apr 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. def minimum(xs: List[Int]): List[Int] =
  2. (List(xs.head) /: xs.tail) {
  3. (ys, x) =>
  4. if(x < ys.head) (x :: ys)
  5. else (ys.head :: x :: ys.tail)
  6. }
  7.  
  8. def selectionSort(xs: List[Int]): List[Int] =
  9. if(xs.isEmpty) List()
  10. else {
  11. val ys = minimum(xs)
  12. if(ys.tail.isEmpty)
  13. ys
  14. else
  15. ys.head :: selectionSort(ys.tail)
  16. }
  17.  
  18. def selectionSort(xs: List[Int]) = {
  19. def selectionSortHelper(xs: List[Int], accumulator: List[Int]): List[Int] =
  20. if(xs.isEmpty) accumulator
  21. else {
  22. val ys = maximum(xs)
  23. selectionSortHelper(ys.tail, ys.head :: accumulator)
  24. }
  25.  
  26. selectionSortHelper(xs, Nil)
  27. }
  28.  
  29. define bubble(data)
  30. if data is empty or just one element: return data;
  31. otherwise, if the first element < the second,
  32. return first element :: bubble(rest of data);
  33. otherwise, return second element :: bubble(
  34. first element :: (rest of data starting at 3rd element)).
  35.  
  36. define bubble(data)
  37. if data is empty or just one element: return data;
  38. otherwise, if the first element < the second,
  39. return first element :: bubble(rest of data);
  40. otherwise, return second element :: bubble(
  41. first element :: (rest of data starting at 3rd element)).
  42.  
  43. define bubblesort [data]
  44. apply bubble to data as often as there are elements in data.
  45.  
  46. Break list into two sub-lists,
  47. either by counting off half the elements into one sublist
  48. and the rest in the other,
  49. or by copying every other element from the original list
  50. into either of the new lists.
  51.  
  52. Sort each of the two smaller lists (recursion here, obviously).
  53.  
  54. Assemble a new list by selecting the smaller from the front of either sub-list
  55. until you've exhausted both sub-lists.
  56.  
  57. def min(x: Int,y: Int) = if (x<y) x else y
  58.  
  59. def min(xs: List[Int], accu: Int): Int = xs match {
  60. case Nil => accu
  61. case x :: ys => min(ys, min(accu, x))
  62. }
  63.  
  64. def minl(xs: List[Int]): List[Int] = minl(xs, List(Int.MaxValue))
  65.  
  66. def minl(xs: List[Int],accu:List[Int]): List[Int] = xs match {
  67. // accu always contains min as head
  68. case Nil => accu take accu.length-1
  69. case x :: ys => minl(ys,
  70. if (x<accu.head) x::accu else accu.head :: x :: accu.tail )
  71. }
  72.  
  73. def ssort(xs: List[Int], accu: List[Int]): List[Int] = minl(xs) match {
  74. case Nil => accu
  75. case min :: rest => ssort(rest, min::accu)
  76. }
Add Comment
Please, Sign In to add comment