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 Submit #1625

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
24 changes: 19 additions & 5 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,24 @@
# if present, else returns -1
def binarySearch(arr, l, r, x):

#write your code here

while l <= r:
mid = l + (r - l) // 2 # Find middle element


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
Expand All @@ -17,6 +31,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")
18 changes: 12 additions & 6 deletions Exercise_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

# give you explanation for the approach
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 = 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
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
19 changes: 16 additions & 3 deletions Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,32 @@
class Node:

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


while fast and fast.next:
fast = fast.next.next
slow = slow.next

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

# Driver code
list1 = LinkedList()
Expand Down
35 changes: 34 additions & 1 deletion Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
# Python program for implementation of MergeSort
def mergeSort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]

mergeSort(left_half)
mergeSort(right_half)

i = j = k = 0


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


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

#write your code here


# Code to print the list
def printList(arr):
for i in range(len(arr)):
print(arr[i], end=" ")
print()

#write your code here

Expand Down
32 changes: 30 additions & 2 deletions Exercise_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,37 @@

# 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 = 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()


pi = partition(arr, l, h)


if pi - 1 > l:
stack.append((l, pi - 1))
if pi + 1 < h:
stack.append((pi + 1, h))
arr = [2, 1, 3, 5, 6, 7]
n = len(arr)
print("Given array is:", arr)
quickSortIterative(arr, 0, n - 1)
print("Sorted array is:", arr)