Skip to content

Commit

Permalink
Merge 1b4a15e into 8f68ac8
Browse files Browse the repository at this point in the history
  • Loading branch information
hsi1032 committed Jun 5, 2018
2 parents 8f68ac8 + 1b4a15e commit fc75730
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README_KR.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
English(README_EN.md) | [简体中文](README_CN.md) | [Deutsch](README_GE.md) | 한국어 | [日本語](README_JP.md)
[English](README.md) | [简体中文](README_CN.md) | [Deutsch](README_GE.md) | 한국어 | [日本語](README_JP.md)

Python 버전 자료구조 및 알고리즘
=========================================
Expand All @@ -11,7 +11,7 @@ Python 3로 구현한 간단하고 명확한 자료구조와 알고리즘들의

## 테스트 종류들

### 단위별 테스트 사용
### unittest 사용
아래 명시된 모든 테스트 실행하기:

$ python3 -m unittest discover tests
Expand Down Expand Up @@ -206,6 +206,7 @@ if __name__ == "__main__":
- [search_range](algorithms/search/search_range.py)
- [find_min_rotate](algorithms/search/find_min_rotate.py)
- [search_rotate](algorithms/search/search_rotate.py)
- [jump_search](algorithms/search/jump_search.py)
- [set : 집합](algorithms/set)
- [randomized_set](algorithms/set/randomized_set.py)
- [set_covering](algorithms/set/set_covering.py)
Expand Down
1 change: 1 addition & 0 deletions algorithms/search/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
from .search_range import *
from .find_min_rotate import *
from .search_rotate import *
from .jump_search import *
40 changes: 40 additions & 0 deletions algorithms/search/jump_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import math

def jump_search(arr,target):
"""Jump Search
Worst-case Complexity: O(√n) (root(n))
All items in list must be sorted like binary search
Find block that contains target value and search it linearly in that block
It returns a first target value in array
reference: https://en.wikipedia.org/wiki/Jump_search
"""
n = len(arr)
block_size = int(math.sqrt(n))
block_prev = 0
block= block_size

# return -1 means that array doesn't contain taget value
# find block that contains target value

if arr[n - 1] < target:
return -1
while block <= n and arr[block - 1] < target:
block_prev = block
block += block_size

# find target value in block

while arr[block_prev] < target :
block_prev += 1
if block_prev == min(block, n) :
return -1

# if there is target value in array, return it

if arr[block_prev] == target :
return block_prev
else :
return -1
10 changes: 9 additions & 1 deletion tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
two_sum, two_sum1, two_sum2,
search_range,
find_min_rotate, find_min_rotate_recur,
search_rotate, search_rotate_recur
search_rotate, search_rotate_recur,
jump_search
)

import unittest
Expand Down Expand Up @@ -88,6 +89,13 @@ def test_search_rotate(self):
self.assertEqual(8, search_rotate_recur(array, 0, 11, 5))
self.assertEqual(-1, search_rotate_recur(array, 0, 11, 9))

def test_jump_search(self):
array = [1, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 6]
self.assertEqual(10, jump_search(array, 5))
self.assertEqual(2, jump_search(array, 3))
self.assertEqual(-1, jump_search(array, 7))
self.assertEqual(-1, jump_search(array, -1))

if __name__ == '__main__':

unittest.main()

0 comments on commit fc75730

Please sign in to comment.