-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathPivotArraySort.ahk
127 lines (121 loc) · 3.34 KB
/
PivotArraySort.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
PivotSortArray(Array, Order="A") {
;Order A: Ascending, D: Descending, R: Reverse
MaxIndex := ObjMaxIndex(Array)
If (Order = "R") {
count := 0
Loop, % MaxIndex
ObjInsert(Array, ObjRemove(Array, MaxIndex - count++))
Return
}
Partitions := "|" ObjMinIndex(Array) "," MaxIndex
Loop {
comma := InStr(this_partition := SubStr(Partitions, InStr(Partitions, "|", False, 0)+1), ",")
spos := pivot := SubStr(this_partition, 1, comma-1) , epos := SubStr(this_partition, comma+1)
if (Order = "A") {
Loop, % epos - spos {
if (Array[pivot] > Array[A_Index+spos])
ObjInsert(Array, pivot++, ObjRemove(Array, A_Index+spos))
}
} else {
Loop, % epos - spos {
if (Array[pivot] < Array[A_Index+spos])
ObjInsert(Array, pivot++, ObjRemove(Array, A_Index+spos))
}
}
Partitions := SubStr(Partitions, 1, InStr(Partitions, "|", False, 0)-1)
if (pivot - spos) > 1 ;if more than one elements
Partitions .= "|" spos "," pivot-1 ;the left partition
if (epos - pivot) > 1 ;if more than one elements
Partitions .= "|" pivot+1 "," epos ;the right partition
} Until !Partitions
}
PivotSortArray2D(ByRef _array, 2D, sortByItem) { ;1 means array is one dimensional, 2 means array is two dimensional
sorted := []
size := _array.MaxIndex()
if (2D = True)
{
loop % size
{
min := _array[1][sortByItem]
for index,item in _array
{
if (item[sortByItem] <= min)
{
temp := index
min := item[sortByItem]
}
}
sorted.Push(_array.RemoveAt(temp))
}
}
else
{
loop % size
{
min := _array[1]
for index,item in _array
{
if (item <= min)
{
temp := index
min := item
}
}
sorted.Push(_array.RemoveAt(temp))
}
}
return sorted
}
sort(ByRef array, element) {
length := array.MaxIndex()
pos := 1
swapped := 0
loop
{
if (pos >= length)
{
pos := 1
if (swapped = 0)
break
swapped := 0
}
if (element = 0)
{
if (array[pos] > array[pos + 1])
{
swap(array, pos, pos + 1)
pos++
swapped := 1
continue
}
}
else
{
if (array[pos][element] > array[pos + 1][element])
{
swap(array, pos, pos + 1)
pos++
swapped := 1
continue
}
}
pos++
}
}
SimpleSortArray(Array) {
Static Delim := Chr(1)
SortVar := ""
For index, item In Array
SortVar .= item . Delim . index . "`n"
Sort, SortVar, N
NewArray := {}
Loop, Parse, SortVar, `n
{
If (A_LoopField)
{
temp := StrSplit(A_LoopField,Delim)
NewArray.Push(Array[temp[2]])
}
}
return NewArray
}