This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.pde
149 lines (133 loc) · 2.97 KB
/
quicksort.pde
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
class Quicksort<T extends Comparable<T>> extends SortingAlg<T>{
int currentDiv = -1;
int currentL = -1;
int currentR = -1;
boolean isInitialized, stillIn = false;
int divResult = -1;
int divI, divJ;
T divPivot;
Step next = null;
class Step{
int l;
int r;
Step(int l_, int r_){
l = l_;
r = r_;
}
}
ArrayList<Step> heap = new ArrayList<Step>();
Quicksort(T[] e){
elements = e;
count = elements.length;
heap.add(new Step(0,count-1));
}
void execute(){
quicksort(0,count-1);
}
void executeStep(){
if (heap.size() > 0){
if (next == null){
next = heap.get(heap.size()-1);
}
if (divResult == -1 && next.l<next.r){
currentL = next.l;
currentR = next.r;
currentDiv = divI;
divisionStep(next.l,next.r);
}else{
quicksortStep(next.l,next.r, divResult);
divResult = -1;
heap.remove(next);
next = null;
}
}else{
finished = true;
}
}
void quicksort(int left, int right){
if (left < right){
int div = divide(left,right);
quicksort(left, div-1);
quicksort(div+1, right);
}
}
void quicksortStep(int left, int right, int div){
if (left < right){
currentDiv = div;
currentL = left;
currentR = right;
heap.add(new Step(left, div-1));
heap.add(new Step(div+1, right));
}
}
int divide(int left, int right){
int i = left;
int j = right-1;
T pivot = elements[right];
do {
while(elements[i].compareTo(pivot) <= 0 && i < right){
compCount++;
i++;
}
while(elements[j].compareTo(pivot) >= 0 && j > left){
compCount++;
j--;
}
if(i<j){
swap(i,j);
}
} while(i<j);
compCount++;
if (elements[i].compareTo(pivot) > 0){
swap(i,right);
}
return i;
}
void divisionStep(int left, int right){
if (!isInitialized){
divI = left;
divJ = right - 1;
divPivot = elements[right];
isInitialized = true;
}
compCount++;
if(divI < right && elements[divI].compareTo(divPivot) <= 0){
divI++;
stillIn = true;
return;
}
compCount++;
if(divJ > left && elements[divJ].compareTo(divPivot) >= 0){
divJ--;
stillIn = true;
return;
}
if(divI<divJ){
swap(divI,divJ);
return;
}
compCount++;
if (elements[divI].compareTo(divPivot) > 0){
swap(divI,right);
}
divResult = divI;
isInitialized = false;
}
color colorForIndex(int i){
color r = color(360);
if (i == divI || i == divJ){
r = color(180, 100, 100);
}
if (i == currentL || i == currentR){
r = color(300,50,100);
}
if (i == currentR){
stroke(200,100,100);
line(0,height-(float)elements[i],width,height-(float)elements[i]);
}
return r;
}
float freqToPlay(){
return (float) elements[currentL];
}
}