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

# Your code here along with comments explaining your approach
# Approach:
# 1. Initialize two pointers, l (left) and r (right), to mark the search boundaries.
# 2. While l <= r:
# - Compute the middle index m = (l + r) // 2.
# - If nums[m] is greater than the target, move r to m - 1 (search left half).
# - If nums[m] is less than the target, move l to m + 1 (search right half).
# - If nums[m] equals the target, return m (element found).
# 3. If the loop ends without returning, the element is not present → return -1.

# Python code to implement iterative Binary
# Search.

# It returns location of x in given array arr
# if present, else returns -1
def binarySearch(arr, l, r, x):

#write your code here
l, r = 0, len(arr)-1
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



Expand All @@ -17,6 +42,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")
43 changes: 36 additions & 7 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
# Python program for implementation of Quicksort Sort
# Time Complexity : Average O(n log n), Worst O(n^2) if pivot choices are poor
# Space Complexity : O(log n) due to recursion stack (in-place sort)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Your code here along with comments explaining your approach
# Approach:
# 1. Choose a pivot (here we choose the last element in the current subarray).
# 2. Partition the array into two halves:
# - All elements <= pivot move to the left side.
# - All elements > pivot move to the right side.
# This is done using a pointer `i` to track the position where the next smaller element should go.
# 3. Swap the pivot into its correct sorted position.
# 4. Recursively apply the same logic to the left and right subarrays (excluding the pivot).
# 5. Sorting happens in-place, requiring no extra array.

# 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] # choosing 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] # swap

arr[i + 1], arr[high] = arr[high], arr[i + 1] # place pivot in correct position
return i + 1



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

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

# Recursively sort elements before and after partition
quickSort(arr, low, pi - 1)
quickSort(arr, pi + 1, high)


# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
Expand Down
33 changes: 28 additions & 5 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
# Node class
# Time Complexity : O(n)
# Space Complexity : O(1)
# 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):
def __init__(self, data):
self.data = data
self.next = None

class LinkedList:

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


def push(self, new_data):
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):
def printMiddle(self):
slow_ptr = self.head
fast_ptr = self.head

while fast_ptr is not None and fast_ptr.next is not None:
fast_ptr = fast_ptr.next.next
slow_ptr = slow_ptr.next

# slow_ptr will now be at the middle
if slow_ptr:
print("The middle element is:", slow_ptr.data)
else:
print("The list is empty")


# Driver code
list1 = LinkedList()
Expand Down
54 changes: 50 additions & 4 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,56 @@
# Python program for implementation of MergeSort
# Time Complexity : O(n log n)
# 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:

# Finding the mid of the array
mid = len(arr) // 2

# Dividing the array elements into 2 halves
L = arr[:mid]
R = arr[mid:]

# Recursive call to sort both halves
mergeSort(L)
mergeSort(R)

i = j = k = 0

# Copy data to temp arrays L[] and R[]
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

# Checking if any element was left
while i < len(L):
arr[k] = L[i]
i += 1
k += 1

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


#write your code here

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


#write your code here

Expand Down
37 changes: 36 additions & 1 deletion Exercise_5.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
# Time Complexity : O(n log n)
# Space Complexity : O(log n)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Python program for implementation of Quicksort

# This function is same in both iterative and recursive
def partition(arr, l, h):
#write your code here
pivot = arr[h] # take last element as pivot
i = l - 1 # index of smaller element

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

arr[i + 1], arr[h] = arr[h], arr[i + 1] # place pivot in correct position
return i + 1


#write your code here


def quickSortIterative(arr, l, h):
stack = []

# Push initial values of l and h
stack.append((l, h))

# Keep popping until stack is empty
while stack:
l, h = stack.pop()
if l < h:
# Partition the array
p = partition(arr, l, h)

# If elements exist on left side of pivot, push left side to stack
if p - 1 > l:
stack.append((l, p - 1))

# If elements exist on right side of pivot, push right side to stack
if p + 1 < h:
stack.append((p + 1, h))
#write your code here