Last active
November 3, 2023 21:30
-
-
Save crazy4groovy/e35714f0a1ba93d70e1d960e07176a5c to your computer and use it in GitHub Desktop.
sort large arrays using the event loop (JavaScript)
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 chunkedMergeSort(arr, callback) { | |
function merge(left, right, arr, callback) { | |
let i = 0; | |
let j = 0; | |
let k = 0; | |
function mergeStep() { | |
while (i < left.length && j < right.length) { | |
if (left[i] <= right[j]) { | |
arr[k++] = left[i++]; | |
} else { | |
arr[k++] = right[j++]; | |
} | |
} | |
while (i < left.length) { | |
arr[k++] = left[i++]; | |
} | |
while (j < right.length) { | |
arr[k++] = right[j++]; | |
} | |
if (i >= left.length && j >= right.length) { | |
callback(arr); | |
return; | |
} | |
// Yield thread, recurse | |
setTimeout(mergeStep, 0); | |
} | |
mergeStep(); | |
} | |
function mergeSort(arr, callback) { | |
if (arr.length <= 1) { | |
return callback(arr); | |
} | |
const middle = Math.floor(arr.length / 2); | |
const left = arr.slice(0, middle); | |
const right = arr.slice(middle); | |
mergeSort(left, () => { | |
mergeSort(right, () => { | |
merge(left, right, arr, callback); | |
}); | |
}); | |
} | |
mergeSort(arr, callback); | |
} |
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 chunkedSelectionSort(arr, callback) { | |
let i = 0; | |
function sortStep() { | |
let lowestIdx = i; | |
// Find lowest index | |
for (let j = i + 1; j < arr.length; j++) { | |
if (arr[j] < arr[lowestIdx]) { | |
lowestIdx = j; | |
} | |
} | |
// Base case, exit | |
if (lowestIdx === i) return callback(arr); | |
// Swap found lower value | |
const tmp = arr[i]; | |
arr[i] = arr[lowestIdx]; | |
arr[lowestIdx] = tmp; | |
// Increment pointers | |
i += 1; | |
// Yield thread, recurse | |
setTimeout(sortStep, 0); | |
} | |
sortStep(); | |
} |
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 chunkedSelectionSort(arr) { | |
return new Promise((resolve, reject) => { | |
let i = 0; | |
function sortStep() { | |
let lowestIdx = i; | |
// Find lowest index | |
for(let j = i + 1; j < arr.length; j++) { | |
if (arr[j] < arr[lowestIdx]) { | |
lowestIdx = j; | |
} | |
} | |
// Base case, exit | |
if (lowestIdx === i) return resolve(arr); | |
// Swap found lower value | |
const tmp = arr[i]; | |
arr[i] = arr[lowestIdx]; | |
arr[lowestIdx] = tmp; | |
// Increment pointers | |
i += 1; | |
// Yield thread, recurse | |
setTimeout(sortStep, 0); | |
} | |
sortStep(); | |
}); | |
} |
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 chunkedSelectionSort(arr, steps = 1000) { | |
return new Promise((resolve, reject) => { | |
let i = 0; | |
let j = i + 1; | |
let lowestIdx = i; | |
function sortStep() { | |
// Find lowest index | |
while(j < arr.length) { | |
if (arr[j] < arr[lowestIdx]) { | |
lowestIdx = j; | |
} | |
j += 1; | |
if (j % steps === 0) { | |
// Yield thread, recurse | |
setTimeout(sortStep, 0); | |
} | |
} | |
// Base case, exit | |
if (lowestIdx === i) return resolve(arr); | |
// Swap found lower value | |
const tmp = arr[i]; | |
arr[i] = arr[lowestIdx]; | |
arr[lowestIdx] = tmp; | |
// Increment pointers | |
i += 1; | |
j = i + 1; | |
lowestIdx = i; | |
} | |
sortStep(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment