We are about to switch to a new forum software. Until then we have removed the registration on this forum.
How would I implement a "simulate all" button in my program?
What my grid looks like:
When I hit simulate all, the algorithms will sort every amount of numbers and display the amount of seconds in each cell.
Code so far:
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int n = 99;
int console = 2;
int min, tmp, i, j, min_id, data, k, temp, start, ellapsed;
int[] array = new int [n];
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void setup() {
size(661, 500);
background(255);
bubbleSort(array, n);
selectionSort(array, n);
insertionSort(array, n);
quickSort(0, n - 1);
reset();
initArr();
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void draw() {
drawGrid();
labels();
keyPressed();
for (int j = 1; j < 5; j ++) {
for (int i = 1; i < 6; i ++) {
if (mouseX > i * 110 && mouseX < i * 110 + 110 && mouseY > j * 80 && mouseY < j * 80 + 80) {
text("Time:" + " " + (float)ellapsed/1000, i * 110 + 23, j * 80 + 45);
} else {
text("", i * 110 + 23, j * 80 + 45);
}
}
}
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void initArr() {
for (i = 0; i < n; i ++) {
array[i] = (int)random(1, 10);
}
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void mouseReleased() {
for (int j = 1; j < 5; j ++) {
for (int i = 1; i < 6; i ++) {
if (mouseX > i * 110 && mouseX < i * 110 + 110 && mouseY > j * 80 && mouseY < j * 80 + 80) {
println("X:" + " " + i + " " + "Y:" + " " + j);
if (i == 1 && j == 1) {
initArr();
start = millis();
bubbleSort(array, n);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 2 && j == 1) {
int n = 999;
array = new int[n]; // Brand new array.
initArr();
start = millis();
bubbleSort(array, n);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 3 && j == 1) {
int n = 9999;
array = new int[n]; // Brand new array.
initArr();
start = millis();
bubbleSort(array, n);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 4 && j == 1) {
int n = 99999;
array = new int[n]; // Brand new array.
initArr();
start = millis();
bubbleSort(array, n);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 5 && j == 1) {
int n = 999999;
array = new int[n]; // Brand new array.
initArr();
start = millis();
bubbleSort(array, n);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 1 && j == 2) {
initArr();
start = millis();
selectionSort(array, n);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 2 && j == 2) {
int n = 999;
array = new int[n]; // Brand new array.
initArr();
start = millis();
selectionSort(array, n);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 3 && j == 2) {
int n = 9999;
array = new int[n]; // Brand new array.
initArr();
start = millis();
selectionSort(array, n);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 4 && j == 2) {
int n = 99999;
array = new int[n]; // Brand new array.
initArr();
start = millis();
selectionSort(array, n);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 5 && j == 2) {
int n = 999999;
array = new int[n]; // Brand new array.
initArr();
start = millis();
selectionSort(array, n);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 1 && j == 3) {
initArr();
start = millis();
insertionSort(array, n);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 2 && j == 3) {
int n = 999;
array = new int[n]; // Brand new array.
initArr();
start = millis();
insertionSort(array, n);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 3 && j == 3) {
int n = 9999;
array = new int[n]; // Brand new array.
initArr();
start = millis();
insertionSort(array, n);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 4 && j == 3) {
int n = 99999;
array = new int[n]; // Brand new array.
initArr();
start = millis();
insertionSort(array, n);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 5 && j == 3) {
int n = 999999;
array = new int[n]; // Brand new array.
initArr();
start = millis();
insertionSort(array, n);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 1 && j == 4) {
initArr();
start = millis();
quickSort(0, n - 1);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 2 && j == 4) {
int n = 999;
array = new int[n]; // Brand new array.
initArr();
start = millis();
quickSort(0, n - 1);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 3 && j == 4) {
int n = 9999;
array = new int[n]; // Brand new array.
initArr();
start = millis();
quickSort(0, n - 1);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 4 && j == 4) {
int n = 99999;
array = new int[n]; // Brand new array.
initArr();
start = millis();
quickSort(0, n - 1);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
} else if (i == 5 && j == 4) {
int n = 999999;
array = new int[n]; // Brand new array.
initArr();
start = millis();
quickSort(0, n - 1);
ellapsed = millis() - start;
println("Time:" + " " + (float)ellapsed/1000);
}
simulateAll();
}
}
}
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void labels() {
for (k = 0; k < 3; k ++) {
fill(255);
if (mouseX > k * 183.5 + 110 && mouseX < k * 183.5 + 293.5 && mouseY > 400 && mouseY < 480) {
fill(205, 205, 180);
}
rect(k * 183.5 + 110, 400, 183.5, 80);
}
if (mousePressed && mouseX > 110 && mouseX < 293.5 && mouseY > 400 && mouseY < 480) {
simulateAll();
}
if (mousePressed && mouseX > 293.5 && mouseX < 477 && mouseY > 400 && mouseY < 480) {
reset();
}
fill(0);
text("ALGORITHM", 20, 45);
text("BUBBLE", 20, 125);
text("SELECTION", 20, 205);
text("INSERTION", 20, 285);
text("QUICK", 20, 365);
text("100", 150, 45);
text("1000", 260, 45);
text("10000", 365, 45);
text("100000", 470, 45);
text("1000000", 575, 45);
text("SIMULATE ALL", 160, 445);
text("RESET", 365, 445);
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void drawGrid() {
for (j = 1; j < 5; j ++) { //columns
for (i = 1; i < 6; i ++) { // rows
fill(255);
if (i != 0 && j != 0 && j <= 4 && i <= 5) {
if (mouseX > i * 110 && mouseX < i * 110 + 110 && mouseY > j * 80 && mouseY < j * 80 + 80)
fill(255, 200, 200);
}
rect(i * 110, j * 80, 110, 80);
}
}
for (k = 0; k < 5; k ++) {
fill(255);
if (mouseY > k * 80 && mouseY < k * 80 + 80 && mouseY > 80) {
fill(72, 118, 255);
}
rect(0, k * 80, 110, 80);
}
for (k = 0; k < 5; k ++) {
fill(255);
if (mouseX > k * 110 + 110 && mouseX < k * 110 + 220 && mouseY < 400) {
fill(0, 238, 0);
}
rect(k * 110 + 110, 0, 110, 80);
}
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void bubbleSort(int array[], int n) {
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
if (console == 1) {
println("SORTING ELEMENT" + " " + i);
if (n == 99) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
if (n == 999) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
if (n == 9999) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
if (n == 99999) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
if (n == 999999) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
}
}
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void selectionSort (int array[], int n) {
for (i = 0; i < n - 1; i ++) {
min = array[i];
for (j = i + 1; j < n; j ++) {
if (array[j] < min) {
min = array[j];
min_id = j;
}
}
tmp = array[i];
array[i] = array[min_id];
array[min_id] = tmp;
if (console == 1) {
println("SORTING ELEMENT" + " " + i);
if (n == 99) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
if (n == 999) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
if (n == 9999) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
if (n == 99999) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
if (n == 999999) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
}
}
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void insertionSort(int[] array, int n) {
for (i = 1; i < n; i++)
{
// a temporary copy of the current element
temp = array[i];
// find the position for insertion
for (j = i; j > 0; j--)
{
if (array[j - 1] < temp)
break;
// shift the sorted part to right
array[j] = array[j - 1];
}
// insert the current element
array[j] = temp;
if (console == 1) {
println("SORTING ELEMENT" + " " + i);
if (n == 99) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
if (n == 999) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
if (n == 9999) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
if (n == 99999) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
if (n == 999999) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
}
}
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void quickSort(int low, int high) {
i = low;
j = high;
// calculate pivot number, I am taking pivot as middle index number
int pivot = array[low+(high-low)/2]; // Divide into two arrays
while (i <= j) {
/**
* In each iteration, we will identify a number from left side which
* is greater then the pivot value, and also we will identify a number
* from right side which is less then the pivot value. Once the search
* is done, then we exchange both numbers.
*/
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
//move index to next position on both sides
i++;
j--;
}
}
if (console == 1) {
println("SORTING ELEMENT" + " " + i);
if (n == 99) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
if (n == 999) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
if (n == 9999) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
if (n == 99999) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
if (n == 999999) {
println(100 * i / n + 100 / n + "%" + " " + "done");
}
}
// call quickSort() method recursively
if (low < j)
quickSort(low, j);
if (i < high)
quickSort(i, high);
}
void simulateAll() {
bubbleSort(array, n);
selectionSort(array, n);
insertionSort(array, n);
quickSort(0, n - 1);
}
void reset() {
n = 99;
for (int j = 1; j < 5; j ++) {
for (int i = 1; i < 6; i ++) {
text("" + " " + (float)ellapsed/1000, i * 110 + 23, j * 80 + 45);
}
}
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP)
console = 1;
else
console = 2;
}
}
Answers
@Chrisir
Just call all the single sections in function mouseReleased at once.
Instead of ellapsed fill a grid with the millis numbers that correspond with your visible grid and show the results there
@Chrisir when you say single sections do you mean:
Because that doesn't work
This is a section
Just put all vars in a 2D array / grid
Like this? @Chrisir :
Because when I put all the sections in the simulateAll() function it says that there are duplicate variables due to the multiple int n's @Chrisir
I told you what to do with your variables
I don't know how to do 2D arrays yet @Chrisir
Read the tutorial on two dimensional arrays then
https://www.processing.org/tutorials/2darray/
So I would make this:
int[][] vars = { {99}, {999}, {9999}, {99999}, {999999} };
Then, how would I put these in the function @Chrisir?
you said there are duplicate variables
let's say elapsed (or ellapsed): you need this differently for each cell.
so before setup()
int[][] elapsed = new int [5][4];
same for
start
of course!!!int[][] start= new int [5][4];
and result:
String[][] result= new String [5][4];
and the function:
there is still a LOT to do, but this is the idea.
To fill the grid, use
result
here is the output using result:
Alright, how is this: @Chrisir
I don't know. Does it work?
why n?
shouldn't you use as many numbers as you need, so 100, 1000 or ... ?
It is working, the only problem is that the text starts off like this:
What do you mean?
Color?
Always 51?
can you show your entire code please?
It just starts at 51, and when clicked it changes:
Hello again,
I don't see the entire code.
I see your old sketch and the function simulateAll().
you want to check if the button simulate All has been clicked and then run the function simulateAll(). You have this. This is normally done in the function mousePressed().
As said, before running initArr(); anywhere in the function simulateAll() you need to set n. Or am I wrong?
You don't have to call keyPressed in draw, it gets called automatically.
What are you suggesting me to do?
did you join the two parts of the sketch? Is it one sketch?
Does it work?
I think in every section
you need
But then I get a duplicate variable error
It kind of works, the grid starts with a random number when it should start blank, and change when clicked, when simulate all is pressed only 4 sections run.
you get this error just with n=99999; ??????
without
int
?in setup instead of
start with
Alright, got it, now I need to find out how to use the setup button, what would I do/put in the reset function I have near the end of my code?
new version:
when you click the button simulate all he starts by doing each cell separately - ONE BY ONE. Very nice. You can see how the grid is filling!!!
He skips the last column.
He still crashes on my computer due to time out in the last but one column, second row (it takes too long):
reset could do this:
in my version I killed most of the mouse behaviour
sorry for this
I have to go. Bye.
Alright, thx for all the help btw
When you use initArr() and n = 99 all the time, the last 99999 (-99) numbers in the array are just 0 / zeros. That's why he might be pretty darn fast on your computer, because there are only 99 random numbers and then many many zeros in your code - so he doesn't have to swap numbers and to sort really.
On my machine e.g. he needs first line, in the last but one column approx. 12 seconds!!!
you're welcome!
"completely new" discussion here:
https://forum.processing.org/two/discussion/24471/why-does-the-simulateall-button-not-work-after-i-reset-the-grid