Skip to content

Instantly share code, notes, and snippets.

View galElmalah's full-sized avatar

Gal Elmalah galElmalah

View GitHub Profile
@galElmalah
galElmalah / script.js
Last active May 26, 2020 19:11
creating and shuffling array
const shuffledArrayInRange = (bottom = 1, top = 30) => {
const arr = [];
for (let i = bottom; i < top; i++) arr.push(i);
return arr.sort((a, b) => (Math.random() > 0.5 ? 1 : -1));
};
@galElmalah
galElmalah / script.js
Last active June 11, 2020 09:51
bubble sort implementation
const bubbleSort = (array, onAction) => {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length; j++) {
if (array[j] > array[j + 1]) {
let tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
}
}
}
const canvas = document.querySelector("canvas");
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
const ctx = canvas.getContext("2d");
@galElmalah
galElmalah / script.js
Created May 26, 2020 13:01
visualisation for a single array member
function ArrayMember(x, y, width, height, color = "gray") {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
this.draw = () => {
ctx.fillStyle = this.color;
@galElmalah
galElmalah / script.js
Created May 26, 2020 13:03
drawing all of the members to the canvas
const randomArray = shuffledArrayInRange();
const arrayMembers = randomArray.map((v, i) => {
return new ArrayMember(10 * i + i, 0, 10, v * 5);
});
const drawAll = () => arrayMembers.forEach((m) => m.draw());
drawAll()
@galElmalah
galElmalah / script.js
Created May 26, 2020 13:08
actions map
const ACTIONS = {
SORT: "SORT",
COMPARE: "COMPARE",
SWAP: "SWAP",
};
// ---- what to do on each action ----
const actionsMap = {
[ACTIONS.SORT]: (action, members) => members[action.data].sorted(),
@galElmalah
galElmalah / script.js
Last active June 11, 2020 09:52
bubble sort with actions
const bubbleSort = (array, onAction) => {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length; j++) {
onAction({ type: ACTIONS.COMPARE, data: [j, j + 1] });
if (array[j] > array[j + 1]) {
let tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
onAction({ type: ACTIONS.SWAP, data: [j, j + 1] });
}
@galElmalah
galElmalah / script.js
Created May 26, 2020 13:13
painting each action
let ticks = 0;
const speed = 50;
bubbleSort(randomArray, (action) => {
ticks++;
setTimeout(() => {
actionsMap[action.type](action, arrayMembers);
ctx.clearRect(0, 0, innerWidth, innerHeight);
drawAll(arrayMembers);
arrayMembers.forEach((m) => m.resetColor());
@galElmalah
galElmalah / script.js
Last active June 11, 2020 09:53
putting it all together
const canvas = document.querySelector("canvas");
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
const ctx = canvas.getContext("2d");
const ACTIONS = {
SORT: "SORT",
COMPARE: "COMPARE",
SWAP: "SWAP",
};
@galElmalah
galElmalah / script.js
Created May 27, 2020 05:22
action types
const ACTIONS = {
SORT: "SORT",
COMPARE: "COMPARE",
SWAP: "SWAP",
};