forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: insertion sort with binary search (TheAlgorithms#1182)
- Loading branch information
syedjafer_k
authored
Oct 16, 2022
1 parent
8c847e3
commit 18baef8
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* 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 | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { binaryInsertionSort } from '../BinaryInsertionSort' | ||
|
||
describe('BinaryInsertionSort', () => { | ||
it('should sort arrays correctly', () => { | ||
expect(binaryInsertionSort([5, 4, 3, 2, 1])).toEqual([1, 2, 3, 4, 5]) | ||
expect(binaryInsertionSort([7, 9, 4, 3, 5])).toEqual([3, 4, 5, 7, 9]) | ||
}) | ||
}) |