Search code examples
processingindexoutofboundsexceptionbubble-sort

Bubble Sort with ellipse


Goal: I want to sort randomly generated ellipse to their radius. From Min -> Max.

Problem: Every time i run, this a error occurs. ArrayIndexOutOfBunds.

Here is my Code:

Ellipse[] e = new Ellipse[100];
int j;
void setup() {
  fullScreen();
  for (int i = 0; i<e.length; i++) {
    e[i] = new Ellipse(random(10, width-10), height/2, random(10, 80), color(random(1, 255)));
  }
}
void draw() {
  background(255);
  for (int i = 0; i<e.length; i++) {
    e[i].show();
  }
  if (j<e.length) {
    for (int i = 0; i<e.length-1; i++) {
      float a = e[i].r;
      float b = e[i+1].r;
      if (a>b) {
        swap( int(e[i].x), int(e[i+1].x));
      }
    }
  }
  j++;
}
void swap(int a, int b) {
  float zwischen = e[a].x;
  e[a].x = e[b].x;
  e[b].x = zwischen;
}
class Ellipse {
  float x, y, r;
  color c;
  Ellipse(float x_, float y_, float r_, color c_) {
    x = x_;
    y = y_;
    r = r_;
    c = c_;
  }
  void show() {
    fill(c);
    ellipse(x, y, r, r);
  }
}

Solution

  • You want to swap the element with the index i and the element with the index i+1. The function swap swaps 2 elements. The arguments to the function are the indices of the elements. So it has to be:

    swap( int(e[i].x), int(e[i+1].x));

    swap(i, i+1);
    

    To make the algorithm work the index of the ellipse in list has to correspond to the position of the ellipse along the x axis. If the x axis and the y axis is random, the ellipses would be sorted to random positions by its size. You what to sort the ellipses by its radius along the x axis.
    Create the ellipses by ascending x coordinate with random radius:

    for (int i = 0; i<e.length; i++) {
        int x = 10 + (width-20) * i / e.length;
        e[i] = new Ellipse(x, height/2, random(10, 80), color(random(1, 255)));
    }
    

    Further more, swap has to swap the ellipses, but it has to keep the position along the x axis:

    void swap(int a, int b) {
        float ax = e[a].x;
        float bx = e[b].x;
    
        Ellipse temp = e[a];
        e[a] = e[b];
        e[b] = temp;
    
        e[a].x = ax;
        e[b].x = bx;
    }
    

    See the exmple:

    Ellipse[] e = new Ellipse[100];
    int j=0;
    
    void setup() {
        fullScreen();
        for (int i = 0; i<e.length; i++) {
            int x = 80 + (width-160) * i / e.length;
            e[i] = new Ellipse(x, height/2, random(10, 80), color(random(1, 255)));
        }
    }
    
    void draw() {
        background(255);
        for (int i = 0; i<e.length; i++) {
           e[i].show();
        }
    
        if (j<e.length) {
            for (int i = 0; i < e.length-1-j; i++) {
                float a = e[i].r;
                float b = e[i+1].r;
                if (a > b) {
                  swap(i, i+1);
                }
            }
        }
        j++;
    }
    
    void swap(int a, int b) {
        float ax = e[a].x;
        float bx = e[b].x;
    
        Ellipse temp = e[a];
        e[a] = e[b];
        e[b] = temp;
    
        e[a].x = ax;
        e[b].x = bx;
    }
    
    class Ellipse {
        float x, y, r;
        color c;
    
        Ellipse(float x_, float y_, float r_, color c_) {
            x = x_;
            y = y_;
            r = r_;
            c = c_;
        }
    
        void show() {
            fill(c);
            ellipse(x, y, r, r);
        }
    }