-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathquick_sort_array_no_recursion.ahk
81 lines (66 loc) · 1.95 KB
/
quick_sort_array_no_recursion.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
;~ MyObj:=Main()
;~ ExitApp
;~ Main(){
;~ Test:={}
;~ Test[1]:={}
;~ Loop, 100000
;~ {
;~ Random, Rnd, 0x00, 0xFF
;~ Test[1,A_Index,"R"]:=Rnd, Test[1,A_Index,"G"]:=Rnd, Test[1,A_Index,"B"]:=Rnd, Test[1,A_Index,"A"]:=Rnd
;~ }
;~ q_sort(Test[1],"G")
;~ Return Test
;~ }
;;;;; Quick Sort Without Recursion ;;;;;
;;;;; https://www.codeproject.com/Articles/29467/Quick-Sort-Without-Recursion ;;;;;
;;;;; (c) The Code Project Open License (CPOL) ;;;;;
;;;;; https://www.codeproject.com/info/cpol10.aspx ;;;;;
q_sort(ByRef input,Dim){
stack:={}, stack.SetCapacity(500)
pivot:=0
pivotIndex:=1
leftIndex:=pivotIndex+1
rightIndex:=input.Length()
stack.Push(pivotIndex) ;Push always with left and right
stack.Push(rightIndex)
leftIndexOfSubSet:=rightIndexOfSubset:=0
While (stack.Length()>0)
{
rightIndexOfSubset:=stack.Pop() ;pop always with right and left
leftIndexOfSubSet:=stack.Pop()
leftIndex:=leftIndexOfSubSet+1
pivotIndex:=leftIndexOfSubSet
rightIndex:=rightIndexOfSubset
pivot:=input[pivotIndex,Dim]
If (leftIndex>rightIndex)
Continue
While (leftIndex<rightIndex)
{
While ((leftIndex<=rightIndex)&&(input[leftIndex,Dim]<=pivot))
leftIndex++ ;increment right to find the greater element than the pivot
While ((leftIndex<=rightIndex)&&(input[rightIndex,Dim]>=pivot))
rightIndex-- ;decrement right to find the smaller element than the pivot
If (rightIndex>=leftIndex) ;if right index is greater then only swap
SwapElement(input,leftIndex,rightIndex)
}
If (pivotIndex<=rightIndex)
If (input[pivotIndex,Dim]>input[rightIndex,Dim])
SwapElement(input,pivotIndex,rightIndex)
If (leftIndexOfSubSet<rightIndex)
{
stack.Push(leftIndexOfSubSet)
stack.Push(rightIndex-1)
}
If (rightIndexOfSubset>rightIndex)
{
stack.Push(rightIndex+1)
stack.Push(rightIndexOfSubset)
}
}
Return input
}
SwapElement(ByRef arr,left,right){
temp:=arr[left]
arr[left]:=arr[right]
arr[right]:=temp
}