Skip to content

Commit 1d673d7

Browse files
authored
Merge branch 'master' into master
2 parents 14bf84b + 18e59a8 commit 1d673d7

File tree

7 files changed

+82
-12
lines changed

7 files changed

+82
-12
lines changed

allalgorithms/searches/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from .binary_search import *
22
from .fibonacci_search import *
3+
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: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ Date: October 9, 2018
88

99
### Algorithms:
1010

11-
- ### Sorting
12-
- Bubble Sort
1311
- ### Searches
1412
- Merge Sort
1513

14+
- ### Sorting
15+
- Bubble Sort
16+
1617
# Version `0.0.1`
1718

1819
Date: TO ADDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD, 2018
@@ -22,16 +23,13 @@ Date: TO ADDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD, 2018
2223
Added:
2324

2425
- ### Searches
26+
- Fibonacci Search
27+
- Jump Search
28+
29+
- ### Sorting
2530
- Bubble Sort
2631
- Cocktail Shaker Sort
2732
- Insertion Sort
2833
- Pigeonhole Sort
2934
- Selection Sort
30-
- Stooge Sort
31-
32-
# Version `0.0.2`
33-
34-
Date: October 10, 2018
35-
36-
- ### Sorting
37-
- Fibonacci Search
35+
- Stooge Sort

docs/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](searches/binary-search)
62+
- [Jump Search](searches/jump-search)
6263
- ### Sorting
6364
- [Bubble Sort](sorting/bubble-sort)
6465
- [Cocktail Shaker Sort](sorting/cocktail-shaker-sort)

docs/searches/jump-search.md

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

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ print(binary_search(arr, 3))
6060
- ### Searches
6161
- [Binary Search](https://python.allalgorithms.com/searches/binary-search)
6262
- [Fibonacci Search](https://python.allalgorithms.com/searches/fibonacci-search)
63+
- [Jump Search](https://python.allalgorithms.com/searches/jump-search)
64+
6365
- ### Sorting
6466
- [Bubble Sort](https://python.allalgorithms.com/sorting/bubble-sort)
6567
- [Cocktail Shaker Sort](https://python.allalgorithms.com/sorting/cocktail-shaker-sort)

tests/test_searches.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from allalgorithms.searches import (
44
binary_search,
55
fibonacci_search
6+
jump_search
67
)
78

89
class TestSearches(unittest.TestCase):
@@ -20,7 +21,13 @@ def test_fibonacci_search(self):
2021
self.assertEqual(7, fibonacci_search(arr, 77))
2122
self.assertEqual(None, fibonacci_search(arr, 8))
2223
self.assertEqual(None, fibonacci_search(arr, -1))
23-
24+
25+
def test_jump_search(self):
26+
arr = [1, 2, 3, 7, 10, 19, 27, 77]
27+
self.assertEqual(3, binary_search(arr, 7))
28+
self.assertEqual(7, binary_search(arr, 77))
29+
self.assertEqual(None, binary_search(arr, 8))
30+
self.assertEqual(None, binary_search(arr, -1))
2431

2532
if __name__ == '__main__':
26-
unittest.main()
33+
unittest.main()

0 commit comments

Comments
 (0)