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

Precourse 2 Completed #1618

Open
wants to merge 1 commit 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
Binary file added .DS_Store
Binary file not shown.
30 changes: 17 additions & 13 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@

# It returns location of x in given array arr
# if present, else returns -1
def binarySearch(arr, l, r, x):
def binarySearch(arr, left, right, x):
while left <= right:
mid = (left + right) // 2
if arr[mid] == x:
return mid
elif arr[mid] > x:
right = mid - 1
else:
left = mid + 1
return -1


arr = [2, 3, 4, 10, 40]
x = 50

#write your code here



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

# Function call
result = binarySearch(arr, 0, len(arr)-1, 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")
17 changes: 17 additions & 0 deletions Exercise_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,29 @@ def partition(arr,low,high):


#write your code here
pivot = arr[high] # last element as the pivot
i = low - 1 # smaller element

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

# swapping the pivot element with the element at i+1
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 = 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
18 changes: 17 additions & 1 deletion 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):
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

if slow_ptr:
print(f"The middle element is {slow_ptr.data}")
else:
print("The linked list is empty")

# Driver code
list1 = LinkedList()
Expand Down
40 changes: 40 additions & 0 deletions Exercise_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,51 @@
def mergeSort(arr):

#write your code here
if len(arr) > 1:
# the middle of the array
mid = len(arr) // 2

# dividing the array into left and right halves
left = arr[:mid]
right = arr[mid:]

# recursively sorting the two halves
mergeSort(left)
mergeSort(right)

# merging the sorted halves
i = j = k = 0

# copying data to temporary arrays left[] and right[]
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

# checking for any remaining elements in left[]
while i < len(left):
arr[k] = left[i]
i += 1
k += 1

# checking for any remaining elements in right[]
while j < len(right):
arr[k] = right[j]
j += 1
k += 1


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

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

# driver code to test the above code
if __name__ == '__main__':
Expand Down
26 changes: 26 additions & 0 deletions Exercise_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,34 @@
# This function is same in both iterative and recursive
def partition(arr, l, h):
#write your code here
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
stack = []

stack.append((l, h))

while stack:
l, h = stack.pop()

pivot_index = partition(arr, l, h)

if pivot_index - 1 > l:
stack.append((l, pivot_index - 1))

if pivot_index + 1 < h:
stack.append((pivot_index + 1, h))