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

done with precourse-2 #1641

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
19 changes: 17 additions & 2 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
def binarySearch(arr, l, r, x):

#write your code here
if len(arr)== 0:
return -1
middle = (l+r)//2
if x == arr[middle]:
return middle
else:
if x < arr[middle]:
return binarySearch(arr,l,(middle-1),x)
else:
return binarySearch(arr,(middle+1),r,x)







Expand All @@ -17,6 +31,7 @@ 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")

52 changes: 30 additions & 22 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
# Python program for implementation of Quicksort Sort

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


#write your code here

def partition(arr, low, high):
pivot = arr[low]
i = low + 1
j = high

# 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)
quickSort(arr,0,n-1)
print ("Sorted array is:")
for i in range(n):
print ("%d" %arr[i]),


while True:
while i <= j and arr[i] <= pivot:
i += 1
while i <= j and arr[j] > pivot:
j -= 1
if i > j:
break
arr[i], arr[j] = arr[j], arr[i]
arr[low], arr[j] = arr[j], arr[low]
return j


def quickSort(arr, low, high):
if low < high:
mid = partition(arr, low, high)
quickSort(arr, low, mid - 1)
quickSort(arr, mid + 1, high)


if __name__ == "__main__":
arr = [10, 7, 8, 9, 1, 5]
n = len(arr)
print("Original array:", arr)
quickSort(arr, 0, n - 1)
print("Sorted array is:")
print(arr)
24 changes: 22 additions & 2 deletions Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,38 @@ 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):


node = Node(new_data)
if self.head == None:
self.head = node
else:
curr = self.head
while curr.next:
curr = curr.next
curr.next = 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(slow_ptr.data)
else:
print("the linked list is empty")


# Driver code
list1 = LinkedList()
Expand Down
66 changes: 49 additions & 17 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
# Python program for implementation of MergeSort
def mergeSort(arr):

#write your code here

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

#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)
print("Sorted array is: ", end="\n")
printList(arr)
if len(arr) > 1:
# Find the middle of the array
mid = len(arr) // 2

# Divide the array into two halves
L = arr[:mid]
R = arr[mid:]

# Recursively sort the two halves
mergeSort(L)
mergeSort(R)

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

# Copy data to temp 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

# Check if any element was left
while i < len(L):
arr[k] = L[i]
i += 1
k += 1

while j < len(R):
arr[k] = R[j]
j += 1
k += 1


def printList(arr):
for i in arr:
print(i, end=" ")
print()

if __name__ == '__main__':
arr = [12, 11, 13, 5, 6, 7]
print("Given array is:")
printList(arr)
mergeSort(arr)
print("Sorted array is:")
printList(arr)