Last active
October 7, 2020 18:12
-
-
Save nns2009/a7bb68a626364531aeb21e17a0a92b2c to your computer and use it in GitHub Desktop.
Count sort made while counting every single key pressed
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
// Count sort I coded while counting every single key I press | |
// Watch video here: | |
// https://youtu.be/HFgzhYHM4QM | |
function countSort(vs) { | |
const cs = Array(Math.max(...vs) + 1).fill(0); | |
let res = []; | |
for (let v of vs) | |
cs[v]++; | |
for (let v = 0; v < cs.length; v++) { | |
for (let c = 0; c < cs[v]; c++) | |
res.push(v); | |
} | |
return res; | |
} | |
// Testing code | |
const test = [34, 3, 13, 5, 8, 13, 21, 21, 55, 34, 3, 3, 2, 1, 34, 8]; | |
console.log(countSort(test)); | |
console.log(test.sort((a, b) => a - b)); | |
// Count sort I originally planned to code during the video above: | |
function countSort(vs) { | |
const max = Math.max(...vs); | |
const counts = Array(max + 1).fill(0); | |
for (const v of vs) | |
counts[v]++; | |
const res = []; | |
for (let i = 0; i <= max; i++) | |
for (let j = 0; j < counts[i]; j++) | |
res.push(i); | |
return res; | |
} | |
// Count sort but only using letter 'i' for variable names | |
// Watch here (the second part of the video above): | |
// https://youtu.be/HFgzhYHM4QM?t=792 | |
function iiiiiiiii(iiiiii){ | |
let ii = Array(Math.max(...iiiiii) + 1).fill(0); | |
for (let i of iiiiii) | |
ii[i]++; | |
let iii = []; | |
for (let i = 0; i < ii.length; i++) | |
for (let iiii = 0; | |
iiii < ii[i]; iiii++) | |
iii.push(i); | |
return iii; | |
} | |
// Testing code | |
const test = [34, 3, 13, 5, 8, 13, 21, 21, 55, 34, 3, 3, 2, 1, 34, 8]; | |
console.log(iiiiiiiii(test)); | |
console.log(test.sort((a, b) => a - b)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment