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
49 changes: 28 additions & 21 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
# 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):
while l <= r:
mid = l + (r - l) // 2

if arr[mid] == x:
return mid
elif arr[mid] < x:
l = mid + 1
else:
r = mid - 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 %d" % result)
else:
print("Element is not present in array")
61 changes: 38 additions & 23 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
# Python program for implementation of Quicksort Sort

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


#write your code here


# 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]),


# Python program for implementation of Quick Sort

# Approach:
# Pick the last element as pivot.
# Place all smaller elements before pivot and greater elements after pivot.
# Then recursively sort the left and right parts.

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):
if low < high:
pi = partition(arr, low, high)

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


# 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], end=" ")
68 changes: 42 additions & 26 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
# 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):
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):
slow_ptr = self.head
fast_ptr = self.head

if self.head is not None:
while fast_ptr is not None and fast_ptr.next is not None:
fast_ptr = fast_ptr.next.next
slow_ptr = slow_ptr.next

print("The middle element is:", slow_ptr.data)


# Driver code
list1 = LinkedList()
list1.push(5)
list1.push(4)
list1.push(2)
list1.push(3)
list1.push(1)

list1.printMiddle()
68 changes: 51 additions & 17 deletions Exercise_4.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,52 @@
# Python program for implementation of MergeSort
# 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:
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):
for i in range(len(arr)):
print(arr[i], end=" ")
print()


# 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)
57 changes: 54 additions & 3 deletions Exercise_5.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
# Python program for implementation of Quicksort
# Python program for implementation of Iterative Quicksort

# 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
size = h - l + 1
stack = [0] * size

top = -1

top += 1
stack[top] = l

top += 1
stack[top] = h

while top >= 0:
h = stack[top]
top -= 1

l = stack[top]
top -= 1

p = partition(arr, l, h)

if p - 1 > l:
top += 1
stack[top] = l

top += 1
stack[top] = p - 1

if p + 1 < h:
top += 1
stack[top] = p + 1

top += 1
stack[top] = h


# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
n = len(arr)

quickSortIterative(arr, 0, n - 1)

print("Sorted array is:")
for i in range(n):
print("%d" % arr[i], end=" ")