How would I implement a "simulate all" button in my program?

How would I implement a "simulate all" button in my program?

What my grid looks like: Screen Shot 2017-10-06 at 11.01.13 AM

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

  • 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:

      bubbleSort(array, n);
      selectionSort(array, n);
      insertionSort(array, n);
      quickSort(0, n - 1);
    
  • Because that doesn't work

  • This is a section

          initArr();
          start = millis();
          quickSort(0, n - 1);
          ellapsed = millis() - start;
          println("Time:" + " " + (float)ellapsed/1000);
    

    Just put all vars in a 2D array / grid

  • edited October 2017

    Like this? @Chrisir :

    void simulateAll() {
      initArr();
      start = millis();
      bubbleSort(array, n);
      ellapsed = millis() - start;
      println("Time:" + " " + (float)ellapsed/1000);
    
      initArr();
      start = millis();
      selectionSort(array, n);
      ellapsed = millis() - start;
      println("Time:" + " " + (float)ellapsed/1000);
    
      initArr();
      start = millis();
      insertionSort(array, n);
      ellapsed = millis() - start;
      println("Time:" + " " + (float)ellapsed/1000);
    
      initArr();
      start = millis();
      quickSort(0, n - 1);
      ellapsed = millis() - start;
      println("Time:" + " " + (float)ellapsed/1000);
    }
    
  • 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

  • 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:

    void simulateAll() {
    
      // first column in grid!!!!! 
      int x=0; //column
      int y=0; // row
      initArr();
      start[x][y] = millis();
      bubbleSort(array, 100);
      elapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)elapsed[x][y]/1000);
      result[x][y] = str (  (float)elapsed[x][y]/1000 ); 
    
      y=1; 
      initArr();
      start[x][y] = millis();
      selectionSort(array, 100);
      elapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)elapsed[x][y]/1000);
      result[x][y] = str (  (float)elapsed[x][y]/1000 ); 
    
      // ......     etc.     ..........
      // .....................
      y=2;
      initArr();
      start = millis();
      insertionSort(array, 100);
      ellapsed = millis() - start;
      println("Time:" + " " + (float)ellapsed/1000);
    
      y=3;
      initArr();
      start = millis();
      quickSort(0, 100 - 1);
      ellapsed = millis() - start;
      println("Time:" + " " + (float)ellapsed/1000);
    
      // 2nd column !!!!!!!
      x=1; 
      y=0;
      // ......................
    }
    

    there is still a LOT to do, but this is the idea.

    To fill the grid, use result

  • edited October 2017

    here is the output using result:

    String[][] frame= new String [5][4];
    
    String[][] result= new String [5][4];
    
    void setup() {
    
      size(880, 880);
    
      // just dummy data - we want your real results here
      for (int x=0; x < result.length; x++) {
        for (int y=0; y < result[0].length; y++) {
          result[x][y] = str(millis()+random(1111));
        }
      }
    
      // the frame in red - pre-init 
      for (int x=0; x < result.length; x++) {
        for (int y=0; y < result[0].length; y++) {
          frame[x][y] = "";
        }
      }
    
      // init the frame
      frame[0] [0] ="Algo";
      frame[0] [1] ="Bub";
      frame[0] [2] ="Sel";
    
    
      frame[1] [0] ="100";
      frame[2] [0] ="1000";
      frame[3] [0] ="10000";
    
      background(0);
    }
    
    void draw() {
      background(0);
    
      fill(255); 
      for (int x=0; x < result.length; x++) {
        for (int y=0; y < result[0].length; y++) {
          text ( result[x][y], x*99+100, y * 99 +100+40);
        }
      }
    
      fill(255, 0, 0); 
      for (int x=0; x < result.length; x++) {
        for (int y=0; y < result[0].length; y++) {
          text ( frame[x][y], x*99+20, y * 99 +40);
        }
      }
    }
    
  • Alright, how is this: @Chrisir

    void simulateAll() {
    
      int x = 0;
      int y = 0;
      initArr();
      start[x][y] = millis();
      bubbleSort(array, n);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[n]; // Brand new array.
      x = 1;
      initArr();
      start[x][y] = millis();
      bubbleSort(array, 999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[n]; // Brand new array.
      x = 2;
      initArr();
      start[x][y] = millis();
      bubbleSort(array, 9999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[n]; // Brand new array.
      x = 3;
      initArr();
      start[x][y] = millis();
      bubbleSort(array, 99999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[n]; // Brand new array.
      x = 4;
      initArr();
      start[x][y] = millis();
      bubbleSort(array, 999999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      x = 0;
      y = 1;
      initArr();
      start[x][y] = millis();
      selectionSort(array, n);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[n]; // Brand new array.
      x = 1;
      y = 1;
      initArr();
      start[x][y] = millis();
      selectionSort(array, 999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[n]; // Brand new array.
      x = 2;
      y = 1;
      initArr();
      start[x][y] = millis();
      selectionSort(array, 9999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[n]; // Brand new array.
      x = 3;
      y = 1;
      initArr();
      start[x][y] = millis();
      selectionSort(array, 99999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[n]; // Brand new array.
      x = 4;
      y = 1;
      initArr();
      start[x][y] = millis();
      selectionSort(array, 999999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      x = 0;
      y = 2;
      initArr();
      start[x][y] = millis();
      insertionSort(array, n);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[n]; // Brand new array.
      x = 1;
      y = 2;
      initArr();
      start[x][y] = millis();
      insertionSort(array, 999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[n]; // Brand new array.
      x = 2;
      y = 2;
      initArr();
      start[x][y] = millis();
      insertionSort(array, 9999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[n]; // Brand new array.
      x = 3;
      y = 2;
      initArr();
      start[x][y] = millis();
      insertionSort(array, 99999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[n]; // Brand new array.
      x = 4;
      y = 2;
      initArr();
      start[x][y] = millis();
      insertionSort(array, 999999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      x = 0;
      y = 3;
      initArr();
      start[x][y] = millis();
      quickSort(0, n - 1);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[n]; // Brand new array.
      x = 1;
      y = 3;
      initArr();
      start[x][y] = millis();
      quickSort(0, 999 - 1);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[n]; // Brand new array.
      x = 2;
      y = 3;
      initArr();
      start[x][y] = millis();
      quickSort(0, 9999 - 1);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[n]; // Brand new array.
      x = 3;
      y = 3;
      initArr();
      start[x][y] = millis();
      quickSort(0, 99999 - 1);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[n]; // Brand new array.
      x = 4;
      y = 3;
      initArr();
      start[x][y] = millis();
      quickSort(0, 999999 - 1);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    }
    
  • I don't know. Does it work?

  • why n?

    array = new int[n]; // Brand new array 
    

    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:

    Screen Shot 2017-10-07 at 1.47.20 PM

  • What do you mean?

    Color?

    Always 51?

  • can you show your entire code please?

  • It just starts at 51, and when clicked it changes:

    int n = 99;
    int console = 2;
    int min, tmp, i, j, min_id, data, k, temp;
    int[] array = new int [n];
    int[][] ellapsed = new int [5][4];
    int[][] start = new int [5][4];
    String[][] result= new String [5][4];
    String[][] frame= new String [5][4];
    
    void setup() {
    
      size(661, 500);
      background(255);
    
      bubbleSort(array, n);
      selectionSort(array, n);
      insertionSort(array, n);
      quickSort(0, n - 1);
    
      reset();
    
      initArr();
    
      for (int x=0; x < result.length; x++) {
        for (int y=0; y < result[0].length; y++) {
          result[x][y] = str(millis() - 422);
        }
      }
    }
    
    //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    void draw() {
    
      drawGrid();
      labels();
    
      keyPressed();
    
      fill(0); 
      for (int x = 0; x < result.length; x++) {
        for (int y = 0; y < result[0].length; y++) {
          text(result[x][y], x * 110 + 140, y * 80 + 125);
        }
      }
    }
    
    //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    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);
            int x = 0; //column
            int y = 0; // row
    
            if (i == 1 && j == 1) {
              initArr();
              start[x][y] = millis();
              bubbleSort(array, n);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 2 && j == 1) {
              int n = 999;
              array = new int[n]; // Brand new array.
              x = 1;
              initArr();
              start[x][y] = millis();
              bubbleSort(array, n);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 3 && j == 1) {
              int n = 9999;
              array = new int[n]; // Brand new array.
              x = 2;
              initArr();
              start[x][y] = millis();
              bubbleSort(array, n);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 4 && j == 1) {
              int n = 99999;
              array = new int[n]; // Brand new array.
              x = 3;
              initArr();
              start[x][y] = millis();
              bubbleSort(array, n);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 5 && j == 1) {
              int n = 999999;
              array = new int[n]; // Brand new array.
              x = 4;
              initArr();
              start[x][y] = millis();
              bubbleSort(array, n);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 1 && j == 2) {
              x = 0;
              y = 1;
              initArr();
              start[x][y] = millis();
              selectionSort(array, n);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 2 && j == 2) {
              int n = 999;
              array = new int[n]; // Brand new array.
              x = 1;
              y = 1;
              initArr();
              start[x][y] = millis();
              selectionSort(array, n);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 3 && j == 2) {
              int n = 9999;
              array = new int[n]; // Brand new array.
              x = 2;
              y = 1;
              initArr();
              start[x][y] = millis();
              selectionSort(array, n);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 4 && j == 2) {
              int n = 99999;
              array = new int[n]; // Brand new array.
              x = 3;
              y = 1;
              initArr();
              start[x][y] = millis();
              selectionSort(array, n);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 5 && j == 2) {
              int n = 999999;
              array = new int[n]; // Brand new array.
              x = 4;
              y = 1;
              initArr();
              start[x][y] = millis();
              selectionSort(array, n);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 1 && j == 3) {
              x = 0;
              y = 2;
              initArr();
              start[x][y] = millis();
              insertionSort(array, n);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 2 && j == 3) {
              int n = 999;
              array = new int[n]; // Brand new array.
              x = 1;
              y = 2;
              initArr();
              start[x][y] = millis();
              insertionSort(array, n);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 3 && j == 3) {
              int n = 9999;
              array = new int[n]; // Brand new array.
              x = 2;
              y = 2;
              initArr();
              start[x][y] = millis();
              insertionSort(array, n);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 4 && j == 3) {
              int n = 99999;
              array = new int[n]; // Brand new array.
              x = 3;
              y = 2;
              initArr();
              start[x][y] = millis();
              insertionSort(array, n);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 5 && j == 3) {
              int n = 999999;
              array = new int[n]; // Brand new array.
              x = 4;
              y = 2;
              initArr();
              start[x][y] = millis();
              insertionSort(array, n);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 1 && j == 4) {
              x = 0;
              y = 3;
              initArr();
              start[x][y] = millis();
              quickSort(0, n - 1);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 2 && j == 4) {
              int n = 999;
              array = new int[n]; // Brand new array.
              x = 1;
              y = 3;
              initArr();
              start[x][y] = millis();
              quickSort(0, n - 1);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 3 && j == 4) {
              int n = 9999;
              array = new int[n]; // Brand new array.
              x = 2;
              y = 3;
              initArr();
              start[x][y] = millis();
              quickSort(0, n - 1);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 4 && j == 4) {
              int n = 99999;
              array = new int[n]; // Brand new array.
              x = 3;
              y = 3;
              initArr();
              start[x][y] = millis();
              quickSort(0, n - 1);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            } else if (i == 5 && j == 4) {
              int n = 999999;
              array = new int[n]; // Brand new array.
              x = 4;
              y = 3;
              initArr();
              start[x][y] = millis();
              quickSort(0, n - 1);
              ellapsed[x][y] = millis() - start[x][y];
              println("Time:" + " " + (float)ellapsed[x][y]/1000);
              result[x][y] = str ((float)ellapsed[x][y]/1000 );
            }
          }
        }
      }
      if (mouseX > 110 && mouseX < 293.5 && mouseY > 400 && mouseY < 480) {
        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 > 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() {
    
          int x = 0;
          int y = 0;
          initArr();
          start[x][y] = millis();
          bubbleSort(array, n);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          array = new int[999]; // Brand new array.
          x = 1;
          initArr();
          start[x][y] = millis();
          bubbleSort(array, 999);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          array = new int[9999]; // Brand new array.
          x = 2;
          initArr();
          start[x][y] = millis();
          bubbleSort(array, 9999);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          array = new int[99999]; // Brand new array.
          x = 3;
          initArr();
          start[x][y] = millis();
          bubbleSort(array, 99999);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          array = new int[999999]; // Brand new array.
          x = 4;
          initArr();
          start[x][y] = millis();
          bubbleSort(array, 999999);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          x = 0;
          y = 1;
          initArr();
          start[x][y] = millis();
          selectionSort(array, n);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          array = new int[999]; // Brand new array.
          x = 1;
          y = 1;
          initArr();
          start[x][y] = millis();
          selectionSort(array, 999);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          array = new int[9999]; // Brand new array.
          x = 2;
          y = 1;
          initArr();
          start[x][y] = millis();
          selectionSort(array, 9999);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          array = new int[99999]; // Brand new array.
          x = 3;
          y = 1;
          initArr();
          start[x][y] = millis();
          selectionSort(array, 99999);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          array = new int[999999]; // Brand new array.
          x = 4;
          y = 1;
          initArr();
          start[x][y] = millis();
          selectionSort(array, 999999);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          x = 0;
          y = 2;
          initArr();
          start[x][y] = millis();
          insertionSort(array, n);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          array = new int[999]; // Brand new array.
          x = 1;
          y = 2;
          initArr();
          start[x][y] = millis();
          insertionSort(array, 999);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          array = new int[9999]; // Brand new array.
          x = 2;
          y = 2;
          initArr();
          start[x][y] = millis();
          insertionSort(array, 9999);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          array = new int[99999]; // Brand new array.
          x = 3;
          y = 2;
          initArr();
          start[x][y] = millis();
          insertionSort(array, 99999);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          array = new int[999999]; // Brand new array.
          x = 4;
          y = 2;
          initArr();
          start[x][y] = millis();
          insertionSort(array, 999999);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          x = 0;
          y = 3;
          initArr();
          start[x][y] = millis();
          quickSort(0, n - 1);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          array = new int[999]; // Brand new array.
          x = 1;
          y = 3;
          initArr();
          start[x][y] = millis();
          quickSort(0, 999 - 1);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          array = new int[9999]; // Brand new array.
          x = 2;
          y = 3;
          initArr();
          start[x][y] = millis();
          quickSort(0, 9999 - 1);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          array = new int[99999]; // Brand new array.
          x = 3;
          y = 3;
          initArr();
          start[x][y] = millis();
          quickSort(0, 99999 - 1);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
          array = new int[999999]; // Brand new array.
          x = 4;
          y = 3;
          initArr();
          start[x][y] = millis();
          quickSort(0, 999999 - 1);
          ellapsed[x][y] = millis() - start[x][y];
          println("Time:" + " " + (float)ellapsed[x][y]/1000);
          result[x][y] = str ((float)ellapsed[x][y]/1000 );
        }
    
        void reset() {
          n = 99;
        }
    
        void keyPressed() {
          if (key == CODED) {
            if (keyCode == UP) 
              console = 1;
            else 
            console = 2;
            if (keyCode == DOWN)
              console = 2;
          }
        }
    
  • 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?

    void simulateAll() {
    
      int x = 0;
      int y = 0;
      initArr();
      start[x][y] = millis();
      bubbleSort(array, 99);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[999]; // Brand new array.
      x = 1;
      initArr();
      start[x][y] = millis();
      bubbleSort(array, 999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[9999]; // Brand new array.
      x = 2;
      initArr();
      start[x][y] = millis();
      bubbleSort(array, 9999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[99999]; // Brand new array.
      x = 3;
      initArr();
      start[x][y] = millis();
      bubbleSort(array, 99999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[999999]; // Brand new array.
      x = 4;
      initArr();
      start[x][y] = millis();
      bubbleSort(array, 999999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      x = 0;
      y = 1;
      initArr();
      start[x][y] = millis();
      selectionSort(array, 99);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[999]; // Brand new array.
      x = 1;
      y = 1;
      initArr();
      start[x][y] = millis();
      selectionSort(array, 999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[9999]; // Brand new array.
      x = 2;
      y = 1;
      initArr();
      start[x][y] = millis();
      selectionSort(array, 9999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[99999]; // Brand new array.
      x = 3;
      y = 1;
      initArr();
      start[x][y] = millis();
      selectionSort(array, 99999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[999999]; // Brand new array.
      x = 4;
      y = 1;
      initArr();
      start[x][y] = millis();
      selectionSort(array, 999999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      x = 0;
      y = 2;
      initArr();
      start[x][y] = millis();
      insertionSort(array, n);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[999]; // Brand new array.
      x = 1;
      y = 2;
      initArr();
      start[x][y] = millis();
      insertionSort(array, 999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[9999]; // Brand new array.
      x = 2;
      y = 2;
      initArr();
      start[x][y] = millis();
      insertionSort(array, 9999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[99999]; // Brand new array.
      x = 3;
      y = 2;
      initArr();
      start[x][y] = millis();
      insertionSort(array, 99999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[999999]; // Brand new array.
      x = 4;
      y = 2;
      initArr();
      start[x][y] = millis();
      insertionSort(array, 999999);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      x = 0;
      y = 3;
      initArr();
      start[x][y] = millis();
      quickSort(0, n - 1);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[999]; // Brand new array.
      x = 1;
      y = 3;
      initArr();
      start[x][y] = millis();
      quickSort(0, 999 - 1);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[9999]; // Brand new array.
      x = 2;
      y = 3;
      initArr();
      start[x][y] = millis();
      quickSort(0, 9999 - 1);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[99999]; // Brand new array.
      x = 3;
      y = 3;
      initArr();
      start[x][y] = millis();
      quickSort(0, 99999 - 1);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    
      array = new int[999999]; // Brand new array.
      x = 4;
      y = 3;
      initArr();
      start[x][y] = millis();
      quickSort(0, 999999 - 1);
      ellapsed[x][y] = millis() - start[x][y];
      println("Time:" + " " + (float)ellapsed[x][y]/1000);
      result[x][y] = str ((float)ellapsed[x][y]/1000 );
    }
    
  • did you join the two parts of the sketch? Is it one sketch?

    Does it work?

    I think in every section

      array = new int[99999]; // Brand new array.
      x = 3;
      y = 3;
      initArr();
      start[x][y] = millis();
    

    you need

      n=99999;
      array = new int[n]; // Brand new array.
      x = 3;
      y = 3;
      initArr(); // uses n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      start[x][y] = millis();
    
  • 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.

  • edited October 2017

    duplicate variable error

    you get this error just with n=99999; ??????

    without int?

  • in setup instead of

    for (int x=0; x < result.length; x++) {
        for (int y=0; y < result[0].length; y++) {
          result[x][y] = str(millis() - 422);
        }
      }
    

    start with

    for (int x=0; x < result.length; x++) {
        for (int y=0; y < result[0].length; y++) {
          result[x][y] = "<no result>"; // or just "" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        }
      }
    
  • 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?

  • edited October 2017 Answer ✓

    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):

    (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 has 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!!!)

    int n;
    int console = 2;
    int min, tmp, i, j, min_id, data, k, temp;
    int[] array = new int [n];
    int[][] ellapsed = new int [5][4];
    int[][] start = new int [5][4];
    String[][] result= new String [5][4];
    String[][] frame= new String [5][4];
    
    boolean simAllFlag = false;
    int simAllFlagCounter = 0; 
    
    void setup() {
    
      size(661, 500);
      background(255);
    
      for (int x=0; x < result.length; x++) {
        for (int y=0; y < result[0].length; y++) {
          result[x][y] = "/";
        }
      }
    }
    
    //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    void draw() {
    
      drawGrid();
      labels();
    
      if (simAllFlag) { 
        simulateAll(simAllFlagCounter);
        simAllFlagCounter++;
        //  println ("here "+simAllFlagCounter);
      }
    
    
      fill(0); 
      for (int x = 0; x < result.length; x++) {
        for (int y = 0; y < result[0].length; y++) {
          text(result[x][y], x * 110 + 140, y * 80 + 125);
        }
      }
    }//func 
    
    //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    void initArr() {
    
      for (i = 0; i < n; i ++) {
        array[i] = (int)random(1, 10);
      }
    }
    
    //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    void mousePressed() {
      if (mouseX > 110 && 
        mouseX < 293.5 && 
        mouseY > 400 && 
        mouseY < 480) {
        simAllFlag=true;
      } else   if ( mouseX > 293.5  &&
        mouseX < 477 && 
        mouseY > 400 &&
        mouseY < 480) {
        n=99;
      }
    }
    
    //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    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);
      }
    
    
    
      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(int i222) {
    
      int x = 0;
      int y = 0;
    
      if (i222==4||
        i222==8||
        i222==12||
        i222==16||
        i222==20) {
        // skip this 
        // simAllFlagCounter++;
        return;
      }
    
      switch(i222) {
    
      case 0: 
        n=99; 
        array = new int[n]; // Brand new array.
        initArr();
        start[x][y] = millis();
        bubbleSort(array, n);
        ellapsed[x][y] = millis() - start[x][y];
        // println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break; 
    
      case 1: 
        n=999; 
        array = new int[999]; // Brand new array.
        x = 1;
        initArr();
        start[x][y] = millis();
        bubbleSort(array, 999);
        ellapsed[x][y] = millis() - start[x][y];
        //  println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 2: 
        n=9999; 
        array = new int[9999]; // Brand new array.
        x = 2;
        initArr();
        start[x][y] = millis();
        bubbleSort(array, 9999);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 3: 
        n=99999;
        array = new int[99999]; // Brand new array.
        x = 3;
        initArr();
        start[x][y] = millis();
        bubbleSort(array, 99999);
        ellapsed[x][y] = millis() - start[x][y];
        // println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 4: 
        n=999999;
        array = new int[999999]; // Brand new array.
        x = 4;
        initArr();
        start[x][y] = millis();
        bubbleSort(array, 999999);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 5: 
        x = 0;
        y = 1;
        n=99; 
        array = new int[99]; // Brand new array.
        initArr();
        start[x][y] = millis();
        selectionSort(array, n);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 6: 
        n=999;
        array = new int[999]; // Brand new array.
        x = 1;
        y = 1;
        initArr();
        start[x][y] = millis();
        selectionSort(array, 999);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 7: 
        n=9999;
        array = new int[9999]; // Brand new array.
        x = 2;
        y = 1;
        initArr();
        start[x][y] = millis();
        selectionSort(array, 9999);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 8: 
        n=99999;
        array = new int[99999]; // Brand new array.
        x = 3;
        y = 1;
        initArr();
        start[x][y] = millis();
        selectionSort(array, 99999);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 9: 
        n=999999;
        array = new int[999999]; // Brand new array.
        x = 4;
        y = 1;
        initArr();
        start[x][y] = millis();
        selectionSort(array, 999999);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 10: 
        n=99;
        array = new int[n]; // Brand new array.
        x = 0;
        y = 2;
        initArr();
        start[x][y] = millis();
        insertionSort(array, n);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 11: 
        n=999;
        array = new int[n]; // Brand new array.
        x = 1;
        y = 2;
        initArr();
        start[x][y] = millis();
        insertionSort(array, 999);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 12: 
        n=9999;
        array = new int[9999]; // Brand new array.
        x = 2;
        y = 2;
        initArr();
        start[x][y] = millis();
        insertionSort(array, 9999);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 13: 
        n=99999; 
        array = new int[99999]; // Brand new array.
        x = 3;
        y = 2;
        initArr();
        start[x][y] = millis();
        insertionSort(array, 99999);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 14: 
        n=999999; 
        array = new int[999999]; // Brand new array.
        x = 4;
        y = 2;
        initArr();
        start[x][y] = millis();
        insertionSort(array, 999999);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 15: 
        x = 0;
        y = 3;
        n=99; 
        array = new int[n]; // Brand new array.
        initArr();
        start[x][y] = millis();
        quickSort(0, n - 1);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 16: 
        n=999; 
        array = new int[999]; // Brand new array.
        x = 1;
        y = 3;
        initArr();
        start[x][y] = millis();
        quickSort(0, 999 - 1);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 17: 
        array = new int[9999]; // Brand new array.
        x = 2;
        y = 3;
        initArr();
        start[x][y] = millis();
        quickSort(0, 9999 - 1);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 18: 
        array = new int[99999]; // Brand new array.
        x = 3;
        y = 3;
        initArr();
        start[x][y] = millis();
        quickSort(0, 99999 - 1);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
    
      case 19: 
        array = new int[999999]; // Brand new array.
        x = 4;
        y = 3;
        initArr();
        start[x][y] = millis();
        quickSort(0, 999999 - 1);
        ellapsed[x][y] = millis() - start[x][y];
        println("Time:" + " " + (float)ellapsed[x][y]/1000);
        result[x][y] = str ((float)ellapsed[x][y]/1000 );
        break;
      }
    }
    
    void keyPressed() {
      if (key == CODED) {
        if (keyCode == UP) 
          console = 1;
        else
          console = 2;
        if (keyCode == DOWN)
          console = 2;
      }
    }
    
  • reset could do this:

     for (int x=0; x < result.length; x++) {
        for (int y=0; y < result[0].length; y++) {
          result[x][y] = "/";
        }
      }
    

    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

  • edited October 2017 Answer ✓

    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!!!

  • Answer ✓

    you're welcome!

This discussion has been closed.