From cba2a6711f82b3493683c7d8dbe87d5f5fff531a Mon Sep 17 00:00:00 2001 From: pieromoto Date: Thu, 11 Oct 2018 00:06:18 +0200 Subject: [PATCH 1/4] Added jump search --- allalgorithms/searches/__init__.py | 1 + allalgorithms/searches/jump_search.py | 25 +++++++++++++++++++ changelog.md | 11 +++++++++ docs/searches/jump-search.md | 35 +++++++++++++++++++++++++++ readme.md | 1 + tests/test_searches.py | 9 ++++++- 6 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 allalgorithms/searches/jump_search.py create mode 100644 docs/searches/jump-search.md 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..25f8cb5 100644 --- a/changelog.md +++ b/changelog.md @@ -28,3 +28,14 @@ Added: - Pigeonhole Sort - Selection Sort - Stooge Sort + +# Version `0.0.2` + +Date: October 10, 2018 + +### Algorithms: + +Added: + +- ### Searches + - Jump Search diff --git a/docs/searches/jump-search.md b/docs/searches/jump-search.md new file mode 100644 index 0000000..f7ece99 --- /dev/null +++ b/docs/searches/jump-search.md @@ -0,0 +1,35 @@ +# 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 + +### binary_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() From 14116dc4320e9d4ab6d4a7274ea97577f0de09c2 Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Wed, 10 Oct 2018 21:10:53 -0400 Subject: [PATCH 2/4] fixed api docs jump_search --- docs/searches/jump-search.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/searches/jump-search.md b/docs/searches/jump-search.md index f7ece99..d439278 100644 --- a/docs/searches/jump-search.md +++ b/docs/searches/jump-search.md @@ -1,6 +1,7 @@ # 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 ``` @@ -23,7 +24,7 @@ print(jump_search(arr, 3)) ## API -### binary_search(array, query) +### jump_search(array, query) > Return array index if its found, otherwise returns `None` From 53c6a1ea66272f5c8489cf1ae3060691085af535 Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Wed, 10 Oct 2018 21:11:59 -0400 Subject: [PATCH 3/4] jump_search will be included on `v0.0.1` --- changelog.md | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/changelog.md b/changelog.md index 25f8cb5..36058d9 100644 --- a/changelog.md +++ b/changelog.md @@ -28,14 +28,4 @@ Added: - Pigeonhole Sort - Selection Sort - Stooge Sort - -# Version `0.0.2` - -Date: October 10, 2018 - -### Algorithms: - -Added: - -- ### Searches - Jump Search From 2ae94f9c86a82c15230d76c6ff7e901ff0cb93bc Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Wed, 10 Oct 2018 21:12:58 -0400 Subject: [PATCH 4/4] add jump search to tree --- docs/readme.md | 1 + 1 file changed, 1 insertion(+) 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) 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