forked from PriyaGhosal/SkillWise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchMain.py
69 lines (48 loc) · 1.18 KB
/
SearchMain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
def newNode(x):
temp = Node(0)
temp.data = x
temp.next = None
return temp
def middle(start, last):
if (start == None):
return None
slow = start
fast = start . next
while (fast != last):
fast = fast . next
if (fast != last):
slow = slow . next
fast = fast . next
return slow
def binarySearch(head,value):
start = head
last = None
while True :
mid = middle(start, last)
if (mid == None):
return None
if (mid . data == value):
return mid
elif (mid . data < value):
start = mid . next
else:
last = mid
if not (last == None or last != start):
break
return None
head = newNode(2)
head.next = newNode(5)
head.next.next = newNode(7)
head.next.next.next = newNode(11)
head.next.next.next.next = newNode(15)
head.next.next.next.next.next = newNode(18)
value = 9
if (binarySearch(head, value) == None):
print("Element not Found\n")
else:
print("Element Found")