xxxxxxxxxx
178
// Constants
const WIDTH = 800;
const HEIGHT = 400;
const BAR_WIDTH = 10;
const DELAY = 10; // Delay between frames (in milliseconds)
// Sorting variables
let values = [];
let sorted = false;
let sortFunction;
// Button variables
let shuffleButton;
let multipleShufflesButton;
let randomSwapsButton;
let delaysButton;
let partialShufflingButton;
let recursiveBogoButton;
function setup() {
createCanvas(WIDTH, HEIGHT);
// Create buttons
shuffleButton = createButton('Shuffle');
shuffleButton.position(20, 20);
shuffleButton.mousePressed(shuffleList);
multipleShufflesButton = createButton('Multiple Shuffles');
multipleShufflesButton.position(120, 20);
multipleShufflesButton.mousePressed(multipleShufflesSort);
randomSwapsButton = createButton('Random Swaps');
randomSwapsButton.position(260, 20);
randomSwapsButton.mousePressed(randomSwapsSort);
delaysButton = createButton('Delays');
delaysButton.position(380, 20);
delaysButton.mousePressed(delaysSort);
partialShufflingButton = createButton('Partial Shuffling');
partialShufflingButton.position(470, 20);
partialShufflingButton.mousePressed(partialShufflingSort);
recursiveBogoButton = createButton('Recursive Bogosort');
recursiveBogoButton.position(610, 20);
recursiveBogoButton.mousePressed(recursiveBogoSort);
// Generate initial list of values
generateValues();
}
function draw() {
background(255);
// Display bars
for (let i = 0; i < values.length; i++) {
const x = i * BAR_WIDTH;
const y = height - values[i];
fill(0);
rect(x, y, BAR_WIDTH, values[i]);
}
// Check if the list is sorted
sorted = isSorted(values);
// Stop the animation if the list is sorted
if (sorted) {
noLoop();
}
}
// Shuffle the list randomly
function shuffleList() {
values = shuffle(values);
loop();
}
// Sort by shuffling multiple times
function multipleShufflesSort() {
sortFunction = multipleShuffles;
loop();
}
// Sort by random swaps
function randomSwapsSort() {
sortFunction = randomSwaps;
loop();
}
// Sort with delays between shuffling attempts
function delaysSort() {
sortFunction = delays;
loop();
}
// Sort by partially shuffling the list
function partialShufflingSort() {
sortFunction = partialShuffling;
loop();
}
// Sort recursively by shuffling sublists
function recursiveBogoSort() {
sortFunction = recursiveBogo;
loop();
}
// Generate a new list of values
function generateValues() {
values = [];
for (let i = 0; i < width / BAR_WIDTH; i++) {
values.push(random(height));
}
}
// Check if the list is sorted
function isSorted(arr) {
for (let i = 1; i < arr.length; i++) {
if (arr[i] < arr[i - 1]) {
return false;
}
}
return true;
}
// Shuffle array randomly using Fisher-Yates algorithm
function shuffle(arr) {
for (let i = arr.length - 1; i > 0; i--) {
const j = floor(random(i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}
// Sort by shuffling multiple times
function multipleShuffles(arr) {
arr = shuffle(arr);
return arr;
}
// Sort by random swaps
function randomSwaps(arr) {
const i = floor(random(arr.length));
const j = floor(random(arr.length));
[arr[i], arr[j]] = [arr[j], arr[i]];
return arr;
}
// Sort with delays between shuffling attempts
function delays(arr) {
setTimeout(() => {
arr = shuffle(arr);
loop();
}, DELAY);
return arr;
}
// Sort by partially shuffling the list
function partialShuffling(arr) {
const startIndex = floor(random(arr.length));
const endIndex = floor(random(startIndex, arr.length));
const subArr = arr.slice(startIndex, endIndex);
const shuffledSubArr = shuffle(subArr);
arr.splice(startIndex, subArr.length, shuffledSubArr);
return arr;
}
// Sort recursively by shuffling sublists
function recursiveBogo(arr) {
for (let i = 0; i < arr.length; i++) {
const subArr = arr.slice(i);
arr.splice(i, subArr.length, shuffle(subArr));
loop();
}
return arr;
}