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

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

'''
arr = [2 3 5 [10] {40}]
left = 3
right = 4
middle = 3
target = 10
'''
def binarySearch(arr, left, right, target):
#write your code here


while left <= right:
middle = (left + right) // 2

if arr[middle] == target:
return middle
elif arr[middle] < target:
left = middle + 1
else:
right = middle - 1

return - 1

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

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

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")
31 changes: 25 additions & 6 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
# Python program for implementation of Quicksort Sort

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

'''
There are a lot of partitions that I could possibly use. The most efficient partition I understand easily is the Lomuto partition.

I recognize that Hoare's partition is the fastest of all, but I have stuck with what I understand for now

Basically as I understand it we keep track of the smaller element relative to the pivot and keep moving them left. Once we're done we swap the pivot into it's appropriate position,
and all elements less than it will be to the left and all elements to the right will be greater. We then keep doing this recursively until we've processed the entire array
'''
def partition(arr, low, high):
pivot = arr[high]
smallerIndex = low - 1

for index in range(low, high):
# Move all smaller elements to the left side
if arr[index] < pivot:
smallerIndex += 1
arr[smallerIndex], arr[index] = arr[index], arr[smallerIndex]

arr[smallerIndex + 1], arr[high] = arr[high], arr[smallerIndex + 1]

#write your code here
return smallerIndex + 1


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

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

# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
Expand Down
36 changes: 27 additions & 9 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
# Node class
class Node:

# Function to initialise the node object
def __init__(self, data):
self.data = data
self.next = None

class LinkedList:

class LinkedList:
def __init__(self):

self.head = None
self.tail = None
self.length = 0

def push(self, new_data):

new_node = Node(new_data)
# Edge cases, we add our first new_node
is_first_node = self.length == 0
self.length += 1

if is_first_node:
self.head = new_node
self.tail = new_node
return

# Otherwise it is very standard
self.tail.next = new_node
self.tail = 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):
middle = self.length // 2
current = self.head
for _ in range(middle):
current = current.next

print(f"middle node value: {current.data} - index: {middle}")

# Driver code
list1 = LinkedList()
Expand Down
27 changes: 23 additions & 4 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
# Python program for implementation of MergeSort
def mergeSort(arr):

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

mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]

mergeSort(left)
mergeSort(right)

leftIndex = rightIndex = combinedIndex = 0
while leftIndex < len(left) and rightIndex < len(right):
if left[leftIndex] <= right[rightIndex]:
arr[combinedIndex] = left[leftIndex]
leftIndex += 1
else:
arr[combinedIndex] = right[rightIndex]
rightIndex += 1

combinedIndex += 1

arr[combinedIndex:] = left[leftIndex:] + right[rightIndex:]

# 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