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


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

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

# Function call
result = binarySearch(arr, 0, len(arr)-1, 10)

if result != -1:
print("Element is present at index % d" % result)
else:
print("Element is not present in array")

# Function call
result = binarySearch(arr, 0, len(arr)-1, x)
result = binarySearch(arr, 0, len(arr)-1, 20)

if result != -1:
print "Element is present at index % d" % result
if result != -1:
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: 19 additions & 11 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
# Python program for implementation of Quicksort Sort

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

# Keep on checking if all the elemts smaller than pivot are on the left of i + 1
# If all elements till i are lesser than pivot, place the pivot at i+1th position and return the index of the pivot.
# Hence we partition by placing the pivot at its place when array is sorted, so all elements to the left of pivot are smaller and all elements to the right of pivot are greater.
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):

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

# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
n = len(arr)
n = len(arr)
quickSort(arr,0,n-1)
print ("Sorted array is:")
for i in range(n):
print ("%d" %arr[i]),


print("%d" %arr[i])
43 changes: 36 additions & 7 deletions Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@
class Node:

# Function to initialise the node object
def __init__(self, data):
def __init__(self, data):
self.val = 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)
if not self.head:
self.head = new_node
return

curr = self.head
while curr.next:
curr = curr.next

curr.next = new_node

# Function to get the middle of
# the linked list
def printMiddle(self):
# Function to get the middle of the linked list
def printMiddle(self):
if not self.head:
print("Linked List is Empty")
return
slow = self.head
fast = slow
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
print(slow.val)


# Driver code
list1 = LinkedList()
Expand All @@ -24,3 +44,12 @@ def printMiddle(self):
list1.push(3)
list1.push(1)
list1.printMiddle()

list1 = LinkedList()
list1.push(5)
list1.printMiddle()

list1 = LinkedList()
list1.push(1)
list1.push(2)
list1.printMiddle()
48 changes: 44 additions & 4 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,52 @@
def merge(array, left, mid, right):
# Array is sorted from left to mid
leftSortedArray = arr[left:mid+1]
# Array is sorted from mid+1 to right
rightSortedArray = arr[mid+1:right+1]
# Start of writerindex
writerIdx = left
leftIdx = 0
rightIdx = 0

while leftIdx < len(leftSortedArray) and rightIdx < len(rightSortedArray):
if leftSortedArray[leftIdx] < rightSortedArray[rightIdx]:
arr[writerIdx] = leftSortedArray[leftIdx]
leftIdx+=1
else:
arr[writerIdx] = rightSortedArray[rightIdx]
rightIdx+=1
writerIdx+=1

while leftIdx < len(leftSortedArray):
arr[writerIdx] = leftSortedArray[leftIdx]
leftIdx+=1
writerIdx+=1

while rightIdx < len(rightSortedArray):
arr[writerIdx] = rightSortedArray[rightIdx]
rightIdx+=1
writerIdx+=1

def mergeSortRecursive(arr, left, right):
# We have to sort the array from left to right
if left >= right:
return
# we take the mid
mid = (left + right) // 2
# Sort the left hafl
mergeSortRecursive(arr, left, mid)
# Sort the right half
mergeSortRecursive(arr, mid+1, right)
# Merge the 2 sorted halves
merge(arr, left, mid, right)

# Python program for implementation of MergeSort
def mergeSort(arr):

#write your code here
mergeSortRecursive(arr, 0, len(arr)-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
44 changes: 41 additions & 3 deletions Exercise_5.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
# 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):
pivot = arr[high]
i = low - 1 # Index of the smaller element
for j in range(low, high):
# If current element is smaller than or equal to pivot
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


def quickSortIterative(arr, l, h):
#write your code here
# Sorts an array using iterative Quick Sort by managing a stack of sub-array boundaries to be processed.
if not arr:
return
# Create a stack to store low and high indices
# We use a list and append/pop from the end for stack behavior
stack = []
# Push initial low and high indices
stack.append(l)
stack.append(h)

# Keep popping from stack while it is not empty
while stack:
high = stack.pop()
low = stack.pop()
# Get pivot element's position after partitioning
p = partition(arr, low, high)
# If there are elements on the left side of pivot, push them to stack
if p - 1 > low:
stack.append(low)
stack.append(p - 1)
# If there are elements on the right side of pivot, push them to stack
if p + 1 < high:
stack.append(p + 1)
stack.append(high)

return arr

# driver code to test the above code
arr = [10, 80, 30, 90, 40, 50, 70]
print(f"Original array: {arr}")
sortedArr = quickSortIterative(arr, 0, len(arr)-1)
print(f"Sorted array: {sortedArr}")