DSA Lab 9
DSA Lab 9
UNIVERSITY OF SARGODHA
Lab 9 Manual
Divide and Conquer Algorithms
Date Performed
Marks obtained
Instructor Signature
CLO-1 Illustrate understanding of key concepts of Data structures
and Algorithms using Python PyCharm Platform
CLO-2 Design a programming application by applying concepts of
Data Structures and algorithms leading to the solution of a moderate
scale-programming problem.
CLO-3 Write lab notes, effective communication and the analysis of
the given problem to perform in the laboratory environment.
CLO-4 Demonstrate involvement in the Project as a team or
individually with respect to the contribution.
OBJECTIVE:
Objective of this experiment is to design, develop and implement divide and conquer
algorithms. The algorithms implemented in this lab session will be quicksort and tower of
Hanoi.
TOWER OF HANOI
Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle
is to move the entire stack to another rod, obeying the following simple rules:
1) Only one disk can be moved at a time.
2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack
i.e. a disk can only be moved if it is the uppermost disk on a stack.
3) No disk may be placed on top of a smaller disk.
ALGORITHM
Step 1 − Move n-1 disks from source to aux
Step 2 − Move nth disk from source to dest
Step 3 − Move n-1 disks from aux to dest
START
Procedure Hanoi(disk, source, dest, aux)
IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF
END Procedure
STOP
Python Program Code:
import time
# Function to find the maximum value in a given array using Divide and Conquer
def dac_max(arr, index, l):
if l - 1 == 0:
return arr[index]
if index >= l - 2:
if arr[index] > arr[index + 1]:
return arr[index]
else:
return arr[index + 1]
# Function to find the minimum value in a given array using Divide and Conquer
def dac_min(arr, index, l):
if l - 1 == 0:
return arr[index]
if index >= l - 2:
if arr[index] < arr[index + 1]:
return arr[index]
else:
return arr[index + 1]
# Driver code
def main():
arr = [51, 20, 89, 65, 42, 10, 99]
n = len(arr)
max_val = dac_max(arr, 0, n)
min_val = dac_min(arr, 0, n)
# Measure end time
end_time = time.time()
print("Maximum:", max_val)
print("Minimum:", min_val)
if __name__ == "__main__":
main()
OUTPUT:
Maximum: 99
Minimum: 10
Execution Time: 0.0 seconds
start_time = time.time()
max_val = arr[0]
for i in range(1, len(arr)):
if arr[i] > max_val:
max_val = arr[i]
end_time = time.time()
execution_time = end_time - start_time
print("Maximum:", max_val)
print(f"Execution Time (dac_max): {execution_time} seconds")
return max_val
end_time = time.time()
execution_time = end_time - start_time
print("Minimum:", min_val)
print(f"Execution Time (dac_min): {execution_time} seconds")
return min_val
# Driver code
def main():
size = 200000
arr = list(range(1, size + 1)) # Populate the list with numbers from 1 to 200,000
max_val = dac_max(arr)
min_val = dac_min(arr)
if __name__ == "__main__":
main()
OUTPUT:
Maximum: 200000
Execution Time (dac_max): 0.01886725425720215 seconds
Minimum: 1
Execution Time (dac_min): 0.013058662414550781 seconds
Outcomes Assessed:
CLO-1 Illustrate understanding of key concepts of Data structures and Algorithms using Dev
C++ Platform.
CLO-2 Design a programming application by applying concepts of Data Structures and
algorithms leading to the solution of a moderate scale-programming problem.
CLO-3 Write lab notes, effective communication and analysis of the given problem to perform
in the laboratory environment.
CLO-4 Demonstrate involvement in the Project as a team or individually with respect to the
contribution.
Total