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: 18 additions & 2 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,23 @@
def binarySearch(arr, l, r, x):

#write your code here
while l <= r:
mid = l + (r - l) // 2

# Check if x is present at mid
if arr[mid] == x:
return mid

# If x is greater, ignore left half
elif arr[mid] < x:
l = mid + 1

# If x is smaller, ignore right half
else:
r = mid - 1

# If we reach here, then the element was not present
return -1


# Test array
Expand All @@ -17,6 +33,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")
29 changes: 25 additions & 4 deletions Exercise_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,42 @@
# give you explanation for the approach
def partition(arr,low,high):


#write your code here
# Choose the rightmost element as pivot
pivot = arr[high]

# Index of smaller element (indicates right position of pivot)
i = low - 1

for j in range(low, high):
# If current element is smaller than or equal to pivot
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]

# Place pivot at its correct position
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:
# pi is partitioning index, arr[pi] is now at right place
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]
n = len(arr)
quickSort(arr,0,n-1)
print ("Sorted array is:")
print("Sorted array is:")
for i in range(n):
print ("%d" %arr[i]),
print("%d" % arr[i], end=" ")
print()


22 changes: 19 additions & 3 deletions Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,34 @@ 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):
def printMiddle(self):
if self.head is None:
print("List is empty")
return

slow_ptr = self.head
fast_ptr = self.head

while fast_ptr and fast_ptr.next:
slow_ptr = slow_ptr.next
fast_ptr = fast_ptr.next.next

print("Middle element is:", slow_ptr.data)

# 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
Expand Up @@ -2,11 +2,50 @@
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
left_half = arr[:mid]
right_half = arr[mid:]

# Sorting the first half
mergeSort(left_half)

# Sorting the second half
mergeSort(right_half)

i = j = k = 0

# Copy data to temp arrays left_half[] and right_half[]
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1

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

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

# Code to print the list
def printList(arr):

#write your code here
for i in range(len(arr)):
print(arr[i], end=" ")
print()

# driver code to test the above code
if __name__ == '__main__':
Expand Down
36 changes: 36 additions & 0 deletions Exercise_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,44 @@
# This function is same in both iterative and recursive
def partition(arr, l, h):
#write your code here
# Choose the rightmost element as pivot
pivot = arr[h]

# Index of smaller element (indicates right position of pivot)
i = l - 1

for j in range(l, h):
# If current element is smaller than or equal to pivot
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]

# Place pivot at its correct position
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
stack = []

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

# Keep popping from stack while it is not empty
while stack:
# Pop l and h
l, h = stack.pop()

# Set pivot element at its correct position
p = partition(arr, l, h)

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

# If there are elements on right side of pivot, push right side to stack
if p + 1 < h:
stack.append((p + 1, h))