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
20 changes: 17 additions & 3 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#// Time Complexity : O(logn)
#// Space Complexity : O(1)
#// Did this code successfully run on Leetcode : Yes
#// Any problem you faced while coding this : No
# Python code to implement iterative Binary
# Search.

Expand All @@ -6,7 +10,17 @@
def binarySearch(arr, l, r, x):

#write your code here

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




# Test array
Expand All @@ -17,6 +31,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")
30 changes: 29 additions & 1 deletion Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
#//Time Complexity :O(nlogn)
#// Space Complexity : O(logn)
#// Did this code successfully run on Leetcode : Yes
#// Any problem you faced while coding this : Forgot the algorithm and while implementing had difficulty in converting logic to code.
# Python program for implementation of Quicksort Sort

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


#write your code here
#picking middle element for pivot
pivot = arr[(low + high) // 2]
i = low -1
j = high + 1
while True:
# moving i pointer to right
i += 1
while arr[i] < pivot:
i += 1
# moving j pointer to left
j -= 1
while arr[j] > pivot:
j -= 1

#pointer cross return j as pivot
if i >= j :
return j

arr[i], arr[j] = arr[j], arr[i]


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

#write your code here
if low < high:
p = partition(arr,low,high)

quickSort(arr,low,p)
quickSort(arr,p+1,high)

# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
Expand All @@ -19,5 +47,5 @@ def quickSort(arr,low,high):
print ("Sorted array is:")
for i in range(n):
print ("%d" %arr[i]),


20 changes: 20 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
#// Time Complexity : O(n)
#// Space Complexity : O(n) but each operation uses O(1) extra space
#// Did this code successfully run on Leetcode : Yes
#// Any problem you faced while coding this : No
# Node class
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):
slow = self.head
fast = self.head
while fast is not None and fast.next is not None:
slow = slow.next
fast = fast.next.next

if slow is not None:
print("Middle element is:", slow.data)
else:
print("List is empty")

# Driver code
list1 = LinkedList()
Expand Down
39 changes: 39 additions & 0 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
#//Time Complexity : O(nlogn)
#// Space Complexity : O(n)
#// Did this code successfully run on Leetcode : Yes
#// Any problem you faced while coding this : No
# Python program for implementation of MergeSort
def mergeSort(arr):

#write your code here
if len(arr) <= 1:
return arr

#diving into two parts
mid = len(arr) // 2
L = arr[:mid]
R = arr[mid:]

#Recurrsive call for split until will get 1 one element
mergeSort(L)
mergeSort(R)

#Merging
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 a in arr:
print(a)

# driver code to test the above code
if __name__ == '__main__':
Expand Down
57 changes: 55 additions & 2 deletions Exercise_5.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,63 @@
# Python program for implementation of Quicksort
#//Time Complexity :O(nlogn)
#// Space Complexity : O(logn)
#// Did this code successfully run on Leetcode : No
#// Any problem you faced while coding this :Had difficulty in converting logic to code.
# Python program for implementation of Quicksort Sort

# This function is same in both iterative and recursive
def partition(arr, l, h):
#write your code here
#write your code here
i = l - 1
pivot = arr[h]
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
# Create an auxiliary stack
size = h -l + 1
stack = [0] * size
top = -1
stack[top] = l
top += 1
stack[top] = h

#keep popping until it is empty
while top >= 0:
h = stack[top]
top -= 1
l = stack[top]
top -= 1

#setting pivot element position
p = partition(arr, l, h)

#elemnets on left side of pivot push left side of stack
if p -1 > l:
top += 1
stack[top] = l
top += 1
stack[top] = p - 1

#elemnts on right side of pivot push ot right sode of the stack
if p + 1 < h:
top += 1
stack[top] = p + 1
top +=1
stack [top] = h


if __name__ == "__main__":
# Sample array to test
data = [10, 7, 8, 9, 1, 5]
n = len(data)
print("Original array:", data)
# l is 0 (start index) and h is n-1 (last index)
quickSortIterative(data, 0, n - 1)
print("Sorted array: ", data)