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

86 lines
2.0 KiB
Plaintext

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