Skip to content

Commit 18e59a8

Browse files
authored
Merge pull request abranhe#17 from pieromoto/jump_search
Added jump search
2 parents ee71bfe + 2ae94f9 commit 18e59a8

File tree

7 files changed

+73
-1
lines changed

7 files changed

+73
-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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ Added:
2828
- Pigeonhole Sort
2929
- Selection Sort
3030
- Stooge Sort
31+
- Jump Search

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: 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy