Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
# if present, else returns -1
def binarySearch(arr, l, r, x):

#write your code here
while l <= r:
m = l + (r - l) // 2
if arr[m] == x:
return m
if arr[m] < x:
l = m + 1
else:
r = m - 1
return -1



Expand All @@ -17,6 +25,6 @@ def binarySearch(arr, l, r, x):
result = binarySearch(arr, 0, len(arr)-1, x)

if result != -1:
print "Element is present at index % d" % result
print("Element is present at index % d" % result)
else:
print "Element is not present in array"
print("Element is not present in array")
18 changes: 14 additions & 4 deletions Exercise_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@
def partition(arr,low,high):


#write your code here
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[high] = arr[high], arr[i+1]
return i + 1


# Function to do Quick sort
def quickSort(arr,low,high):

#write your code here
if low < high:
pi = partition(arr, low, high)
quickSort(arr, low, pi - 1)
quickSort(arr, pi + 1, high)

# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
n = len(arr)
quickSort(arr,0,n-1)
print ("Sorted array is:")
print("Sorted array is:")
for i in range(n):
print ("%d" %arr[i]),
print("%d" % arr[i])


17 changes: 14 additions & 3 deletions Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@ class Node:

# Function to initialise the node object
def __init__(self, data):

self.data = data
self.next = None
class LinkedList:

def __init__(self):

self.head = None

def push(self, new_data):

new_node = Node(new_data)
new_node.next = self.head
self.head = new_node

# Function to get the middle of
# the linked list
def printMiddle(self):
if self.head is None:
return
slow = self.head
fast = self.head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
print(slow.data)

# Driver code
list1 = LinkedList()
Expand Down
32 changes: 30 additions & 2 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
# Python program for implementation of MergeSort
def mergeSort(arr):

#write your code here
if len(arr) > 1:
mid = len(arr) // 2
L = arr[:mid]
R = arr[mid:]

mergeSort(L)
mergeSort(R)

i = j = k = 0
while i < len(L) and j < len(R):
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1

while i < len(L):
arr[k] = L[i]
i += 1
k += 1

while j < len(R):
arr[k] = R[j]
j += 1
k += 1

# Code to print the list
def printList(arr):

#write your code here
for i in range(len(arr)):
print(arr[i], end=" ")
print()

# driver code to test the above code
if __name__ == '__main__':
Expand Down
20 changes: 18 additions & 2 deletions Exercise_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,25 @@

# This function is same in both iterative and recursive
def partition(arr, l, h):
#write your code here
pivot = arr[h]
i = l - 1
for j in range(l, h):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[h] = arr[h], arr[i+1]
return i + 1


def quickSortIterative(arr, l, h):
#write your code here
stack = []
stack.append((l, h))
while stack:
l, h = stack.pop()
if l < h:
p = partition(arr, l, h)
if p - 1 > l:
stack.append((l, p - 1))
if p + 1 < h:
stack.append((p + 1, h))