Skip to content

Commit 10deec3

Browse files
committed
Add insertIntoSortedArray utility function
Uses binary search to insert an element into a sorted array in logN time
1 parent 52559f8 commit 10deec3

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

src/sortutils.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Inserts an element into a sorted array.
3+
* @template T
4+
* @param {Array<T>} array - The sorted array to insert into.
5+
* @param {T} element - The element to insert.
6+
* @param {(a: T, b: T) => number} compare - The comparison function used to sort the array.
7+
* @returns {number} The index at which the element was inserted.
8+
*/
9+
export declare function insertIntoSortedArray<T>(array: Array<T>, element: T, compare: (a: T, b: T) => number): number;

src/sortutils.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
Object.defineProperty(exports, '__esModule', { value: true });
3+
exports.insertIntoSortedArray = void 0;
4+
/**
5+
* Inserts an element into a sorted array.
6+
* @template T
7+
* @param {Array<T>} array - The sorted array to insert into.
8+
* @param {T} element - The element to insert.
9+
* @param {(a: T, b: T) => number} compare - The comparison function used to sort the array.
10+
* @returns {number} The index at which the element was inserted.
11+
*/
12+
function insertIntoSortedArray(array, element, compare) {
13+
let high = array.length - 1;
14+
let low = 0;
15+
let mid;
16+
let highElement, lowElement, midElement;
17+
let compareHigh, compareLow, compareMid;
18+
let targetIndex;
19+
while (targetIndex === undefined) {
20+
if (high < low) {
21+
targetIndex = low;
22+
continue;
23+
}
24+
mid = Math.floor((low + high) / 2);
25+
highElement = array[high];
26+
lowElement = array[low];
27+
midElement = array[mid];
28+
compareHigh = compare(element, highElement);
29+
compareLow = compare(element, lowElement);
30+
compareMid = compare(element, midElement);
31+
if (low === high) {
32+
// Target index is either to the left or right of element at low
33+
if (compareLow <= 0) targetIndex = low;
34+
else targetIndex = low + 1;
35+
continue;
36+
}
37+
if (compareHigh >= 0) {
38+
// Target index is to the right of high
39+
low = high;
40+
continue;
41+
}
42+
if (compareLow <= 0) {
43+
// Target index is to the left of low
44+
high = low;
45+
continue;
46+
}
47+
if (compareMid <= 0) {
48+
// Target index is to the left of mid
49+
high = mid;
50+
continue;
51+
}
52+
// Target index is to the right of mid
53+
low = mid + 1;
54+
}
55+
array.splice(targetIndex, 0, element);
56+
return targetIndex;
57+
}
58+
exports.insertIntoSortedArray = insertIntoSortedArray;

ts_src/sortutils.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Inserts an element into a sorted array.
3+
* @template T
4+
* @param {Array<T>} array - The sorted array to insert into.
5+
* @param {T} element - The element to insert.
6+
* @param {(a: T, b: T) => number} compare - The comparison function used to sort the array.
7+
* @returns {number} The index at which the element was inserted.
8+
*/
9+
export function insertIntoSortedArray<T>(
10+
array: Array<T>,
11+
element: T,
12+
compare: (a: T, b: T) => number,
13+
) {
14+
let high = array.length - 1;
15+
let low = 0;
16+
let mid;
17+
let highElement, lowElement, midElement;
18+
let compareHigh, compareLow, compareMid;
19+
let targetIndex;
20+
while (targetIndex === undefined) {
21+
if (high < low) {
22+
targetIndex = low;
23+
continue;
24+
}
25+
26+
mid = Math.floor((low + high) / 2);
27+
28+
highElement = array[high];
29+
lowElement = array[low];
30+
midElement = array[mid];
31+
32+
compareHigh = compare(element, highElement);
33+
compareLow = compare(element, lowElement);
34+
compareMid = compare(element, midElement);
35+
36+
if (low === high) {
37+
// Target index is either to the left or right of element at low
38+
if (compareLow <= 0) targetIndex = low;
39+
else targetIndex = low + 1;
40+
continue;
41+
}
42+
43+
if (compareHigh >= 0) {
44+
// Target index is to the right of high
45+
low = high;
46+
continue;
47+
}
48+
if (compareLow <= 0) {
49+
// Target index is to the left of low
50+
high = low;
51+
continue;
52+
}
53+
54+
if (compareMid <= 0) {
55+
// Target index is to the left of mid
56+
high = mid;
57+
continue;
58+
}
59+
60+
// Target index is to the right of mid
61+
low = mid + 1;
62+
}
63+
64+
array.splice(targetIndex, 0, element);
65+
return targetIndex;
66+
}

0 commit comments

Comments
 (0)