Bubble Sort

Hau Phan / May 18, 2022

1 min read

var bar = [];

function setup() {
  createCanvas(1280, 720);
  for (let i = 0; i < width; i++) {
    bar[i] = random(height);
  }
  i = width - 1;
}

function draw() {
  background(0);
  stroke(255);
  if (i >= 0) {
    for (let j = 0; j <= i; j++) {
      if (bar[j] > bar[j + 1]) {
        let temp = bar[j];
        bar[j] = bar[j + 1];
        bar[j + 1] = temp;
      }
    }
    i--;
  } else {
    setup();
  }
  for (let i = 0; i < width; i++) {
    line(i, height, i, height - bar[i]);
  }
}

function mouseClicked() {
  setup();
}

Contact me!

Below are my primary work email address.
You can also message me on LinkedIn and Facebook.

Reply times tend to be 1-2 days (at most).