diff --git a/allalgorithms/searches/__init__.py b/allalgorithms/searches/__init__.py index dad7119..9f9d0fa 100644 --- a/allalgorithms/searches/__init__.py +++ b/allalgorithms/searches/__init__.py @@ -1 +1,2 @@ from .binary_search import * +from .jump_search import * diff --git a/allalgorithms/searches/jump_search.py b/allalgorithms/searches/jump_search.py new file mode 100644 index 0000000..39a0cc0 --- /dev/null +++ b/allalgorithms/searches/jump_search.py @@ -0,0 +1,25 @@ +# -*- coding: UTF-8 -*- +# +# Jump search works for a sorted array. +# The All â–²lgorithms library for python +# +# Contributed by: pieromoto +# Github: @pieromoto +# +import math + +def jump_search( arr, query): + arr_len = len(arr) + prev = 0 + step = int(math.sqrt(arr_len)) + + for i in range(step-1, arr_len, step): + if(arr[i] >= query): + break + prev = i + + for j in range(prev, arr_len): + if(arr[j] == query): + return j + + return None \ No newline at end of file diff --git a/changelog.md b/changelog.md index 71baf9e..36058d9 100644 --- a/changelog.md +++ b/changelog.md @@ -28,3 +28,4 @@ Added: - Pigeonhole Sort - Selection Sort - Stooge Sort + - Jump Search diff --git a/docs/readme.md b/docs/readme.md index f755458..e29f3da 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -59,6 +59,7 @@ print(binary_search(arr, 3)) - ### Searches - [Binary Search](searches/binary-search) + - [Jump Search](searches/jump-search) - ### Sorting - [Bubble Sort](sorting/bubble-sort) - [Cocktail Shaker Sort](sorting/cocktail-shaker-sort) diff --git a/docs/searches/jump-search.md b/docs/searches/jump-search.md new file mode 100644 index 0000000..d439278 --- /dev/null +++ b/docs/searches/jump-search.md @@ -0,0 +1,36 @@ +# Jump Search + +In computer science, jump search is a search algorithm for sorted array which find the element by jumping ahead by fixed steps or skipping some elements in place of searching all elements. + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.searches import jump_search + +arr = [-2, 1, 2, 7, 10, 77] + +print(jump_search(arr, 7)) +# -> 3 + +print(jump_search(arr, 3)) +# -> None +``` + +## API + +### jump_search(array, query) + +> Return array index if its found, otherwise returns `None` + +##### Params: + +- `arr`: Sorted Array +- `query`: Element to search for in the array + + diff --git a/readme.md b/readme.md index 1d5be4f..e31cefe 100644 --- a/readme.md +++ b/readme.md @@ -59,6 +59,7 @@ print(binary_search(arr, 3)) - ### Searches - [Binary Search](https://python.allalgorithms.com/searches/binary-search) + - [Jump Search](https://python.allalgorithms.com/searches/jump-search) - ### Sorting - [Bubble Sort](https://python.allalgorithms.com/sorting/bubble-sort) - [Cocktail Shaker Sort](https://python.allalgorithms.com/sorting/cocktail-shaker-sort) diff --git a/tests/test_searches.py b/tests/test_searches.py index 622beeb..dddd19f 100644 --- a/tests/test_searches.py +++ b/tests/test_searches.py @@ -1,6 +1,6 @@ import unittest -from allalgorithms.searches import binary_search +from allalgorithms.searches import (binary_search, jump_search) class TestSearches(unittest.TestCase): @@ -12,6 +12,13 @@ def test_binary_search(self): self.assertEqual(None, binary_search(arr, 8)) self.assertEqual(None, binary_search(arr, -1)) + def test_jump_search(self): + arr = [1, 2, 3, 7, 10, 19, 27, 77] + self.assertEqual(3, binary_search(arr, 7)) + self.assertEqual(7, binary_search(arr, 77)) + self.assertEqual(None, binary_search(arr, 8)) + self.assertEqual(None, binary_search(arr, -1)) + if __name__ == '__main__': unittest.main()
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: