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
19 changes: 16 additions & 3 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@

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

# Time and Space Complexity
# Time Complexity is O(log N) where N is the nuber of elements that are present in the given arr
# Space Complexity is O(1) as the variables are constant irrespective of the array size
def binarySearch(arr, l, r, x):

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


return -1


# Test array
arr = [ 2, 3, 4, 10, 40 ]
Expand All @@ -17,6 +30,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")
27 changes: 21 additions & 6 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
# Time and Space Complexity
# Space Complexity: O(lon N) where N is the number of elements present
# Time Complexity: Average case is O(Nlog N)
# partition function has time complexity of O(N) and space complexity of O(1)
# quickSort function has space complexity of O(N log N) and space complexity of O(log N)

# Python program for implementation of Quicksort Sort

# give you explanation for the approach

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:
value=partition(arr,low,high)
quickSort(arr,low,value-1)
quickSort(arr,value+1,high)


# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
Expand Down
27 changes: 22 additions & 5 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
# Time and Space Complexity
# Space Complexity: O (N) where N denoted the number of elementes/nodes in the given LinkedList
# Time Complexity:
# push function has the time complexity of O(1) amd space complexity of O(1)
# printMiddle function has the time complexity of O(N) and space complexity of O(1)

# Node class
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

def push(self, new_data):

def push(self, new_data):
newNode=Node(new_data)
newNode.next=self.head
self.head=newNode

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


# Driver code
list1 = LinkedList()
Expand Down
49 changes: 33 additions & 16 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
# Python program for implementation of MergeSort
#Kindly include Time and Space complexity at top of each file

#Time and Space Complexity
# Space Complexity : O(N)
# Time Complexity : O(N log N) where N is the number of elements in the array


def mergeSort(arr):

#write your code here

# Code to print the list
def printList(arr):
if len(arr)>1:
mid=len(arr)//2
left=arr[:mid]
right=arr[mid:]

mergeSort(left)
mergeSort(right)

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

#write your code here

# driver code to test the above code
if __name__ == '__main__':
arr = [12, 11, 13, 5, 6, 7]
print ("Given array is", end="\n")
printList(arr)
mergeSort(arr)
print("Sorted array is: ", end="\n")
printList(arr)
while i<len(left):
arr[k]=left[i]
i=i+1
k=k+1

while j<len(right):
arr[k]=right[j]
j=j+1
k=k+1
56 changes: 52 additions & 4 deletions Exercise_5.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,58 @@
# Python program for implementation of Quicksort
#Kindly include Time and Space complexity at top of each file

#Time and Space Complexity
# Space Complexity : O(N) due to the stack space that needs to be allocated
# Time Complexity : O(N log N) where N is the number of elements in the array


# 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
size=h-l+1
stack=[0]*size
top=-1

top=top+1
stack[top]=l
top=top+1
stack[top]=h

while top>=0:
h=stack[top]
top=top-1
l=stack[top]
top=top-1

p=partition(arr,l,h)

if l<p-1:
top = top + 1
stack[top] = l
top = top + 1
stack[top] = p - 1

if p+1<h:
top=top+1
stack[top]=p+1
top=top+1
stack[top]=h



arr=[4,3,5,2,1,3,2,3]
n=len(arr)
quickSortIterative(arr,0,n-1)
print(arr)