Guest User

Untitled

a guest
Jan 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. ////////////////////////////////////////////////
  2. // Modified so that this can be copy and pasted to my Sorting.scala file
  3.  
  4. private def insertionSort2(a: Array[Master.ElemType], i0: Int, iN: Int): Unit = {
  5. val n = iN - i0
  6. if (n < 2) return
  7. if (a(i0) > a(i0+1)) {
  8. val temp = a(i0)
  9. a(i0) = a(i0+1)
  10. a(i0+1) = temp
  11. }
  12. var m = 2
  13. while (m < n) {
  14. // Speed up already-sorted case by checking last element first
  15. val next = a(i0 + m)
  16. if (next < a(i0+m-1)) {
  17. var iA = i0
  18. var iB = i0 + m - 1
  19. while (iB - iA > 1) {
  20. val ix = (iA + iB) >>> 1 // Use bit shift to get unsigned div by 2
  21. if (next < a(ix)) iB = ix
  22. else iA = ix
  23. }
  24. val ix = iA + (if (next < a(iA)) 0 else 1)
  25. var i = i0 + m
  26. while (i > ix) {
  27. a(i) = a(i-1)
  28. i -= 1
  29. }
  30. a(ix) = next
  31. }
  32. m += 1
  33. }
  34. }
  35.  
  36. def quicksort2(a: Array[Master.ElemType]): Unit = {
  37. val qsortThreshold = 16
  38. // Must have iN >= i0 or math will fail. Also, i0 >= 0.
  39. def inner(a: Array[Master.ElemType], i0: Int, iN: Int): Unit = {
  40. if (iN - i0 < qsortThreshold) insertionSort2(a, i0, iN)
  41. else {
  42. val iK = (i0 + iN) >>> 1 // Unsigned div by 2
  43. // Find index of median of first, central, and last elements
  44. var pL =
  45. if (a(i0) <= a(iN - 1))
  46. if (a(i0) < a(iK))
  47. if (a(iN - 1) < a(iK)) iN - 1 else iK
  48. else i0
  49. else
  50. if (a(i0) < a(iK)) i0
  51. else
  52. if (a(iN - 1) <= a(iK)) iN - 1
  53. else iK
  54. val pivot = a(pL)
  55. // pL is the start of the pivot block; move it into the middle if needed
  56. if (pL != iK) { a(pL) = a(iK); a(iK) = pivot; pL = iK }
  57. // Elements equal to the pivot will be in range pL until pR
  58. var pR = pL + 1
  59. // Items known to be less than pivot are below iA (range i0 until iA)
  60. var iA = i0
  61. // Items known to be greater than pivot are at or above iB (range iB until iN)
  62. var iB = iN
  63. // Scan through everything in the buffer before the pivot(s)
  64. while (pL - iA > 0) {
  65. val current = a(iA)
  66. (current - pivot) match {
  67. case 0 =>
  68. // Swap current out with pivot block
  69. a(iA) = a(pL - 1)
  70. a(pL - 1) = current
  71. pL -= 1
  72. case x if x < 0 =>
  73. // Already in place. Just update indices.
  74. iA += 1
  75. case _ if iB > pR =>
  76. // Wrong side. There's room on the other side, so swap
  77. a(iA) = a(iB - 1)
  78. a(iB - 1) = current
  79. iB -= 1
  80. case _ =>
  81. // Wrong side and there is no room. Swap by rotating pivot block.
  82. a(iA) = a(pL - 1)
  83. a(pL - 1) = a(pR - 1)
  84. a(pR - 1) = current
  85. pL -= 1
  86. pR -= 1
  87. iB -= 1
  88. }
  89. }
  90. // Get anything remaining in buffer after the pivot(s)
  91. while (iB - pR > 0) {
  92. val current = a(iB - 1)
  93. (current - pivot) match {
  94. case 0 =>
  95. // Swap current out with pivot block
  96. a(iB - 1) = a(pR)
  97. a(pR) = current
  98. pR += 1
  99. case x if x > 0 =>
  100. // Already in place. Just update indices.
  101. iB -= 1
  102. case _ =>
  103. // Wrong side and we already know there is no room. Swap by rotating pivot block.
  104. a(iB - 1) = a(pR)
  105. a(pR) = a(pL)
  106. a(pL) = current
  107. iA += 1
  108. pL += 1
  109. pR += 1
  110. }
  111. }
  112. // Use tail recursion on large half (Sedgewick's method) so we don't blow up the stack if pivots are poorly chosen
  113. if (iA - i0 < iN - iB) {
  114. inner(a, i0, iA) // True recursion
  115. inner(a, iB, iN) // Should be tail recursion
  116. }
  117. else {
  118. inner(a, iB, iN) // True recursion
  119. inner(a, i0, iA) // Should be tail recursion
  120. }
  121. }
  122. }
  123. inner(a, 0, a.length)
  124. }
Add Comment
Please, Sign In to add comment