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

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



Expand All @@ -17,6 +24,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")
28 changes: 21 additions & 7 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
# 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[high]
i = low - 1

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

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

#write your code here
# base condition
if low >= high:
return

# partition the array
pi = partition(arr, low, high)

# recursively sort left and right parts
quickSort(arr, low, pi - 1)
quickSort(arr, pi + 1, high)

# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
Expand Down
23 changes: 17 additions & 6 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
# Node class
class Node:

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

class LinkedList:

def __init__(self):

self.head = Node(-1)

def push(self, new_data):
def push(self, new_data):
temp = self.head
while temp.next:
temp = temp.next
newNode = Node(new_data)
temp.next = newNode


# Function to get the middle of
# the linked list
def printMiddle(self):
def printMiddle(self):
slow,fast = self.head.next,self.head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
print(slow.data)

# Driver code
list1 = LinkedList()
Expand Down
32 changes: 27 additions & 5 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
# Python program for implementation of MergeSort
def mergeSort(arr):

#write your code here
if len(arr) > 1:
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]
mergeSort(left)
mergeSort(right)
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
k += 1
while i < len(left):
arr[k] = left[i]
i += 1
k += 1
while j < len(right):
arr[k] = right[j]
j += 1
k += 1

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

#write your code here
def printList(arr):
for item in arr:
print(item)


# driver code to test the above code
if __name__ == '__main__':
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 += 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
# Create an explicit stack
stack = []

# push initial range
stack.append((l, h))

# Process stack until empty
while stack:
l, h = stack.pop()

# Partition the array
pi = partition(arr, l, h)

# If elements on left side of pivot, push left side
if pi - 1 > l:
stack.append((l, pi - 1))

# If elements on right side of pivot, push right side
if pi + 1 < h:
stack.append((pi + 1, h))