Implement the Template Method design pattern for sorting algorithms. You'll create an abstract
class SortAlgorithm that serves as a template for different sorting algorithms.
Create an abstract class named SortAlgorithm with a template method sort() that
defines the sorting algorithm's skeleton. This method should call two abstract
methods:
● compare() to compare elements during sorting.
● swap() to swap elements if necessary.
Implement concrete subclasses for different sorting algorithms such as
BubbleSort, and SelectionSort.
Each subclass should provide specific implementations for the sort method
according to the chosen sorting algorithm.
Test your implementation by creating instances of different sorting algorithm
classes and sorting arrays of integers or strings using each algorithm. JUST USE PRINTSTAEMENTS IN THE FUNCTION NO LOGIC IS NEEDED ONYL SHOW TEMPLATE
Assistant
To implement the Template Method design pattern for sorting algorithms, we'll start by defining the abstract class SortAlgorithm, and then create concrete subclasses like BubbleSort and SelectionSort. Each subclass will implement the sort() method following the specific behaviors of the corresponding algorithm but will rely on the abstract methods compare() and swap() which will be called within sort(). Here's how this can be laid out in Python:
python
from abc import ABC, abstractmethod
class SortAlgorithm(ABC):
@abstractmethod
def compare(self, x, y):
pass
@abstractmethod
def swap(self, array, i, j):
pass
@abstractmethod
def sort(self, array):
pass
class BubbleSort(SortAlgorithm):
def compare(self, x, y):
print(f"Comparing {x} and {y}")
return x > y
def swap(self, array, i, j):
print(f"Swapping {array[i]} and {array[j]}")
array[i], array[j] = array[j], array[i]
def sort(self, array):
n = len(array)
for i in range(n):
for j in range(0, n - i - 1):
if self.compare(array[j], array[j + 1]):
self.swap(array, j, j + 1)
return array
class SelectionSort(SortAlgorithm):
def compare(self, x, y):
print(f"Comparing {x} and {y}")
return x > y
def swap(self, array, i, j):
print(f"Swapping {array[i]} and {array[j]}")
array[i], array[j] = array[j], array[i]
def sort(self, array):
n = len(array)
for i in range(n):
min_idx = i
for j in range(i+1, n):
if self.compare(array[j], array[min_idx]):
min_idx = j
self.swap(array, i, min_idx)
return array
# Test the implementation
if __name__ == "__main__":
array_bubble = [64, 34, 25, 12, 22, 11, 90]
bubble_sort = BubbleSort()
print("Bubble Sort:")
sorted_bubble = bubble_sort.sort(array_bubble)
print(sorted_bubble)
array_selection = [64, 34, 25, 12, 22, 11, 90]
selection_sort = SelectionSort()
print("Selection Sort:")
sorted_selection = selection_sort.sort(array_selection)
print(sorted_selection)
In this implementation:
SortAlgorithm is an abstract base class with abstract methods compare(), swap(), and sort(). These methods define what operations must be implemented in any concrete sorting algorithm class that inherits from SortAlgorithm.
The BubbleSort class and SelectionSort class are concrete implementations. In their sort() methods, they follow the logic specific to bubble sort and selection sort algorithms, respectively.
In the compare() methods, a simple comparison is made, which can be tailored to facilitate different ordering (such as ascending or descending).
The swap() method handles swapping elements in the array.
We have used simple print statements as requested to demonstrate how each step is approached, as per the Template Method pattern.
The test block demonstrates how two different sort algorithms work with the same data input, and sorts the input array demonstrating each swap and comparison step.