( sortedArray: Array<T>, value: T, compareFn: (a: T, b: T) => number, )
| 6 | * @returns The index where the value should be inserted to maintain order |
| 7 | */ |
| 8 | export function findInsertPositionInArray<T>( |
| 9 | sortedArray: Array<T>, |
| 10 | value: T, |
| 11 | compareFn: (a: T, b: T) => number, |
| 12 | ): number { |
| 13 | let left = 0 |
| 14 | let right = sortedArray.length |
| 15 | |
| 16 | while (left < right) { |
| 17 | const mid = Math.floor((left + right) / 2) |
| 18 | const comparison = compareFn(sortedArray[mid]!, value) |
| 19 | |
| 20 | if (comparison < 0) { |
| 21 | left = mid + 1 |
| 22 | } else { |
| 23 | right = mid |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | return left |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Finds the correct insert position for a value in a sorted tuple array using binary search |
no outgoing calls
no test coverage detected