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
26 changes: 20 additions & 6 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
# Python code to implement iterative Binary
# Search.
# Python code to implement iterative Binary Search.

# Time Complexity :O(logn)
# Space Complexity :O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : None
# Your code here along with comments explaining your approach: Here we use a while loop to continue the process of comparing the key and splitting the search space in two halves. Based if the key is greater than the middle element, we search the right half of the array, otherwise we search the left half. This process continues until we find the key or until the search space is empty.

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

#write your code here

low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == x:
return mid
elif arr[mid] < x:
low = mid + 1
else:
high = 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")
24 changes: 20 additions & 4 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
# Python program for implementation of Quicksort Sort

# give you explanation for the approach
# Time Complexity :O(N^2)
# Space Complexity :O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : None
# Your code here along with comments explaining your approach: Select the rightmost element as the pivot. Rearrange the array so that elements smaller than the pivot are on the left, and elements greater than the pivot are on the right. Return the pivot’s index. Recursively apply the same process to the left and right sub-arrays created by the pivot.
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 = 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):
def quickSort(arr,low,high):
if low < high:
pi = partition(arr, low, high)
quickSort(arr, low, pi -1)
quickSort(arr, pi + 1, high)


#write your code here

Expand Down
35 changes: 25 additions & 10 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
# Time Complexity :O(N)
# Space Complexity :O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : None
# Your code here along with comments explaining your approach: Traverse linked list using two pointers. Move one pointer by one and another pointer by two. When the fast pointer reaches the end slow pointer will reach middle of the linked list.

# 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 push(self, new_data):


# Function to get the middle of
# the linked list
def printMiddle(self):
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
if self.head is not None:
while (fast is not None and fast.next is not None):
fast = fast.next.next
slow = slow.next
print("The middle element is: ", slow.data)
# Driver code
list1 = LinkedList()
list1.push(5)
Expand Down
45 changes: 39 additions & 6 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
# Python program for implementation of MergeSort
# 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 : None
# Your code here along with comments explaining your approach: The code implements Merge Sort, a divide-and-conquer algorithm that recursively splits the array into two halves until each subarray has one element. The mergeSort function then merges these sorted halves by comparing elements one by one and placing them in the correct order. The printList function is just for displaying the array, and the driver code shows the array before and after sorting.


def mergeSort(arr):

#write your code here

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 += 1
else:
arr[k] = right[j]
j +=1
k += 1

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

while j < len(right):
arr[k] = right[j]
j += 1
k += 1
# 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

#write your code here
# driver code to test the above code
if __name__ == '__main__':
arr = [12, 11, 13, 5, 6, 7]
Expand Down
49 changes: 49 additions & 0 deletions Exercise_5.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,59 @@
# Python program for implementation of Quicksort

# 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 : None
# Your code here along with comments explaining your approach: The code implements iterative QuickSort using an explicit stack instead of recursion. The partition function rearranges elements around a pivot (last element), placing smaller elements to the left and larger ones to the right, and returns the pivot’s final index. The quickSortIterative function pushes subarray bounds onto the stack, processes them in a loop, and sorts the array by repeatedly partitioning until the entire array is sorted.


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

for j in range(l, h):
if arr[j] <= x:
i = 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
stack = [0] * (h - l + 1)
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 p -1 > l:
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 ("Sorted array is:")
for i in range(n):
print ("%d" %arr[i])