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

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
15 changes: 12 additions & 3 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
# It returns location of x in given array arr
# if present, else returns -1
def binarySearch(arr, l, r, x):

while l<r:
mid = (l+r)//2
if arr[mid]==x:
return mid
else:
if arr[mid]>x:
r = mid-1
elif arr[mid]<x:
l = mid+1
return -1
#write your code here


Expand All @@ -17,6 +26,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",result)
else:
print "Element is not present in array"
print("Element is not present in array")
21 changes: 17 additions & 4 deletions Exercise_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@

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



i=low-1
pivot = arr[high]
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
#write your code here


# Function to do Quick sort
def quickSort(arr,low,high):
if low<high:
pivot = partition(arr,low,high)

quickSort(arr, low, pivot-1)
quickSort(arr, pivot+1, high)

#write your code here

Expand All @@ -17,7 +31,6 @@ def quickSort(arr,low,high):
n = len(arr)
quickSort(arr,0,n-1)
print ("Sorted array is:")
for i in range(n):
print ("%d" %arr[i]),
print(arr)


18 changes: 15 additions & 3 deletions Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@ 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):
ptr1 = self.head
ptr2 = self.head

while ptr2 and ptr2.next:
ptr1 = ptr1.next
ptr2 = ptr2.next.next

print(ptr1.data)
return ptr1

# Driver code
list1 = LinkedList()
Expand Down
33 changes: 30 additions & 3 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
# Python program for implementation of MergeSort
def mergeSort(arr):
if len(arr)<=1:
return arr

left =0
right = len(arr)
mid = (left+right)//2

left_array=mergeSort(arr[:mid])
right_array = mergeSort(arr[mid:])

i,j=0,0
result =[]
while i < len(left_array) and j < len(right_array):
if left_array[i] < right_array[j]:
result.append(left_array[i])
i += 1
else:
result.append(right_array[j])
j += 1

result.extend(left_array[i:])
result.extend(right_array[j:])

return result



#write your code here

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

print(arr)
return
#write your code here

# 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)
result=mergeSort(arr)
print("Sorted array is: ", end="\n")
printList(arr)
printList(result)
26 changes: 22 additions & 4 deletions Exercise_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@

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

if l < h:
pivot_index = partition(arr, l, h)

stack.append((l, pivot_index - 1))

stack.append((pivot_index + 1, h))