Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed Pre course 2 #1594

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
18 changes: 14 additions & 4 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@
def binarySearch(arr, l, r, x):

#write your code here

# The Binary Search function checks whether an element exists in a given array by utilizing the left
# and right pointers of the array and updating them based on specific criteria.
while l<=r:
mid = (l+r)//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
x = 4

# Function call
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 ", result)
else:
print "Element is not present in array"
print ("Element is not present in array")
14 changes: 14 additions & 0 deletions Exercise_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@ def partition(arr,low,high):


#write your code here
# Rearranges the array so that smaller and greater elements are to left side and right side of pivot element respectively.
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):

#write your code here
# Sorts the left and right sub arrays recursively based on the index.
if low<high:
pi = partition(arr,low,high)
quickSort(arr,low,pi-1)
quickSort(arr,pi+1,high)

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

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

class LinkedList:

def __init__(self):

# Initialise the head node
self.head = Node()

def push(self, new_data):
# Append a new node to the list if we have a head node or assign the new node as head node
if self.head is None:
newNode = Node(new_data)
self.head = newNode
return
newNode = Node(new_data)
temp = self.head
while temp.next is not None:
temp = temp.next
temp.next = newNode


# Function to get the middle of
# the linked list
def printMiddle(self):
# use 2 pointers slow and fast to fetch the middle node.
slow,fast = self.head,self.head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
print("Value of Middle Node: ",slow.data)

# Driver code
list1 = LinkedList()
Expand All @@ -23,4 +42,6 @@ def printMiddle(self):
list1.push(2)
list1.push(3)
list1.push(1)
list1.push(10)
list1.push(11)
list1.printMiddle()
33 changes: 31 additions & 2 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
# Python program for implementation of MergeSort
def merge(left, right):
result = []
i = j = 0

while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1

result.extend(left[i:])
result.extend(right[j:])

return result

def mergeSort(arr):

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

mid = len(arr) // 2
leftHalf = arr[:mid]
rightHalf = arr[mid:]

sortedLeft = mergeSort(leftHalf)
sortedRight = mergeSort(rightHalf)

return merge(sortedLeft, sortedRight)

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

#write your code here
print(arr)

# driver code to test the above code
if __name__ == '__main__':
arr = [12, 11, 13, 5, 6, 7]
print ("Given array is", end="\n")
printList(arr)
mergeSort(arr)
sortedarr = mergeSort(arr)
print("Sorted array is: ", end="\n")
printList(arr)
printList(sortedarr)
24 changes: 24 additions & 0 deletions Exercise_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,32 @@
# This function is same in both iterative and recursive
def partition(arr, l, h):
#write your code here
# Rearranges the array so that smaller and greater elements are to left side and right side of pivot element respectively.
pivot = arr[h]
i = l - 1
for j in range(l,h):
if arr[j] < pivot:
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
# This approach follows the idea of appending the start and end indexes of arrays and corresponding subarrays for the array partitions.
st = []
st.append((0,len(arr)-1))
while st:
l,h = st.pop()
if l <h :
pi = partition(arr,l,h)
st.append((l,pi-1))
st.append((pi+1,h))

# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
n = len(arr)
quickSortIterative(arr,0,n-1)
print ("Sorted array is:")
print(arr)