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
26 changes: 19 additions & 7 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
# It returns location of x in given array arr
# if present, else returns -1
def binarySearch(arr, l, r, x):

#write your code here




#write your code here

while l<=r:
m = (l + r)// 2
if arr[m]>x:
r = m-1
elif arr[m]<x:
l = m+1
else:
return m
return -1

# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
Expand All @@ -17,6 +25,10 @@ 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")


# TC - O(logn)
# SC - O(1)
28 changes: 24 additions & 4 deletions Exercise_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,42 @@

# give you explanation for the approach
def partition(arr,low,high):


#write your code here

pivot = arr[high] # choose last element as pivot
i = low - 1 # index of smaller element

for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]

# place pivot in correct position
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)

# sort left part
quickSort(arr, low, pi - 1)

# sort right part
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:")
for i in range(n):
print ("%d" %arr[i]),
print ("%d" %arr[i])


# TC O(nlogn)
# SC O(logn) - average O(n)- worst case
32 changes: 28 additions & 4 deletions Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@
class Node:

# Function to initialise the node object
def __init__(self, data):
def __init__(self, data):
self.data = data
self.next = None

class LinkedList:

def __init__(self):
def __init__(self):
self.head = None
self.tail = None


def push(self, new_data):
def push(self, new_data):
new_node = Node(new_data)

if self.head is None:
self.head = self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node


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

print(slow.data)


# Driver code
list1 = LinkedList()
Expand All @@ -24,3 +44,7 @@ def printMiddle(self):
list1.push(3)
list1.push(1)
list1.printMiddle()


# TC O(n)
# SC O(1)
48 changes: 46 additions & 2 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,54 @@
# Python program for implementation of MergeSort
def mergeSort(arr):
# write your code here

if len(arr) > 1:
mid = len(arr) // 2

# Divide
left = arr[:mid]
right = arr[mid:]

# Recursively sort both halves
mergeSort(left)
mergeSort(right)

# Merge the two sorted halves
i = j = k = 0

# Compare elements from left and right
while i < len(left) and j < len(right):
if left[i] < right[j]:
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
k += 1

# Copy remaining elements of left
while i < len(left):
arr[k] = left[i]
i += 1
k += 1

# Copy remaining elements of right
while j < len(right):
arr[k] = right[j]
j += 1
k += 1

#write your code here


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

#write your code here

for x in arr:
print(x, end=" ")
print()


# driver code to test the above code
if __name__ == '__main__':
arr = [12, 11, 13, 5, 6, 7]
Expand All @@ -16,3 +57,6 @@ def printList(arr):
mergeSort(arr)
print("Sorted array is: ", end="\n")
printList(arr)

# TC - O(nlogn)
# SC - O(n)
34 changes: 32 additions & 2 deletions Exercise_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,39 @@

# This function is same in both iterative and recursive
def partition(arr, l, h):
#write your code here
#write your code here
pivot = arr[h]
i = l

for j in range(l, h):
if arr[j] <= pivot:
arr[i], arr[j] = arr[j], arr[i]
i += 1

arr[i], arr[h] = arr[h], arr[i]
return i


def quickSortIterative(arr, l, h):
#write your code here
# write your code here
stack = [(l, h)]

while stack:
l, h = stack.pop()

if l >= h:
continue

p = partition(arr, l, h)

stack.append((l, p - 1))
stack.append((p + 1, h))


# Driver code (NO INDENTATION)
arr = [10, 7, 8, 9, 1, 5]
quickSortIterative(arr, 0, len(arr) - 1)
print(arr)

# TC - O(nlogn)
# SC - O(logn) - average O(n^2) worst case