Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2e65aa2

Browse files
Improve explanatory comments; Add reference
1 parent 6f24f5e commit 2e65aa2

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

‎algorithms/Searching/jump_search.m‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
function found_at = jump_search(input_array, search_key)
2-
% Contributed by - Harshit Pant, harshitpant83@gmail.com
2+
% Contributed by - Harshit Pant, harshitpant83@gmail.com
3+
% Reference - https://en.wikipedia.org/wiki/Jump_search
34

4-
% input_array - The input array, should be sorted in ascending order.
5-
% 'input_array' can contain -ve numbers as well as non-integers.
5+
% input_array - Holds the array in which the 'search_key' is to be searched.
6+
% It should be sorted in ascending order.
7+
% It can contain -ve numbers as well as non-integers.
68
% search_key - The value to be searched in the 'input_array'.
79
% found_at - The index at which 'search_key' is found in 'input_array'.
810
% -1 is returned if 'search_key' is not found in 'input_array'.
911

1012
array_length = length(input_array);
1113
found_at = -1;
1214

15+
% Finding the optimum block_size to be jumped.
1316
block_size = sqrt(array_length);
1417
block_size = round(block_size);
1518

19+
% low and high denote the lower
20+
% and upper bound of a certain block.
1621
low = 1;
1722
high = 1 + block_size;
1823

24+
% Finding the block where the 'search_key' is present
25+
% if low >= array_length, the 'search_key' is not found
26+
% in the 'input_array', thus, -1 is returned.
1927
while input_array(min(high, array_length)) < search_key
2028
low = high;
2129
high = high + block_size;
@@ -24,22 +32,25 @@
2432
endif;
2533
endwhile;
2634

35+
% Now that the required block is found,
36+
% running a linear search within the block
37+
% to find the 'search_key'
2738
while input_array(low) < search_key
2839
low = low + 1;
2940
if low > min(high, array_length)
3041
return;
3142
endif;
3243
endwhile;
3344

45+
% Checks if the 'search_key' was found within
46+
% the block. If found, the index is returned.
47+
% If not -1 is returned.
3448
if input_array(low) == search_key
3549
found_at = low;
3650
endif;
3751

3852
endfunction;
3953

40-
4154
% TEST:
42-
% Save this file to your local computer, then use the command:
43-
% jump_search([-11.1, -3.3, -1.3, 0.1, 1.5, 3.5, 3.9, 5.5, 7.5, 9.6, 13.7, 21.3, 35.9], 7.5)
44-
% This should return, ans = 9.
45-
% TODO - Transfer this test when automated testing functionality is added to this repository.
55+
% jump_search([-11.1, -3.3, -1.3, 0.1, 1.5, 3.5, 3.9,...
56+
% 5.5, 7.5, 9.6, 13.7, 21.3, 35.9], 7.5) == 9

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /