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
51 changes: 30 additions & 21 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
# Python code to implement iterative Binary
# Search.

# It returns location of x in given array arr
# if present, else returns -1
def binarySearch(arr, l, r, x):

#write your code here



# Test array
arr = [ 2, 3, 4, 10, 40 ]
# Python code to implement iterative Binary
# Search.


# 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
elif arr[m] > x:
r = m - 1
else:
l = m + 1
return -1


# Test array
arr = [2, 3, 4, 10, 40]
x = 10
# Function call
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print "Element is present at index % d" % result
else:
print "Element is not present in array"

# Function call
result = binarySearch(arr, 0, len(arr) - 1, x)

if result != -1:
print("Element is present at index:", result)
else:
print("Element is not present in array")
54 changes: 33 additions & 21 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
# Python program for implementation of Quicksort Sort

# 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):
# write your code here
pivot = arr[high]

start = low
for end in range(low, high):
if arr[end] <= pivot:
arr[start], arr[end] = arr[end], arr[start]
start+=1
arr[start], arr[high] = arr[high], arr[start]
return start


# Function to do Quick sort
def quickSort(arr, low, high):
# write your code here
if low < high:
index = partition(arr, low, high)
# Sort left half recursively
quickSort(arr, low, index - 1)
# Sort right half recursively
quickSort(arr, index + 1, 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]),


# 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]),
64 changes: 38 additions & 26 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
# Node class
class Node:

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

class LinkedList:

def __init__(self):


def push(self, new_data):


# Function to get the middle of
# the linked list
def printMiddle(self):

# Driver code
list1 = LinkedList()
list1.push(5)
list1.push(4)
list1.push(2)
list1.push(3)
list1.push(1)
list1.printMiddle()
# Node class
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)
node.next = self.head
self.head = node

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


# Driver code
list1 = LinkedList()
list1.push(5)
list1.push(4)
list1.push(2)
list1.push(3)
list1.push(1)
list1.printMiddle()
29 changes: 27 additions & 2 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
# Python program for implementation of MergeSort
def mergeSort(arr):
if len(arr)<=1:
return arr
mid=len(arr)//2
left = mergeSort(arr[:mid])
right = mergeSort(arr[mid:])

return merge(left,right)

#write your code here
def merge(left,right):
result=[]
i=0
j=0

while i<len(left) and j<len(right):
if left[i]<right[j]:
result.append(left[i])
i+=1
else:
result.append(right[j])
j+=1
result.extend(left[i:])
result.extend(right[j:])
return result


# Code to print the list
def printList(arr):
def printList(arr):
for i in arr:
print(i,"\n")

#write your code here

Expand All @@ -13,6 +38,6 @@ def printList(arr):
arr = [12, 11, 13, 5, 6, 7]
print ("Given array is", end="\n")
printList(arr)
mergeSort(arr)
arr=mergeSort(arr)
print("Sorted array is: ", end="\n")
printList(arr)
27 changes: 27 additions & 0 deletions Exercise_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,35 @@
# This function is same in both iterative and recursive
def partition(arr, l, h):
#write your code here
pivot=arr[h]
start=l
for end in range(l,h):
if arr[end]<=pivot:
arr[start],arr[end]=arr[end],arr[start]
start+=1
arr[start],arr[h]=arr[h],arr[start]
return start


def quickSortIterative(arr, l, h):
#write your code here
callstack=[]
callstack.append((l,h))
while callstack:
l,h=callstack.pop()
if l<h:
pivot = partition(arr,l,h)
if pivot-1>l:
callstack.append((l,pivot-1))
if pivot+1<h:
callstack.append((pivot+1,h))

if __name__ == "__main__":
arr = [10, 7, 8, 9, 1, 5]
print("Before:", arr)

quickSortIterative(arr, 0, len(arr) - 1)

print("After: ", arr)