Skip to content

Added jump search #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions allalgorithms/searches/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .binary_search import *
from .jump_search import *
25 changes: 25 additions & 0 deletions allalgorithms/searches/jump_search.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ Added:
- Pigeonhole Sort
- Selection Sort
- Stooge Sort
- Jump Search
1 change: 1 addition & 0 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions docs/searches/jump-search.md
Original file line number Diff line number Diff line change
@@ -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


1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion tests/test_searches.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from allalgorithms.searches import binary_search
from allalgorithms.searches import (binary_search, jump_search)


class TestSearches(unittest.TestCase):
Expand All @@ -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()