Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewoestreich committed Jul 31, 2024
1 parent 9476061 commit cbaed81
Show file tree
Hide file tree
Showing 7 changed files with 388 additions and 349 deletions.
48 changes: 24 additions & 24 deletions beep.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
class Beep {
/**
*
* @param {AudioContext} audioContext
*/
constructor(audioContext = null) {
this.audioContext = audioContext;
if (audioContext === null) {
this.audioContext = new(AudioContext || webkitAudioContext || window.webkitAudioContext);
}
}
/**
*
* @param {AudioContext} audioContext
*/
constructor(audioContext = null) {
this.audioContext = audioContext;
if (audioContext === null) {
this.audioContext = new (AudioContext || webkitAudioContext || window.webkitAudioContext)();
}
}

playNote(frequency, duration) {
const oscillator = this.audioContext.createOscillator();
const volume = this.audioContext.createGain();
duration = Math.max(duration, 0.03);
/*
playNote(frequency, duration) {
const oscillator = this.audioContext.createOscillator();
const volume = this.audioContext.createGain();
duration = Math.max(duration, 0.03);
/*
const compressor = this.audioContext.createDynamicsCompressor();
oscillator.frequency.value = frequency;
Expand All @@ -31,12 +31,12 @@ class Beep {
oscillator.start(this.audioContext.currentTime);
oscillator.stop(this.audioContext.currentTime+decay+duration);
*/
oscillator.frequency.value = frequency;
volume.gain.value = 0.01;
volume.gain.exponentialRampToValueAtTime(volume.gain.value, this.audioContext.currentTime+duration);
volume.gain.exponentialRampToValueAtTime(0.0001, this.audioContext.currentTime+0.3);
oscillator.connect(volume).connect(this.audioContext.destination);
oscillator.start();
oscillator.stop(this.audioContext.currentTime+duration+0.01);
}
}
oscillator.frequency.value = frequency;
volume.gain.value = 0.01;
volume.gain.exponentialRampToValueAtTime(volume.gain.value, this.audioContext.currentTime + duration);
volume.gain.exponentialRampToValueAtTime(0.0001, this.audioContext.currentTime + 0.3);
oscillator.connect(volume).connect(this.audioContext.destination);
oscillator.start();
oscillator.stop(this.audioContext.currentTime + duration + 0.01);
}
}
78 changes: 39 additions & 39 deletions bubbleSort.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
function* bubbleSort(array=[]) {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < (array.length - 1 - i); j++) {
if (array[j].dataset.value > array[j+1].dataset.value) {
yield {
type: "swap",
greater: { index: j, element: array[j] },
lesser: { index: j+1, element: array[j+1] }
}
} else {
yield {
type: "nop",
greater: { index: j+1, element: array[j+1] },
lesser: { index: j, element: array[j] }
}
}
}
}
function* bubbleSort(array = []) {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length - 1 - i; j++) {
if (array[j].dataset.value > array[j + 1].dataset.value) {
yield {
type: "swap",
greater: { index: j, element: array[j] },
lesser: { index: j + 1, element: array[j + 1] },
};
} else {
yield {
type: "nop",
greater: { index: j + 1, element: array[j + 1] },
lesser: { index: j, element: array[j] },
};
}
}
}
}

async function renderBubbleSort(animations) {
for (const { type, greater, lesser } of animations) {
greater.element.style.backgroundColor = BAR_COLORS.compare;
lesser.element.style.backgroundColor = BAR_COLORS.compare;
for (const { type, greater, lesser } of animations) {
greater.element.style.backgroundColor = BAR_COLORS.compare;
lesser.element.style.backgroundColor = BAR_COLORS.compare;

await sleep(sliderSpeed.dataset.value / 2);

if (type === "swap") {
greater.element.style.backgroundColor = BAR_COLORS.correct;
lesser.element.style.backgroundColor = BAR_COLORS.incorrect;
[ARRAY[greater.index], ARRAY[lesser.index]] = [ARRAY[lesser.index], ARRAY[greater.index]];
await renderBars(ARRAY, divRenderBars);
} else {
greater.element.style.backgroundColor = BAR_COLORS.correct;
lesser.element.style.backgroundColor = BAR_COLORS.correct;
}

await sleep(sliderSpeed.dataset.value / 2);
lesser.element.style.backgroundColor = BAR_COLORS.default;
greater.element.style.backgroundColor = BAR_COLORS.completed;
}
await sleep(sliderSpeed.dataset.value / 2);

ARRAY[0].style.backgroundColor = BAR_COLORS.completed;
}
if (type === "swap") {
greater.element.style.backgroundColor = BAR_COLORS.correct;
lesser.element.style.backgroundColor = BAR_COLORS.incorrect;
[ARRAY[greater.index], ARRAY[lesser.index]] = [ARRAY[lesser.index], ARRAY[greater.index]];
await renderBars(ARRAY, divRenderBars);
} else {
greater.element.style.backgroundColor = BAR_COLORS.correct;
lesser.element.style.backgroundColor = BAR_COLORS.correct;
}

await sleep(sliderSpeed.dataset.value / 2);
lesser.element.style.backgroundColor = BAR_COLORS.default;
greater.element.style.backgroundColor = BAR_COLORS.completed;
}

ARRAY[0].style.backgroundColor = BAR_COLORS.completed;
}
164 changes: 100 additions & 64 deletions cocktailShakerSort.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,111 @@
function* cocktailShakerSort(array) {
let isSwap = true;
// Since each iteration will produce the largest and smallest elements,
// we can keep track of how many loops we have completed as to not process
// unnecessary elements (which we already know are correct).
let loops = 0;
let isSwap = true;
// Since each iteration will produce the largest and smallest elements,
// we can keep track of how many loops we have completed as to not process
// unnecessary elements (which we already know are correct).
let loops = 0;

while (isSwap) {
isSwap = false;
for (let i = 0+loops; i < (array.length - 1 - loops); i++) {
yield { type: "colors", elements: [{ index: i, value: BAR_COLORS.compare }, { index: i+1, value: BAR_COLORS.compare }] };
if (Number(array[i].dataset.value) > Number(array[i+1].dataset.value)) {
yield { type: "colors", elements: [{ index: i, value: BAR_COLORS.incorrect }, { index: i+1, value: BAR_COLORS.incorrect }] };
yield { type: "swap", leftIndex: i, rightIndex: i+1 };
[array[i], array[i+1]] = [array[i+1], array[i]];
isSwap = true;
}
yield { type: "colors", elements: [{ index: i, value: BAR_COLORS.default }, { index: i+1, value: BAR_COLORS.correct }] };
}
while (isSwap) {
isSwap = false;
for (let i = 0 + loops; i < array.length - 1 - loops; i++) {
yield {
type: "colors",
elements: [
{ index: i, value: BAR_COLORS.compare },
{ index: i + 1, value: BAR_COLORS.compare },
],
};
if (Number(array[i].dataset.value) > Number(array[i + 1].dataset.value)) {
yield {
type: "colors",
elements: [
{ index: i, value: BAR_COLORS.incorrect },
{ index: i + 1, value: BAR_COLORS.incorrect },
],
};
yield { type: "swap", leftIndex: i, rightIndex: i + 1 };
[array[i], array[i + 1]] = [array[i + 1], array[i]];
isSwap = true;
}
yield {
type: "colors",
elements: [
{ index: i, value: BAR_COLORS.default },
{ index: i + 1, value: BAR_COLORS.correct },
],
};
}

// "Last" element is now in correct position.
yield { type: "color", index: array.length-1-loops, value: BAR_COLORS.completed };
// "Last" element is now in correct position.
yield { type: "color", index: array.length - 1 - loops, value: BAR_COLORS.completed };

// If nothing was swapped we know we are sorted and can break early.
if (!isSwap) {
break;
}
// If nothing was swapped we know we are sorted and can break early.
if (!isSwap) {
break;
}

isSwap = false;
for (let i = array.length-2-loops; i > (0 + loops); i--) {
yield { type: "colors", elements: [{ index: i, value: BAR_COLORS.compare }, { index: i-1, value: BAR_COLORS.compare }] };
if (Number(array[i].dataset.value) < Number(array[i-1].dataset.value)) {
yield { type: "colors", elements: [{ index: i, value: BAR_COLORS.incorrect }, { index: i-1, value: BAR_COLORS.incorrect }] };
yield { type: "swap", leftIndex: i, rightIndex: i-1 };
[array[i], array[i-1]] = [array[i-1], array[i]];
isSwap = true;
}
yield { type: "colors", elements: [{ index: i, value: BAR_COLORS.default }, { index: i-1, value: BAR_COLORS.correct }] };
}
isSwap = false;
for (let i = array.length - 2 - loops; i > 0 + loops; i--) {
yield {
type: "colors",
elements: [
{ index: i, value: BAR_COLORS.compare },
{ index: i - 1, value: BAR_COLORS.compare },
],
};
if (Number(array[i].dataset.value) < Number(array[i - 1].dataset.value)) {
yield {
type: "colors",
elements: [
{ index: i, value: BAR_COLORS.incorrect },
{ index: i - 1, value: BAR_COLORS.incorrect },
],
};
yield { type: "swap", leftIndex: i, rightIndex: i - 1 };
[array[i], array[i - 1]] = [array[i - 1], array[i]];
isSwap = true;
}
yield {
type: "colors",
elements: [
{ index: i, value: BAR_COLORS.default },
{ index: i - 1, value: BAR_COLORS.correct },
],
};
}

// "First" element is now in correct position.
yield { type: "color", index: 0+loops, value: BAR_COLORS.completed };
// "First" element is now in correct position.
yield { type: "color", index: 0 + loops, value: BAR_COLORS.completed };

loops++;
}
loops++;
}

// Mark remaining as completed
for (let i = loops; i < array.length-loops; i++) {
yield { type: "color", index: i, value: BAR_COLORS.completed };
}
// Mark remaining as completed
for (let i = loops; i < array.length - loops; i++) {
yield { type: "color", index: i, value: BAR_COLORS.completed };
}
}

async function renderCocktailShakerSort(animations) {
for (const animation of animations) {
if (animation.type === "color") {
const { index, value } = animation;
divRenderBars.childNodes[index].style.backgroundColor = value;
await sleep(sliderSpeed.dataset.value);
}
if (animation.type === "colors") {
for (let i = 0; i < animation.elements.length; i++) {
const { index, value } = animation.elements[i];
divRenderBars.childNodes[index].style.backgroundColor = value;
}
await sleep(sliderSpeed.dataset.value);
}
if (animation.type === "swap") {
const { leftIndex, rightIndex } = animation;
const temp = divRenderBars.childNodes[leftIndex].style.height;
divRenderBars.childNodes[leftIndex].style.height = divRenderBars.childNodes[rightIndex].style.height;
divRenderBars.childNodes[rightIndex].style.height = temp;
await sleep(sliderSpeed.dataset.value);
}
}
}
for (const animation of animations) {
if (animation.type === "color") {
const { index, value } = animation;
divRenderBars.childNodes[index].style.backgroundColor = value;
await sleep(sliderSpeed.dataset.value);
}
if (animation.type === "colors") {
for (let i = 0; i < animation.elements.length; i++) {
const { index, value } = animation.elements[i];
divRenderBars.childNodes[index].style.backgroundColor = value;
}
await sleep(sliderSpeed.dataset.value);
}
if (animation.type === "swap") {
const { leftIndex, rightIndex } = animation;
const temp = divRenderBars.childNodes[leftIndex].style.height;
divRenderBars.childNodes[leftIndex].style.height = divRenderBars.childNodes[rightIndex].style.height;
divRenderBars.childNodes[rightIndex].style.height = temp;
await sleep(sliderSpeed.dataset.value);
}
}
}
42 changes: 21 additions & 21 deletions mergeSort.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
function* mergeSort(array) {
yield* mergePartition(0, array.length - 1);
yield* mergePartition(0, array.length - 1);
}

function* merge(start, mid, end) {
let backgroundColor = (start === 0 && end === ARRAY.length - 1) ? BAR_COLORS.completed : BAR_COLORS.correct;
let backgroundColor = start === 0 && end === ARRAY.length - 1 ? BAR_COLORS.completed : BAR_COLORS.correct;
let p = start;
let q = mid + 1;
let arr = [];
let arr = [];

for (let i = start; i <= end; i++) {
if (p > mid) {
arr.push(ARRAY[q].cloneNode());
arr.push(ARRAY[q].cloneNode());
yield { type: "backgroundColor", value: BAR_COLORS.incorrect, index: q };
q++;
} else if (q > end || (Number(ARRAY[p].dataset.value) < Number(ARRAY[q].dataset.value))) {
arr.push(ARRAY[p].cloneNode());
} else if (q > end || Number(ARRAY[p].dataset.value) < Number(ARRAY[q].dataset.value)) {
arr.push(ARRAY[p].cloneNode());
yield { type: "backgroundColor", value: BAR_COLORS.incorrect, index: p };
p++;
} else {
Expand All @@ -24,12 +24,12 @@ function* merge(start, mid, end) {
}
}

for (let i = 0; i < arr.length; i++) {
ARRAY[start] = arr[i];
yield { type: "backgroundColor", index: start, value: backgroundColor };
yield { type: "height", index: start, value: arr[i].style.height }
start++;
}
for (let i = 0; i < arr.length; i++) {
ARRAY[start] = arr[i];
yield { type: "backgroundColor", index: start, value: backgroundColor };
yield { type: "height", index: start, value: arr[i].style.height };
start++;
}
}

function* mergePartition(start, end) {
Expand All @@ -43,14 +43,14 @@ function* mergePartition(start, end) {
}

async function renderMergeSort(animations) {
let next = animations.next();
if (next.done) {
return;
}
let next = animations.next();
if (next.done) {
return;
}

const { type, value, index } = next.value;
divRenderBars.childNodes[index].style[type] = value;
await sleep(sliderSpeed.dataset.value);
const { type, value, index } = next.value;
divRenderBars.childNodes[index].style[type] = value;
await sleep(sliderSpeed.dataset.value);

await renderMergeSort(animations);
}
await renderMergeSort(animations);
}
Loading

0 comments on commit cbaed81

Please sign in to comment.