-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9476061
commit cbaed81
Showing
7 changed files
with
388 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.