SortingAlgorithms/CombSort/CombSort.pde
2022-08-16 10:03:18 +02:00

81 lines
1.9 KiB
Plaintext

final int amount = 250;
final float timeout = 2.5;
ArrayList<Integer> bars;
float barWidth;
int current = 0;
int distance = -1;
boolean complete = false;
int sortedFrameCountCapture = -1;
float tenPercentHeight;
float ninetyPercentHeight;
void setup() {
fullScreen(P3D);
// size(1280, 720, P3D);
distance = amount-1;
tenPercentHeight = height * 0.1;
ninetyPercentHeight = height * 0.9;
bars = new ArrayList<Integer>();
for (int i = 0; i < amount; i++) {
bars.add(floor(random(tenPercentHeight, ninetyPercentHeight)));
}
barWidth = (float)width / amount;
frameRate(250);
}
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 == current + distance && !complete) {
fill(0,255,0);
} 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 && distance > 0) {
final int tempValue = bars.get(current);
if (tempValue > bars.get(current+distance)) {
bars.set(current, bars.get(current+distance));
bars.set(current+distance, tempValue);
}
current++;
if (current+distance == amount) {
current = 0;
distance--;
if (distance == 0) {
complete = true;
sortedFrameCountCapture = frameCount;
}
}
} else if (sortedFrameCountCapture != -1 && frameCount-sortedFrameCountCapture >= amount*timeout) {
sortedFrameCountCapture = -1;
current = 0;
complete = false;
distance = amount-1;
bars = new ArrayList<Integer>();
for (int i = 0; i < amount; i++) {
bars.add(floor(random(tenPercentHeight, ninetyPercentHeight)));
}
barWidth = (float)width / amount;
// frameRate(amount);
}
}