|
1 | | -function found_at = jump_search(arr, value) |
| 1 | +function found_at = jump_search(input_array, search_key) |
2 | 2 | % Contributed by - Harshit Pant, harshitpant83@gmail.com |
3 | 3 |
|
4 | | - % arr - The input array, should be sorted in ascending order. |
5 | | - % 'arr' can contain -ve numbers as well as non-integers. |
6 | | - % value - The value to be searched in the array. |
7 | | - % found_at - The index at which 'value' is found in 'arr'. |
8 | | - % -1 is returned if 'value' is not found in 'arr'. |
| 4 | + % input_array - The input array, should be sorted in ascending order. |
| 5 | + % 'input_array' can contain -ve numbers as well as non-integers. |
| 6 | + % search_key - The value to be searched in the 'input_array'. |
| 7 | + % found_at - The index at which 'search_key' is found in 'input_array'. |
| 8 | + % -1 is returned if 'search_key' is not found in 'input_array'. |
9 | 9 |
|
10 | | - n = length(arr); |
| 10 | + array_length = length(input_array); |
11 | 11 | found_at = -1; |
12 | 12 |
|
13 | | - % 'm' holds the block size |
14 | | - m = sqrt(n); |
15 | | - m = round(m); |
| 13 | + block_size = sqrt(array_length); |
| 14 | + block_size = round(block_size); |
| 15 | + |
16 | 16 | low = 1; |
17 | | - high = 1 + m; |
| 17 | + high = 1 + block_size; |
18 | 18 |
|
19 | | - while arr(min(high, n)) < value |
| 19 | + while input_array(min(high, array_length)) < search_key |
20 | 20 | low = high; |
21 | | - high = high + m; |
22 | | - if low >= n |
| 21 | + high = high + block_size; |
| 22 | + if low >= array_length |
23 | 23 | return; |
24 | 24 | endif; |
25 | 25 | endwhile; |
26 | 26 |
|
27 | | - while arr(low) < value |
| 27 | + while input_array(low) < search_key |
28 | 28 | low = low + 1; |
29 | | - if low > min(high, n) |
| 29 | + if low > min(high, array_length) |
30 | 30 | return; |
31 | 31 | endif; |
32 | 32 | endwhile; |
33 | 33 |
|
34 | | - if arr(low) == value |
| 34 | + if input_array(low) == search_key |
35 | 35 | found_at = low; |
36 | 36 | endif; |
37 | 37 |
|
|
0 commit comments