Skip to content

Instantly share code, notes, and snippets.

@Git-I985
Last active July 16, 2023 16:33
Show Gist options
  • Save Git-I985/0ce1fa4fa4f6e85d1d1f111bf82b8da2 to your computer and use it in GitHub Desktop.
Save Git-I985/0ce1fa4fa4f6e85d1d1f111bf82b8da2 to your computer and use it in GitHub Desktop.
bubble-sort-js
const stdout = {
template: (val) => `\x1b[${val}m`,
reset: () => stdout.template(0),
cyanBright: (value) => stdout.template(96) + value + stdout.template(39) + stdout.reset(),
yellowBright: (value) => stdout.template(93) + value + stdout.template(39) + stdout.reset(),
redBright: (value) => stdout.template(91) + value + stdout.template(39) + stdout.reset(),
greenBright: (value) => stdout.template(92) + value + stdout.template(39) + stdout.reset(),
gray: (value) => stdout.template(30) + value + stdout.template(89) + stdout.reset(),
bold: (value) => stdout.template(1) + value + stdout.template(22) + stdout.reset(),
dim: (value) => stdout.template(2) + value + stdout.template(22) + stdout.reset(),
strikethrough: (value) => stdout.template(9) + value + stdout.template(29) + stdout.reset(),
magenta: (value) => stdout.template(35) + value + stdout.template(89) + stdout.reset(),
array: (arr) => [stdout.yellowBright('['), arr.join(', '), stdout.yellowBright(']')].join(stdout.reset()) + stdout.reset(),
newLine: () => '\n',
dimAfter: i => (el, index) => index > i ? stdout.dim(el) : stdout.cyanBright(el),
space: () => ' '
}
const bubbleSort = (_arr) => {
if (!Array.isArray(_arr)) {
throw new TypeError('Passed argument is not array')
}
if (!_arr.length) {
return []
}
const arr = [..._arr]
// DEBUGSTART
console.log(
stdout.newLine()
+ `Bubble sort:`
+ stdout.space()
+ stdout.array(arr)
)
// DEBUGEND
for (let i = arr.length - 1; i > 0; i--) {
// DEBUGSTART
console.log(
stdout.newLine()
+ `Loop to ${stdout.cyanBright(i + 1)} element`
+ stdout.space()
+ stdout.array(arr.map(stdout.dimAfter(i)))
+ stdout.newLine()
)
// DEBUGEND
for (let j = 0; j < i; j++) {
// DEBUGSTART
process.stdout.write(
`> Compare ${stdout.redBright(arr[j])} and ${stdout.greenBright(arr[j + 1])}`
+ stdout.space()
+ stdout.array(arr.map((el, index) => index === j ? stdout.redBright(el) : index === j + 1 ? stdout.greenBright(el) : index > i ? stdout.dim(el) : el))
+ stdout.space()
+ (arr[j] > arr[j + 1] ? stdout.magenta(' swap') : stdout.dim('noswap'))
+ stdout.space()
)
// DEBUGEND
if (arr[j] > arr[j + 1]) {
const temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
}
// DEBUGSTART
process.stdout.write(
stdout.array(arr.map((el, index) => index === j + 1 ? stdout.redBright(el) : index === j ? stdout.greenBright(el) : index > i ? stdout.dim(el) : el))
+ stdout.newLine()
)
// DEBUGEND
}
}
// DEBUGSTART
console.log(
stdout.newLine()
+ 'Sort finished, result:'
+ stdout.space()
+ stdout.array(arr)
+ stdout.newLine()
)
// DEBUGEND
return arr
}
/* Impl without debug expressions
const bubbleSort = (_arr) => {
if (!Array.isArray(_arr)) {
throw new TypeError('Passed argument is not array');
}
if (!_arr.length) {
return [];
}
const arr = [..._arr];
for (let i = arr.length - 1; i > 0; i--) {
for (let j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
const temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
*/
const arr = [5, 4, 2, 3, 1]
const newArr = bubbleSort(arr)
@Git-I985
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment