Skip to content

Commit cba2a67

Browse files
committed
Added jump search
1 parent ee71bfe commit cba2a67

File tree

6 files changed

+81
-1
lines changed

6 files changed

+81
-1
lines changed

allalgorithms/searches/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from .binary_search import *
2+
from .jump_search import *

allalgorithms/searches/jump_search.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Jump search works for a sorted array.
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: pieromoto
7+
# Github: @pieromoto
8+
#
9+
import math
10+
11+
def jump_search( arr, query):
12+
arr_len = len(arr)
13+
prev = 0
14+
step = int(math.sqrt(arr_len))
15+
16+
for i in range(step-1, arr_len, step):
17+
if(arr[i] >= query):
18+
break
19+
prev = i
20+
21+
for j in range(prev, arr_len):
22+
if(arr[j] == query):
23+
return j
24+
25+
return None

changelog.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,14 @@ Added:
2828
- Pigeonhole Sort
2929
- Selection Sort
3030
- Stooge Sort
31+
32+
# Version `0.0.2`
33+
34+
Date: October 10, 2018
35+
36+
### Algorithms:
37+
38+
Added:
39+
40+
- ### Searches
41+
- Jump Search

docs/searches/jump-search.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Jump Search
2+
3+
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.
4+
## Install
5+
6+
```
7+
pip install allalgorithms
8+
```
9+
10+
## Usage
11+
12+
```py
13+
from allalgorithms.searches import jump_search
14+
15+
arr = [-2, 1, 2, 7, 10, 77]
16+
17+
print(jump_search(arr, 7))
18+
# -> 3
19+
20+
print(jump_search(arr, 3))
21+
# -> None
22+
```
23+
24+
## API
25+
26+
### binary_search(array, query)
27+
28+
> Return array index if its found, otherwise returns `None`
29+
30+
##### Params:
31+
32+
- `arr`: Sorted Array
33+
- `query`: Element to search for in the array
34+
35+

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ print(binary_search(arr, 3))
5959

6060
- ### Searches
6161
- [Binary Search](https://python.allalgorithms.com/searches/binary-search)
62+
- [Jump Search](https://python.allalgorithms.com/searches/jump-search)
6263
- ### Sorting
6364
- [Bubble Sort](https://python.allalgorithms.com/sorting/bubble-sort)
6465
- [Cocktail Shaker Sort](https://python.allalgorithms.com/sorting/cocktail-shaker-sort)

tests/test_searches.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from allalgorithms.searches import binary_search
3+
from allalgorithms.searches import (binary_search, jump_search)
44

55

66
class TestSearches(unittest.TestCase):
@@ -12,6 +12,13 @@ def test_binary_search(self):
1212
self.assertEqual(None, binary_search(arr, 8))
1313
self.assertEqual(None, binary_search(arr, -1))
1414

15+
def test_jump_search(self):
16+
arr = [1, 2, 3, 7, 10, 19, 27, 77]
17+
self.assertEqual(3, binary_search(arr, 7))
18+
self.assertEqual(7, binary_search(arr, 77))
19+
self.assertEqual(None, binary_search(arr, 8))
20+
self.assertEqual(None, binary_search(arr, -1))
21+
1522

1623
if __name__ == '__main__':
1724
unittest.main()

0 commit comments

Comments
 (0)