-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #636 from PranikaBaby/main
Updated SearchMain.py
- Loading branch information
Showing
2 changed files
with
57 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,66 @@ | ||
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 | ||
return Node(x) | ||
|
||
def middle(start, last): | ||
|
||
if (start == None): | ||
if start is None: | ||
return None | ||
|
||
slow = start | ||
fast = start . next | ||
|
||
while (fast != last): | ||
|
||
fast = fast . next | ||
if (fast != last): | ||
fast = start.next | ||
|
||
slow = slow . next | ||
fast = fast . next | ||
while fast != last: | ||
fast = fast.next | ||
if fast != last: | ||
slow = slow.next | ||
fast = fast.next | ||
|
||
return slow | ||
|
||
def binarySearch(head,value): | ||
|
||
def binarySearch(head, value): | ||
start = head | ||
last = None | ||
|
||
while True : | ||
|
||
while True: | ||
mid = middle(start, last) | ||
|
||
if (mid == None): | ||
if mid is None: | ||
return None | ||
|
||
if (mid . data == value): | ||
if mid.data == value: | ||
return mid | ||
|
||
elif (mid . data < value): | ||
start = mid . next | ||
|
||
elif mid.data < value: | ||
start = mid.next | ||
else: | ||
last = mid | ||
|
||
if not (last == None or last != start): | ||
# Correct break condition | ||
if last == start: | ||
break | ||
|
||
return None | ||
|
||
# Create a sorted doubly linked list | ||
head = newNode(2) | ||
head.next = newNode(5) | ||
head.next.prev = head # Set the prev pointer | ||
head.next.next = newNode(7) | ||
head.next.next.prev = head.next # Set the prev pointer | ||
head.next.next.next = newNode(11) | ||
head.next.next.next.prev = head.next.next # Set the prev pointer | ||
head.next.next.next.next = newNode(15) | ||
head.next.next.next.next.prev = head.next.next.next # Set the prev pointer | ||
head.next.next.next.next.next = newNode(18) | ||
head.next.next.next.next.next.prev = head.next.next.next.next # Set the prev pointer | ||
|
||
value = 9 | ||
if (binarySearch(head, value) == None): | ||
result = binarySearch(head, value) | ||
if result is None: | ||
print("Element not Found\n") | ||
else: | ||
print("Element Found") | ||
print("Element Found:", result.data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters