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 #1592

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
27 changes: 20 additions & 7 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@

# It returns location of x in given array arr
# if present, else returns -1

# Time Complexity = O(logn)
# Space Complexity = O(1)
# Any problem you faced while coding this : No
#
def binarySearch(arr, l, r, x):

#write your code here



while l <= r:
# Calculating middle position
mid = l + (r -l) //2
# If value we are looking for is at mid we will return it
if arr[mid] == x:
return mid
# if mid is greater than x shift right pointer to mid -1
elif arr[mid] > x:
r = mid - 1
else:
l = mid + 1
# returning -1 if value is not present in sorted array
return -1
# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
Expand All @@ -17,6 +30,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")
38 changes: 26 additions & 12 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
# Python program for implementation of Quicksort Sort

# give you explanation for the approach
# Time Complexity: O(n log n)
# Space Complexity: O(log n) due to the recursion stack
# Did this code successfully run on Leetcode : yes
# Any problem you faced while coding this :choosing the the right pivot and handling edge case for duplicate element in the array

def partition(arr,low,high):


#write your code here

pivot = arr[high]

i = low - 1

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

# Swap pivot element with i+1
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return i + 1 # Return the partitioning index

def quickSort(arr, low, high):
if low < high:
# Partition the array and get the partition index
pi = partition(arr, low, high)
# Recursively sort elements before and after the partition
quickSort(arr, low, pi - 1) # Left subarray
quickSort(arr, pi + 1, high) # Right subarray

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

#write your code here

# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
n = len(arr)
Expand Down
48 changes: 29 additions & 19 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
# Node class
# Node class
# Time Complexity: O(n)
# Space Complexity: O(1)
# Approach: Used slow and fast pointers to find the middle node
class Node:

# Function to initialise the node object
# Function to initialize the node object
def __init__(self, data):

self.data = data # Assign data to the node
self.next = None # Initialize next as None
class LinkedList:

def __init__(self):


def __init__(self):
self.head = None # Initialize head of the linked list as None

def push(self, new_data):


# Function to get the middle of
# the linked list
# Creating a new node and make it the head
new_Node = Node(new_data)
new_Node.next = self.head
self.head = new_Node

def printMiddle(self):
# Use slow and fast pointers to find the middle as fast is moving by 2 eventually when it reaches to end out slow will be at middle
slow = self.head
fast = self.head
while fast and fast.next:
slow = slow.next # slow by one step
fast = fast.next.next # fast by two steps
print(f"middle = {slow.data}") # Slow is at middle node

# Driver code
# Driver code
list1 = LinkedList()
list1.push(5)
list1.push(4)
list1.push(2)
list1.push(3)
list1.push(1)
list1.printMiddle()
list1.push(5) # Add 5 to the list
list1.push(4) # Add 4 to the list
list1.push(2) # Add 2 to the list
list1.push(3) # Add 3 to the list
list1.push(1) # Add 1 to the list
list1.printMiddle() # Print the middle element
43 changes: 38 additions & 5 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
# Python program for implementation of MergeSort
# Time Complexity - O(nlogn)
# Space Complexity - O(n)
# Did this code successfully run on Leetcode :Yes
# Any problem you faced while coding this : No

def mergeSort(arr):
if len(arr) > 1:
#if we have array finding mid
m = len(arr) //2
l = arr[:m]
r = arr[m:]

mergeSort(l)
mergeSort(r)
i = j = k = 0
# Copy data to the temporary arrays l[] and r[]
while i < len(l) and j < len(r):
if l[i] < r[j]:
arr[k] = l[i]
i += 1
else:
arr[k] = r[j]
j += 1
k += 1
# Checking if any element was left in l[]
while i < len(l):
arr[k] = l[i]
i += 1
k += 1

#write your code here

# Checking if any element was left in r[]
while j < len(r):
arr[k] = r[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__':
arr = [12, 11, 13, 5, 6, 7]
Expand Down
64 changes: 60 additions & 4 deletions Exercise_5.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,66 @@
# Python program for implementation of Quicksort
# Time Complexity - O(n log n)
# Space Complexity - O(log n)

# This function is same in both iterative and recursive
# same as exercise 2
def partition(arr, l, h):
#write your code here

pivot = arr[h] # Choose the last element as the pivot
i = l - 1 # Index for the smaller element
for j in range(l, h):
if arr[j] < pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap
arr[i + 1], arr[h] = arr[h], arr[i + 1] # Place pivot in the correct position
return i + 1

def quickSortIterative(arr, l, h):
#write your code here
# Creating auxiliary stack
size = h - l + 1
stack = [0] * size

# Initialize top of stack
top = -1

# Push initial values of l and h to the stack
top += 1
stack[top] = l
top += 1
stack[top] = h

# popping from stack while it is not empty
while top >= 0:
# Pop h and l
h = stack[top]
top -= 1
l = stack[top]
top -= 1

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

# If there are elements on the left side of the pivot,
# push left side to stack
if p - 1 > l:
top += 1
stack[top] = l
top += 1
stack[top] = p - 1

# If there are elements on the right side of the pivot,
# pushing right side to stack
if p + 1 < h:
top += 1
stack[top] = p + 1
top += 1
stack[top] = h

# driver code
if __name__ == "__main__":
arr = [10, 7, 8, 9, 1, 5]
n = len(arr)
print("Given array is")
print(arr)
quickSortIterative(arr, 0, n - 1)
print("Sorted array is")
print(arr)