mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00

* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
61 lines
1.5 KiB
JavaScript
61 lines
1.5 KiB
JavaScript
/**
|
|
* Pure Implementation of Binary Search Algorithm
|
|
*
|
|
* Binary insertion sort is a sorting algorithm similar to insertion sort,
|
|
* but instead of using linear search to find the position
|
|
* where the element should be inserted, we use binary search.
|
|
* Thus, we reduce the number of comparisons for inserting one element from O(N)
|
|
* (Time complexity in Insertion Sort) to O(log N).
|
|
*
|
|
*/
|
|
|
|
/**
|
|
* Search the key element in the array from start position to end position.
|
|
*
|
|
* @param {Array} array Array of numbers.
|
|
* @param {Number} key Value to be searched
|
|
* @param {Number} start start index position of array
|
|
* @param {Number} end end index position of array
|
|
* @return {Number} Position of the key element
|
|
*/
|
|
function binarySearch(array, key, start, end) {
|
|
if (start === end) {
|
|
if (array[start] > key) {
|
|
return start
|
|
} else {
|
|
return start + 1
|
|
}
|
|
}
|
|
|
|
if (start > end) {
|
|
return start
|
|
}
|
|
|
|
const mid = Math.floor((start + end) / 2)
|
|
|
|
if (array[mid] < key) {
|
|
return binarySearch(array, key, mid + 1, end)
|
|
} else if (array[mid] > key) {
|
|
return binarySearch(array, key, start, mid - 1)
|
|
} else {
|
|
return mid
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Binary Insertion Sort
|
|
*
|
|
* @param {Array} list List to be sorted.
|
|
* @return {Array} The sorted list.
|
|
*/
|
|
export function binaryInsertionSort(array) {
|
|
const totalLength = array.length
|
|
for (let i = 1; i < totalLength; i += 1) {
|
|
const key = array[i]
|
|
const indexPosition = binarySearch(array, key, 0, i - 1)
|
|
array.splice(i, 1)
|
|
array.splice(indexPosition, 0, key)
|
|
}
|
|
return array
|
|
}
|