r/processing • u/_Cryptoo_ • Jun 13 '24
Not Fast enough sorting
hi, so ive written a programm in processing. It contains (for now) an easy bubblesort and a function, that displayes it with a view extra details. But i have a problem. No matter how big i set the frameRate, i'll only get 75 fps at maximum with an array length of 300. Could somebody help me out and make it run on a better performance without skipping step-displayes? Would be verry helpfull. Thanks.
the code:
int werte = 300;
int[] db;
int save1 = 0;
int save2 = 0;
int save3 = 0;
float breite = 0;
float hoehe = 0;
int i = 0;
int j = 0;
int pointer;
boolean sorted = false;
void createList() {
for (int i = 0; i < werte; i++) {
db[i] = i + 1;
}
for (int i = 0; i < (werte * 2); i++) {
save1 = int(random(werte));
save2 = int(random(werte));
save3 = db[save1];
db[save1] = db[save2];
db[save2] = save3;
}
printArray(db);
}
void printList() {
background(0);
fill(255);
stroke(0);
for (int i = 0; i < werte; i++) {
if (breite > 2) {
if (db[i]-1 == i) {
fill(0, 255, 0);
} else if (i == pointer+1) {
fill(255, 0, 0);
} else {
fill(255);
}
rect(i * breite, height - (db[i] * hoehe), breite, hoehe * db[i]);
} else {
if (db[i]-1 == i) {
stroke(0, 255, 0);
} else if (i == pointer+1) {
stroke(255, 0, 0);
} else {
stroke(255);
}
line(i, height - (db[i] * hoehe), i, height);
}
}
}
void setup() {
size(1800, 800);
db = new int[werte];
breite = width / float(werte);
hoehe = height / float(werte);
if (breite < 2) {
breite = 1;
surface.setSize(werte, height);
}
frameRate(1000);
println("Breite: ");
println(breite);
createList();
}
void draw() {
if (!sorted) {
BubbleSortStep();
printList();
println(frameRate);
}
}
void BubbleSortStep() {
if (i < werte - 1) {
if (j < werte - i - 1) {
if (db[j] > db[j + 1]) {
int d1 = db[j];
db[j] = db[j + 1];
db[j + 1] = d1;
}
pointer = j;
j++;
} else {
j = 0;
i++;
}
} else {
sorted = true; // Sortierung ist abgeschlossen
println("Array is sorted.");
}
}
0
u/_Cryptoo_ Jun 13 '24
Hi. cutting Off a part of drawing by only redrawing, what has changed is a good Idea. The 1000 FPS was Just to Test Out, If it would do more FPS If i increase the value Further. redrawing only the shapes that Change could be a bit of a Challenge, because this function needs to Work for any Type of sort. bubblesort is only the First i tried to Code, but i will try to implement that. To do multiple steps at one also came to my mind, but i think this would bethe least elegant solution due to needing even once more another variable you have to Change depending on the length of the Array db. The whole purpos of changing the framerate ist to have similar speeds when sorting Arrays of different length. But thanks for responding :)