Buckets:
| type BinarySearchComparison<T> = (candidate: T) => -1 | 0 | 1; | |
| const BinarySearch = { | |
| /** | |
| * Searches for an item in an array which matches a certain condition. | |
| * This requires the condition to only match one item in the array, | |
| * and for the array to be ordered. | |
| * | |
| * @param list The array to search. | |
| * @param comparisonFn | |
| * Called and provided a candidate item as the first argument. | |
| * Should return: | |
| * > -1 if the item should be located at a lower index than the provided item. | |
| * > 1 if the item should be located at a higher index than the provided item. | |
| * > 0 if the item is the item you're looking for. | |
| * | |
| * @returns the object if found, otherwise returns null | |
| */ | |
| search: function <T>( | |
| list: T[], | |
| comparisonFn: BinarySearchComparison<T>, | |
| ): T | null { | |
| let minIndex: number = 0; | |
| let maxIndex: number = list.length - 1; | |
| let currentIndex: number | null = null; | |
| let currentElement: T | null = null; | |
| while (minIndex <= maxIndex) { | |
| currentIndex = ((minIndex + maxIndex) / 2) | 0; | |
| currentElement = list[currentIndex]; | |
| const comparisonResult = comparisonFn(currentElement); | |
| if (comparisonResult > 0) { | |
| minIndex = currentIndex + 1; | |
| } else if (comparisonResult < 0) { | |
| maxIndex = currentIndex - 1; | |
| } else { | |
| return currentElement; | |
| } | |
| } | |
| return null; | |
| }, | |
| }; | |
| export default BinarySearch; | |
Xet Storage Details
- Size:
- 1.47 kB
- Xet hash:
- 21781a4649ec3532bc1a3fef0530cce065ec92c4f8d03ffd2a8fac62dd475453
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.