This is a bubble sort algorithm visualization code. I want rectangles to be swapped. However, I couldn't make it. When I run the program it waits a bit, max 2 seconds, then all rectangles are moving and it lasts 1 seconds or something. The problem is thread I guess, what should I do?
public void bubbleSort(Rectangle[] rectangles) throws InterruptedException {
int pass = 1;
for(int j = 0; j < rectangles.length-1; j++) {
for (int i = 0; i < rectangles.length - 1-j; i++) {
int pos = i;
if (rectangles[i].getHeight() > rectangles[i + 1].getHeight()) {
Runnable task = new Runnable() {
@Override
public void run() {
try {
System.out.println("opıjapısdjapsoıdj");
Platform.runLater(new Runnable() {
@Override
public void run() {
createAnimation(pos, pos + 1);
System.out.println("xxxxxxx");
}
});
Thread.sleep(2000);
} catch (InterruptedException exception) {
// nothing
}
}
};
new Thread(task).start();
Rectangle temp = rectangles[i];
rectangles[i] = rectangles[i + 1];
rectangles[i + 1] = temp;
}
}
}
}