Last active
January 18, 2019 10:21
-
-
Save Alexandre-cibot/53f801f240550e2323a4cb660dd5ce7f to your computer and use it in GitHub Desktop.
Bubble sort
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
function bubbleSort(array) { | |
let res = [...array]; | |
let status = new Array(res.length - 2).fill(false); | |
while (!status.every(e => e === true)) { | |
for (let i = 0; i < res.length - 1; i++) { | |
if (res[i] > res[i + 1]) { | |
// swap values | |
let temp = res[i]; | |
res[i] = res[i + 1]; | |
res[i + 1] = temp; | |
status[i] = false; | |
} else { | |
status[i] = true; | |
} | |
} | |
} | |
return res; | |
} | |
function logTimeProcess(name, fn) { | |
const t0 = performance.now(); | |
console.log(fn()); // <---- The function you're measuring time for | |
const t1 = performance.now(); | |
return `this function ${name} takes ${t1 - t0} milliseconds.`; | |
} | |
const arr = [1, 2, 4, 3, 2, 9, 3, 5, 6789, 2, 11, 1, 0]; | |
console.log(logTimeProcess("native sort", () => arr.sort((a, b) => a - b))); | |
console.log(logTimeProcess("personnal sort", () => bubbleSort(arr))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment