createCanvas(width, height);
background(color(212, 236, 234));
length = parseInt(list_length);
for (let i = 0; i < list_length; i++) {
array = listShuffle(array);
let pivot = Math.floor((array.length - 1) / 2);
let pivot_height = array[pivot];
record(null, null, 0, length - 1, pivot_height, array);
sorted = quickSort(0, array.length - 1, array);
record(null, null, -100, 0, -10000, array);
if (sorted && count < map.length) {
background(color(212, 236, 234));
drawBar(m.array, m.pivot_height, m.swap);
drawHeight(m.range, m.pivot_height);
function listShuffle(array) {
for (let i = array.length - 1; i >= 0; i--) {
let rand = Math.floor(Math.random() * (i + 1));
[array[i], array[rand]] = [array[rand], array[i]]
function drawHeight(range, height) {
const y = 350 - (grid_pixel * height) + 5;
let left = (grid_pixel + margin_left) * range.srart + 2;
let width = (grid_pixel + margin_left) * (range.end - range.srart + 1);
line(left, y, left + width, y);
function drawRnage(range) {
let left = (grid_pixel + margin_left) * range.srart + 2;
let width = (grid_pixel + margin_left) * (range.end - range.srart + 1);
fill(color(222, 246, 244));
rect(left, under, width, height);
function drawBar(array, pivot_height, swap) {
for (let i = 0; i < array.length; i++) {
let left = (grid_pixel + margin_left) * i + 2;
let height = grid_pixel * array[i];
if (array[i] === pivot_height) {
fill(color(191, 164, 65));
} else if (i === swap.left) {
fill(color(65, 156, 192));
} else if (i === swap.right) {
fill(color(191, 65, 155));
fill(color(237, 235, 213));
rect(left, under - height, grid_pixel, height);
rect(left, under - height, grid_pixel, grid_pixel);
function quickSort(start, end, array) {
let pivot = Math.floor((start + end) / 2);
let pivot_height = array[pivot];
while (array[left] < pivot_height) left++;
while (array[right] > pivot_height) right--;
record(left, right, start, end, pivot_height, array);
swap(left, right, array);
quickSort(start, left - 1, array);
quickSort(right, end, array);
function swap(left, right, array) {
array[left] = array[right];
function record(left, right, start, end, pivot_height, array) {
pivot_height: pivot_height,