Practical No. 9 Divide and Conquer_ Implement Algorithms Like Merge Sort and Strassen's Matrix Multiplication.
Practical No. 9 Divide and Conquer_ Implement Algorithms Like Merge Sort and Strassen's Matrix Multiplication.
9
Divide and Conquer: Implement algorithms like merge sort and Strassen's Matrix
Multiplication.
Practical Implementation
1. Merge Sort
Merge Sort is a sorting algorithm based on the Divide and Conquer technique. It
divides the array into halves, sorts each half recursively, and merges the sorted
halves.
def merge_sort(arr):
sorted_list = []
while left and right:
if left[0] < right[0]:
sorted_list.append(left.pop(0))
else:
sorted_list.append(right.pop(0))
return sorted_list
import numpy as np
def strassen(A, B):
return C
# Test the Strassen's algorithm for matrix multiplication
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
C = strassen(A, B)
print("Matrix Product C:")
print(C)
Complexity:
1. Merge Sort:
○ Time Complexity: O(nlogn)O(n \log n)O(nlogn)
○ Space Complexity: O(n)O(n)O(n)
2. Strassen’s Matrix Multiplication:
○ Time Complexity: O(nlog27)≈O(n2.81)O(n^{\log_2 7}) \
approx O(n^{2.81})O(nlog27)≈O(n2.81)
○ Space Complexity: O(n2)O(n^2)O(n2)
Practical Applications:
1. Merge Sort:
○ Efficient sorting algorithm used in scenarios where stability
(preserving equal element order) is required.
2. Strassen’s Matrix Multiplication:
○ Used in high-performance computing tasks, large matrix
calculations, and algorithms in machine learning.