Advertisement
nO_OnE_910

[AutoIt] Quicksort Test

Feb 22nd, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 4.65 KB | None | 0 0
  1. #cs
  2. ;Output:
  3.  
  4. Array in:  4, 3, 2, 1
  5. Quicksort: 0, 3
  6. Partition: 0, 3
  7.   4  ,3  ,2  ,1  
  8. i|pj |   |   |r  
  9.   4  ,3  ,2  ,1  
  10. i|p  |j  |   |r  
  11.   4  ,3  ,2  ,1  
  12. i|p  |   |j  |r  
  13.   1  ,3  ,2  ,4  
  14. i|p  |   |   |rj
  15. Quicksort: 0, -1
  16. Quicksort: 1, 3
  17. Partition: 1, 3
  18.   1  ,3  ,2  ,4  
  19.  |i  |pj |   |r  
  20.   1  ,3  ,2  ,4  
  21.  |   |pi |j  |r  
  22.   1  ,3  ,2  ,4  
  23.  |   |p  |i  |rj
  24. Quicksort: 1, 2
  25. Partition: 1, 2
  26.   1  ,3  ,2  ,4  
  27.  |i  |pj |r  |  
  28.   1  ,2  ,3  ,4  
  29.  |i  |p  |rj |  
  30. Quicksort: 1, 0
  31. Quicksort: 2, 2
  32. Quicksort: 4, 3
  33. Array out: 1, 2, 3, 4
  34. Array in:  6, 7, 3, 1, 4, 9, 5, 6, 7
  35. Quicksort: 0, 8
  36. Partition: 0, 8
  37.   6  ,7  ,3  ,1  ,4  ,9  ,5  ,6  ,7  
  38. i|pj |   |   |   |   |   |   |   |r  
  39.   6  ,7  ,3  ,1  ,4  ,9  ,5  ,6  ,7  
  40.  |pi |j  |   |   |   |   |   |   |r  
  41.   6  ,7  ,3  ,1  ,4  ,9  ,5  ,6  ,7  
  42.  |p  |i  |j  |   |   |   |   |   |r  
  43.   6  ,7  ,3  ,1  ,4  ,9  ,5  ,6  ,7  
  44.  |p  |   |i  |j  |   |   |   |   |r  
  45.   6  ,7  ,3  ,1  ,4  ,9  ,5  ,6  ,7  
  46.  |p  |   |   |i  |j  |   |   |   |r  
  47.   6  ,7  ,3  ,1  ,4  ,9  ,5  ,6  ,7  
  48.  |p  |   |   |   |i  |j  |   |   |r  
  49.   6  ,7  ,3  ,1  ,4  ,9  ,5  ,6  ,7  
  50.  |p  |   |   |   |i  |   |j  |   |r  
  51.   6  ,7  ,3  ,1  ,4  ,5  ,9  ,6  ,7  
  52.  |p  |   |   |   |   |i  |   |j  |r  
  53.   6  ,7  ,3  ,1  ,4  ,5  ,6  ,7  ,9  
  54.  |p  |   |   |   |   |   |i  |   |rj
  55. Quicksort: 0, 6
  56. Partition: 0, 6
  57.   6  ,7  ,3  ,1  ,4  ,5  ,6  ,7  ,9  
  58. i|pj |   |   |   |   |   |r  |   |  
  59.   6  ,7  ,3  ,1  ,4  ,5  ,6  ,7  ,9  
  60.  |pi |j  |   |   |   |   |r  |   |  
  61.   6  ,7  ,3  ,1  ,4  ,5  ,6  ,7  ,9  
  62.  |pi |   |j  |   |   |   |r  |   |  
  63.   6  ,3  ,7  ,1  ,4  ,5  ,6  ,7  ,9  
  64.  |p  |i  |   |j  |   |   |r  |   |  
  65.   6  ,3  ,1  ,7  ,4  ,5  ,6  ,7  ,9  
  66.  |p  |   |i  |   |j  |   |r  |   |  
  67.   6  ,3  ,1  ,4  ,7  ,5  ,6  ,7  ,9  
  68.  |p  |   |   |i  |   |j  |r  |   |  
  69.   6  ,3  ,1  ,4  ,5  ,6  ,7  ,7  ,9  
  70.  |p  |   |   |   |i  |   |rj |   |  
  71. Quicksort: 0, 4
  72. Partition: 0, 4
  73.   6  ,3  ,1  ,4  ,5  ,6  ,7  ,7  ,9  
  74. i|pj |   |   |   |r  |   |   |   |  
  75.   6  ,3  ,1  ,4  ,5  ,6  ,7  ,7  ,9  
  76. i|p  |j  |   |   |r  |   |   |   |  
  77.   3  ,6  ,1  ,4  ,5  ,6  ,7  ,7  ,9  
  78.  |pi |   |j  |   |r  |   |   |   |  
  79.   3  ,1  ,6  ,4  ,5  ,6  ,7  ,7  ,9  
  80.  |p  |i  |   |j  |r  |   |   |   |  
  81.   3  ,1  ,4  ,5  ,6  ,6  ,7  ,7  ,9  
  82.  |p  |   |i  |   |rj |   |   |   |  
  83. Quicksort: 0, 2
  84. Partition: 0, 2
  85.   3  ,1  ,4  ,5  ,6  ,6  ,7  ,7  ,9  
  86. i|pj |   |r  |   |   |   |   |   |  
  87.   3  ,1  ,4  ,5  ,6  ,6  ,7  ,7  ,9  
  88.  |pi |j  |r  |   |   |   |   |   |  
  89.   3  ,1  ,4  ,5  ,6  ,6  ,7  ,7  ,9  
  90.  |p  |i  |rj |   |   |   |   |   |  
  91. Quicksort: 0, 1
  92. Partition: 0, 1
  93.   3  ,1  ,4  ,5  ,6  ,6  ,7  ,7  ,9  
  94. i|pj |r  |   |   |   |   |   |   |  
  95.   1  ,3  ,4  ,5  ,6  ,6  ,7  ,7  ,9  
  96. i|p  |rj |   |   |   |   |   |   |  
  97. Quicksort: 0, -1
  98. Quicksort: 1, 1
  99. Quicksort: 3, 2
  100. Quicksort: 4, 4
  101. Quicksort: 6, 6
  102. Quicksort: 8, 8
  103. Array out: 1, 3, 4, 5, 6, 6, 7, 7, 9
  104. #ce
  105.  
  106. ;Programm:
  107. #include <String.au3>
  108. #include <Array.au3>
  109.  
  110. Dim $Array1[4] = [4, 3, 2, 1]
  111. Dim $Array2[9] = [6, 7, 3, 1, 4, 9, 5, 6, 7]
  112. Test($Array1)
  113. Test($Array2)
  114.  
  115. Func Test($Array)
  116.     ConsoleWrite("Array in:  ")
  117.     ArrayPrint($Array)
  118.     Quicksort($Array, 0, UBound($Array) - 1)
  119.     ConsoleWrite("Array out: ")
  120.     ArrayPrint($Array)
  121. EndFunc
  122.  
  123. Func Quicksort(ByRef $A, $p, $r)
  124.     ConsoleWrite("Quicksort: " & $p & ", " & $r & @CR)
  125.     If $p < $r Then
  126.         Local $q = Partition($A, $p, $r)
  127.         Quicksort($A, $p, $q - 1)
  128.         Quicksort($A, $q + 1, $r)
  129.     EndIf
  130. EndFunc
  131.  
  132. Func Partition(ByRef $A, $p, $r)
  133.     ConsoleWrite("Partition: " & $p & ", " & $r & @CR)
  134.     Local $x = $A[$r]
  135.     Local $i = $p - 1
  136.     For $j = $p To $r - 1
  137.         PrintStuff($A, $p, $r, $j, $i)
  138.         If $A[$j] <= $x Then
  139.             $i = $i + 1
  140.             Vertausche($A, $i, $j)
  141.         EndIf
  142.     Next
  143.     Vertausche($A, $i + 1, $r)
  144.     PrintStuff($A, $p, $r, $j, $i)
  145.     Return $i + 1
  146. EndFunc
  147.  
  148. Func Vertausche(ByRef $A, $i, $j)
  149.     Local $x = $A[$i]
  150.     $A[$i] = $A[$j]
  151.     $A[$j] = $x
  152. EndFunc
  153.  
  154. Func PrintStuff($A, $p, $r, $j, $i)
  155.     Local $x = "  "
  156.     Local $y = " |"
  157.     If $i = -1 Then $y = "i|"
  158.     For $n = 0 To UBound($A) - 1
  159.         Local $t = ""
  160.         Local $t2 = ""
  161.         $x &= $A[$n] & "  ,"
  162.         If $p = $n Then
  163.             $t2 &= "p"
  164.         Else
  165.             $t &= " "
  166.         EndIf
  167.         If $r = $n Then
  168.             $t2 &= "r"
  169.         Else
  170.             $t &= " "
  171.         EndIf
  172.         If $j = $n Then
  173.             $t2 &= "j"
  174.         Else
  175.             $t &= " "
  176.         EndIf
  177.         If $i = $n Then
  178.             $t2 &= "i"
  179.         Else
  180.             $t &= " "
  181.         EndIf
  182.         $y &= StringLeft($t2 & $t, 3) & "|"
  183.     Next
  184.     ConsoleWrite(StringLeft($x, StringLen($x) - 1) & @CR)
  185.     ConsoleWrite(StringLeft($y, StringLen($x) - 1) & @CR)
  186. EndFunc
  187.  
  188. Func ArrayPrint($A)
  189.     Local $x = ""
  190.     For $n = 0 To UBound($A) - 1
  191.         $x &= $A[$n] & ", "
  192.     Next
  193.     ConsoleWrite(StringLeft($x, StringLen($x) - 2) & @CR)
  194. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement