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
Binary file added .DS_Store
Binary file not shown.
25 changes: 20 additions & 5 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,35 @@

# It returns location of x in given array arr
# if present, else returns -1

#TC of binary search is O(log n) since we halve the work to be done at each division
#SC is also o(log n) as we only use the part that potentially has the element
def binarySearch(arr, l, r, x):

#write your code here


n = len(arr)
l = 0
r = n - 1

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


# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
x = 1

# Function call
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")
26 changes: 21 additions & 5 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
# Python program for implementation of Quicksort Sort
#TC is O(n^2) in the worst case (wrong pivot selection) and o(n log n) in the average of best case
#SC is o(n)

# give you explanation for the approach
def partition(arr,low,high):
p = arr[low]
i = low + 1
j = high
while True:
while i <= j and arr[i] <= p: #shifts all the elements less than pivot to left
i += 1
while i <= j and arr[j] >= p: #shifts all the elements greater than pivot to right
j -= 1
if i <= j:
arr[i], arr[j] = arr[j], arr[i] #shifting done here
else:
break
arr[low], arr[j] = arr[j], arr[low] #swaping of pivot and the jth element, point where pivot is correctly placed
return j


#write your code here


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

if low < high:
pivot = partition(arr, low, high) #calls for the position of pivot
quickSort(arr, low, pivot - 1) #recusrive call ofor left
quickSort(arr, pivot + 1, high) #recusrive call on right

#write your code here

# Driver code to test above
Expand Down
17 changes: 15 additions & 2 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
# Node class
#TC is o(n) since the fast pointer moves thorugh all the elements
#SC is o(N) for pushing, and maybe constant for the finding part, since we use only two pointers
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):

newNode = Node(new_data)
newNode.next = self.head #push the earlier head as the next element of the new node to be added
self.head = newNode #eastablish the new node as head

# Function to get the middle of
# the linked list
def printMiddle(self):
slow, fast = self.head, self.head #slow and fast pointer, bcz fast jumps twice as compared to slow, when the fast will be at the end, the slow will be in the mid
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow


# Driver code
list1 = LinkedList()
Expand All @@ -23,4 +36,4 @@ def printMiddle(self):
list1.push(2)
list1.push(3)
list1.push(1)
list1.printMiddle()
print("Middle element:", list1.printMiddle().data)
38 changes: 37 additions & 1 deletion Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
# Python program for implementation of MergeSort

#TC for mergesort in any condition is O(nlogn)
#SC is o(n)
def mergeSort(arr):
if len(arr) <= 1:
return arr

mid = (len(arr)) //2
left = arr[:mid]
right = arr[mid:]
lhalf = mergeSort(left)
rhalf = mergeSort(right)
return merge(lhalf, rhalf)

def merge(left, right):
res =[]
i, j = 0, 0
while i < len(left) and j < len(right):
if left[i] < right[j]:

res.append(left[i])
i += 1
else:
res.append(right[j])
j += 1
res.extend(left[i:])
res.extend(right[j:])
return res




#write your code here

# Code to print the list
def printList(arr):
for i in arr:
print(i, end =" ")
print()




#write your code here

Expand All @@ -13,6 +49,6 @@ def printList(arr):
arr = [12, 11, 13, 5, 6, 7]
print ("Given array is", end="\n")
printList(arr)
mergeSort(arr)
arr = mergeSort(arr)
print("Sorted array is: ", end="\n")
printList(arr)
17 changes: 15 additions & 2 deletions Exercise_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@

# This function is same in both iterative and recursive
def partition(arr, l, h):
p = arr[low]
i = low + 1
j = high
while True:
while i <= j and arr[i] <= p: #shifts all the elements less than pivot to left
i += 1
while i <= j and arr[j] >= p: #shifts all the elements greater than pivot to right
j -= 1
if i <= j:
arr[i], arr[j] = arr[j], arr[i] #shifting done here
else:
break
arr[low], arr[j] = arr[j], arr[low] #swaping of pivot and the jth element, point where pivot is correctly placed
return j
#write your code here


def quickSortIterative(arr, l, h):
#write your code here

#write your code he