cecell

Untitled

Nov 8th, 2021 (edited)
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. Scriptname TestSorts Hidden
  2.  
  3. function StartSortFunctionInt()
  4. Int[] aIntArray = New Int[13]
  5. aIntArray[0] = 10
  6. aIntArray[1] = 11
  7. aIntArray[2] = -1
  8. aIntArray[3] = 0
  9. aIntArray[4] = 8
  10. aIntArray[5] = 1
  11. aIntArray[6] = 50
  12. aIntArray[7] = 8
  13. aIntArray[8] = 50
  14. aIntArray[9] = 10
  15. aIntArray[10] = 0
  16. aIntArray[11] = 44
  17. aIntArray[12] = 999
  18. Debug.Trace("aIntArray as String Before: " + aIntArray as String)
  19. cArrayQuickSortInt(aIntArray) ; works just fine
  20. Debug.Trace("aIntArray as String After: " + aIntArray as String)
  21. endfunction
  22.  
  23. ; works great
  24. function cArrayQuickSortInt(Int[] aArray, Int low = -1, Int high = -1) global
  25. if high == -1
  26. high = aArray.length - 1
  27. endif
  28. if low == -1
  29. low = 0
  30. endif
  31. if low < high
  32. ; pi is partitioning index, after this aArray[pivot] now at right place
  33. Int pi = cArrayPartitionInt(aArray, low, high)
  34. ; Separately sort elements before partition and after partition
  35. cArrayQuickSortInt(aArray, low, pi - 1)
  36. cArrayQuickSortInt(aArray, pi + 1, high)
  37. endif
  38. endfunction
  39. Int function cArrayPartitionInt(Int[] aArray, Int low, Int high) global
  40. Int pivot = aArray[high]
  41. Int i = low - 1 ; Index of smaller element and indicates the right position of pivot found so far
  42. Int j = low
  43. while j <= (high - 1)
  44. if aArray[j] < pivot
  45. i += 1
  46. aArraySwapIndexInt(aArray, i, j)
  47. endif
  48. j += 1
  49. endwhile
  50. aArraySwapIndexInt(aArray, i + 1, high)
  51. return (i + 1)
  52. endfunction
  53.  
  54.  
  55.  
  56.  
  57. function StartSortFunctionString()
  58. String[] cArray = New String[14]
  59. cArray[0] = ""
  60. cArray[1] = "ppp"
  61. cArray[2] = ""
  62. cArray[3] = "rrr"
  63. cArray[4] = ""
  64. cArray[5] = "qqq"
  65. cArray[6] = ""
  66. cArray[7] = "ttt"
  67. cArray[8] = ""
  68. cArray[9] = "aaa"
  69. cArray[10] = ""
  70. cArray[11] = "bcb"
  71. cArray[12] = "bcb"
  72. cArray[13] = "zzz"
  73. Debug.Trace("cArray as String Before: " + cArray as String)
  74. cArrayQuickSortString(cArray) ; never makes it past here
  75. Debug.Trace("cArray as String After: " + cArray as String)
  76. endfunction
  77.  
  78. ; doesn't work!
  79. function cArrayQuickSortString(String[] aArray, Int low = -1, Int high = -1) global
  80. if high == -1
  81. high = aArray.length - 1
  82. endif
  83. if low == -1
  84. low = 0
  85. endif
  86. if low < high
  87. ; pi is partitioning index, after this aArray[pivot] now at right place
  88. Int pi = cArrayPartitionString(aArray, low, high)
  89. ; Separately sort elements before partition and after partition
  90. cArrayQuickSortString(aArray, low, pi - 1)
  91. cArrayQuickSortString(aArray, pi + 1, high)
  92. endif
  93. endfunction
  94. Int function cArrayPartitionString(String[] aArray, Int low, Int high) global
  95. String pivot = aArray[high]
  96. Int i = low - 1 ; Index of smaller element and indicates the right position of pivot found so far
  97. Int j = low
  98. while j <= (high - 1)
  99. if aArray[j] < pivot
  100. i += 1
  101. aArraySwapIndexString(aArray, i, j)
  102. endif
  103. j += 1
  104. endwhile
  105. aArraySwapIndexString(aArray, i + 1, high)
  106. return (i + 1)
  107. endfunction
  108.  
  109. function aArraySwapIndexInt(Int[] aArray, Int index1, Int index2) global
  110. Int tempValue = aArray[index1]
  111. aArray[index1] = aArray[index2]
  112. aArray[index2] = tempValue
  113. endfunction
  114. function aArraySwapIndexString(String[] aArray, Int index1, Int index2) global
  115. String tempValue = aArray[index1]
  116. aArray[index1] = aArray[index2]
  117. aArray[index2] = tempValue
  118. endfunction
Advertisement
Add Comment
Please, Sign In to add comment