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
30 changes: 27 additions & 3 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,44 @@

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

"""
Time Complexity: log(n) as the search space is divided into halves every time.

Space Complexity: constant - O(1) as we aren't using any extra memory, just pointers


"""
def binarySearch(arr, l, r, x):

#write your code here
ans = -1
while l <= r:
mid = (l+r) // 2

if arr[mid] == x:
ans = mid
break
elif arr[mid] < x:
l = mid + 1
else:
r = mid - 1

return ans





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

# arr = [ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 ]
# x = 7
# 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")
21 changes: 20 additions & 1 deletion Exercise_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,31 @@ def partition(arr,low,high):


#write your code here

pivot = arr[low]
i = low+1
j = high

while True:
while i<=high and arr[i] <= pivot:
i+=1
while j >= low and arr[j] > pivot:
j-=1
if i >= j:
break
arr[i], arr[j] = arr[j], arr[i]
arr[low], arr[j] = arr[j], arr[low]
return j


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

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


# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
Expand Down
40 changes: 37 additions & 3 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
# Node class
"""
Space Complexity:
O(N) for N nodes for the whole linkedlist

Time Complexity:
push: O(1)
PrintMiddle: O(n)

"""

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):

self.head = None
self.tail = None

def push(self, new_data):

new_node = Node(new_data)
if self.head == 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):
fast = slow = self.head

while fast and fast.next:
fast = fast.next.next
slow = slow.next

print("Middle element is: ", slow.data)


# Driver code
list1 = LinkedList()
Expand All @@ -24,3 +50,11 @@ def printMiddle(self):
list1.push(3)
list1.push(1)
list1.printMiddle()
# # Driver code
# list1 = LinkedList()
# list1.push(5)
# list1.push(4)
# list1.push(3)
# list1.push(2)
# list1.push(1)
# list1.printMiddle()
33 changes: 31 additions & 2 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
# Python program for implementation of MergeSort
def mergeSort(arr):

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

mergeSort(left_array)
mergeSort(right_array)

i = j = k = 0

while i < len(left_array) and j < len(right_array):
if left_array[i] < right_array[j]:
arr[k] = left_array[i]
i+=1
else:
arr[k] = right_array[j]
j+=1
k+=1

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

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



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

#write your code here
print(arr)

# driver code to test the above code
if __name__ == '__main__':
Expand Down
31 changes: 28 additions & 3 deletions Exercise_5.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
# Python program for implementation of Quicksort

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

while True:
while i<=high and arr[i] <= pivot:
i+=1
while j >= low and arr[j] > pivot:
j-=1
if i >= j:
break
arr[i], arr[j] = arr[j], arr[i]
arr[low], arr[j] = arr[j], arr[low]
return j


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

stack = [(l, h)]
while stack:
low, high = stack.pop()
if low >= high:
continue
p = partition(arr, low, high)
stack.append((low, p - 1))
stack.append((p + 1, high))

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