Skip to content
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
21 changes: 17 additions & 4 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,33 @@

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

# Time complexity - > O(log(n))
# Space Complexity -> O1
def binarySearch(arr, l, r, x):

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



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

# Function call
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")
33 changes: 30 additions & 3 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
# Python program for implementation of Quicksort Sort

# give you explanation for the approach
# Quick sort is a devide and conqure approach. basically we choose a starting point and then try to place it in
# it's correct position, i.e all the numbers on left are small and right are big.
# Once we that number is in correct place, we devide the array into 2 half from the point (not including that poin)
# and recursively start finding the correct position of 1 elment in the sub array and so on untill all elments are
# in it's correct position
def partition(arr,low,high):

# This helper is used to arrange 1 number in it's corrrect positino and return the new index of that point
# the new index will become point for breaking the array into sub arrays


#write your code here
j = low
i = low -1
pivot = arr[high] #the number we are trying to correct place, let's pick last element

while j<=high:
if arr[j]<=pivot:
i+=1
tmp = arr[j]
arr[j] = arr[i]
arr[i] = tmp
j+=1
return i



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

if low>=high:
return

pivot = partition(arr,0,high)

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

#write your code here

# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
arr = [6,5,4,3,2,1]
n = len(arr)
quickSort(arr,0,n-1)
print ("Sorted array is:")
Expand Down
6 changes: 4 additions & 2 deletions Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ class Node:

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

class LinkedList:

def __init__(self):

self.arr=[]

def push(self, new_data):

self.arr.append(Node(new_data))

# Function to get the middle of
# the linked list
def printMiddle(self):
print(self.arr[(len(self.arr)-1)//2].data)

# Driver code
list1 = LinkedList()
Expand Down
58 changes: 55 additions & 3 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,63 @@
# Python program for implementation of MergeSort
def mergeSort(arr):


# Merge sort is also a devide and conqure approach. But in this the arrays are sorted during merging
# Basically we keep on deviding the arrays in half untill thhe last 2 elements are left 1 in left array
# and 2nd in right array. Then from that we start rebuilding the array by comparing the values in
# both subarray and assigning to the actual array
def mergeSort(arr, left=0, right=None):
if right == None:
right = len(arr) - 1

if left < right:
mid = (left+right)//2
mergeSort(arr, left, mid)
mergeSort(arr, mid+1, right)

merge(arr, left, mid, right)


def merge(arr, left, mid, right):
arr1 = []
arr2 = []

for i in range(left, mid+1):
arr1.append(arr[i])

for j in range(mid+1, right+1):
arr2.append(arr[j])

i = 0
j = 0
k=left
while i < len(arr1) and j < len(arr2):
if arr1[i] < arr2[j]:
arr[k]=arr1[i]
i+=1
else:
arr[k] = arr2[j]
j+=1
k+=1

if i != len(arr1):
while i < len(arr1):
arr[k] = arr1[i]
i+=1
k+=1

if j != len(arr2):
while j < len(arr2):
arr[k] = arr2[j]
j+=1
k+=1
return



#write your code here

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

print(arr)
#write your code here

# driver code to test the above code
Expand Down
36 changes: 36 additions & 0 deletions Exercise_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,44 @@
# This function is same in both iterative and recursive
def partition(arr, l, h):
#write your code here
pivot = arr[h]
i=l-1
j=l

while j<=h:
if arr[j]<=pivot:
i+=1
tmp = arr[i]
arr[i] = arr[j]
arr[j] = tmp
j+=1
return i


def quickSortIterative(arr, l, h):
#write your code here
stack = [0] * len(arr)
stack.append(l)
stack.append(h)

while len(stack) > 0:
high = stack.pop()
low = stack.pop()
pivotIndex = partition(arr, low, high)

if pivotIndex - low >0:
stack.append(low)
stack.append(pivotIndex-1)

if high - pivotIndex > 0:
stack.append(pivotIndex+1)
stack.append(high)

arr = [3, 2, 1, 4]
n = len(arr)
quickSortIterative(arr,0,n-1)
print ("Sorted array is:")
for i in range(n):
print ("%d" %arr[i]),