Jump Search
Jump search is an algorithm for searching for a target value in a sorted array. It's similar to linear search in that it looks at every nth element, but it skips over some elements in a way that makes it more efficient.
Example in TypeScript
This function takes two arguments: an array and a target value. It uses a while loop to jump over some elements in the array by taking sqrt(n)
steps at a time. It starts with the first element and compares it with the target value. If the element is less than the target value, it moves to the next step. It keeps moving to the next step until it finds an element that is greater than or equal to the target value. Then it uses another while loop to check from the previous step until it finds the target value or reaches the end of the array. If it finds the target value, it returns the index. If it doesn't find the target value, it returns null
.
Usage
Jump search is an O(√n) algorithm, which is faster than linear search but slower than binary search. It is useful when the size of the array is too large to be able to scan every element using linear search, but also when the array is not big enough to justify the use of more complex algorithms like binary search or interpolation search. This algorithm works only with sorted arrays.
Last updated