Skip to content

Commit 052ae3a

Browse files
Merge pull request #651 from dhrjklt/dhrjklt
Jump search in python2
2 parents 5006703 + c9aab53 commit 052ae3a

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Search/BinarySearch/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Binary Search
2+
3+
Given a sorted array arr[] of n elements, write a function to search a given element x in arr[].
4+
A simple approach is to do linear search.The time complexity of above algorithm is O(n). Another approach to perform the same task is using Binary Search.
5+
6+
Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.
7+
8+
The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n).
9+
10+
We basically ignore half of the elements just after one comparison.
11+
12+
1.Compare x with the middle element.
13+
14+
2.If x matches with middle element, we return the mid index.
15+
16+
3.Else If x is greater than the mid element, then x can only lie in right half subarray after the mid element. So we recur for right half.
17+
18+
4.Else (x is smaller) recur for the left half.

Search/JumpSearch/jumpSearch.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import math
2+
def jump_search(arr,search):
3+
4+
j_block = len(arr)
5+
low = 0
6+
interval = int(math.sqrt(j_block))
7+
for i in range(0,j_block,interval):
8+
if arr[i] < search:
9+
low = i
10+
elif arr[i] == search:
11+
return i
12+
else:
13+
break # bigger number is found
14+
c=low
15+
for j in arr[low:]:
16+
if j==search:
17+
return c
18+
c+=1
19+
return "Not found"
20+
21+
arr = [ 0, 1, 1, 2, 3, 5, 8, 13, 21,34, 55, 89, 144, 233, 377, 610 ]
22+
23+
search_block = 55
24+
res = jump_search(arr, search_block)
25+
print("Number" , search_block, "is at index" ,"%.0f"%res)

0 commit comments

Comments
 (0)