SortingAlgorithms/SelectionSort/SelectionSort.pde
2022-05-15 17:10:16 +02:00

94 lines
2.2 KiB
Plaintext

// final int FPS = 1000;
final float timeout = 2.5;
ArrayList<Integer> bars;
float barWidth;
int amount;
int current = 0, compare = 1;
boolean complete = false;
int recordIndex = 0;
float tenPercentHeight;
float ninetyPercentHeight;
int sortedFrameCountCapture = -1;
void setup() {
fullScreen(P3D);
// size(1280, 720, P3D);
tenPercentHeight = height * 0.1;
ninetyPercentHeight = height * 0.9;
amount = floor(random(100, 500));
bars = new ArrayList<Integer>();
for (int i = 0; i < amount; i++) {
bars.add(floor(random(tenPercentHeight, ninetyPercentHeight)));
}
barWidth = (float)width / amount;
// frameRate(amount);
frameRate(1000);
}
void draw() {
background(0);
stroke(0);
for (int i = 0; i < bars.size(); i++) {
if (i == current && !complete) {
fill(255,0,0);
} else if (i == compare && !complete) {
fill(0,255,0);
} else if (i == recordIndex && !complete) {
fill(0,0,255);
} else {
fill(255);
}
rect(i * barWidth, height-bars.get(i), barWidth, bars.get(i));
}
fill(255);
stroke(255);
textAlign(LEFT, TOP);
textSize(width*0.022);
text("Bar Amount: "+amount+"; FPS: "+round(frameRate)+";", 0, 0);
if (!complete) {
if (bars.get(compare) < bars.get(recordIndex)) {
recordIndex = compare;
}
compare++;
if (compare == bars.size()) {
final int tempValue = bars.get(current);
if (bars.get(recordIndex) < tempValue) {
bars.set(current, bars.get(recordIndex));
bars.set(recordIndex, tempValue);
}
current++;
compare = current+1;
recordIndex = current;
if (current == bars.size()-1) {
complete = true;
sortedFrameCountCapture = frameCount;
}
}
} else if (sortedFrameCountCapture != -1 && frameCount-sortedFrameCountCapture >= amount*timeout) {
sortedFrameCountCapture = -1;
current = 0;
compare = 1;
complete = false;
amount = floor(random(100, 500));
bars = new ArrayList<Integer>();
for (int i = 0; i < amount; i++) {
bars.add(floor(random(tenPercentHeight, ninetyPercentHeight)));
}
barWidth = (float)width / amount;
// frameRate(amount);
frameRate(1000);
}
}