Last active
December 1, 2024 09:36
-
-
Save Nirajpaul2/1ed825cf53bb296363e29497e2301c65 to your computer and use it in GitHub Desktop.
SortingStrategy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Define a protocol that provides a common interface for sorting strategies | |
| protocol SortingStrategy { | |
| func sort(_ numbers: inout [Int]) // Method to sort an array of integers | |
| } | |
| // Concrete implementation of the SortingStrategy using Bubble Sort | |
| class BubbleSort: SortingStrategy { | |
| func sort(_ numbers: inout [Int]) { | |
| numbers.sort(by: <) // Sorting in ascending order (using a simple built-in sort for demonstration) | |
| print("Sorting array using bubble sort strategy") | |
| // Note: Actual Bubble Sort implementation can be added here for a realistic demonstration. | |
| } | |
| } | |
| // Concrete implementation of the SortingStrategy using Insertion Sort | |
| class InsertionSort: SortingStrategy { | |
| func sort(_ numbers: inout [Int]) { | |
| print("Sorting array using insertion sort strategy") | |
| // Note: Actual Insertion Sort implementation can be added here for a realistic demonstration. | |
| } | |
| } | |
| // Concrete implementation of the SortingStrategy using Quick Sort | |
| class QuickSort: SortingStrategy { | |
| func sort(_ numbers: inout [Int]) { | |
| numbers.sort(by: >) // Sorting in descending order (using a simple built-in sort for demonstration) | |
| print("Sorting array using quick sort strategy") | |
| // Note: Actual Quick Sort implementation can be added here for a realistic demonstration. | |
| } | |
| } | |
| // Concrete implementation of the SortingStrategy using Merge Sort | |
| class MergeSort: SortingStrategy { | |
| func sort(_ numbers: inout [Int]) { | |
| print("Sorting array using merge sort strategy") | |
| // Note: Actual Merge Sort implementation can be added here for a realistic demonstration. | |
| } | |
| } | |
| // Context class that works with a SortingStrategy | |
| class SortingContext { | |
| private var strategy: SortingStrategy // Holds the current sorting strategy | |
| // Initializer to set a default strategy | |
| init(strategy: SortingStrategy) { | |
| self.strategy = strategy | |
| } | |
| // Method to change the strategy dynamically | |
| func setStrategy(_ strategy: SortingStrategy) { | |
| self.strategy = strategy | |
| } | |
| // Method to sort the numbers using the current strategy | |
| func arrange(_ numbers: inout [Int]) { | |
| strategy.sort(&numbers) // Delegates sorting to the current strategy | |
| } | |
| } | |
| // Main function to demonstrate the Strategy Pattern | |
| func main() { | |
| var numbers = [2, 3, 5, 1, 4] // Array of numbers to be sorted | |
| // Initialize the context with the BubbleSort strategy | |
| let context = SortingContext(strategy: BubbleSort()) | |
| context.arrange(&numbers) // Sort using Bubble Sort | |
| print("BubbleSort: \(numbers)") // Print the sorted array | |
| // Reset the numbers array for a fresh sort | |
| numbers = [2, 3, 5, 1, 4] | |
| // Switch the strategy to QuickSort | |
| context.setStrategy(QuickSort()) | |
| context.arrange(&numbers) // Sort using Quick Sort | |
| print("QuickSort: \(numbers)") // Print the sorted array | |
| // Reset the numbers array for a fresh sort | |
| numbers = [2, 3, 5, 1, 4] | |
| // Switch the strategy to MergeSort | |
| context.setStrategy(MergeSort()) | |
| context.arrange(&numbers) // Sort using Merge Sort | |
| print("MergeSort: \(numbers)") // Print the sorted array | |
| // Reset the numbers array for a fresh sort | |
| numbers = [2, 3, 5, 1, 4] | |
| // Switch the strategy to InsertionSort | |
| context.setStrategy(InsertionSort()) | |
| context.arrange(&numbers) // Sort using Insertion Sort | |
| } | |
| // Call the main function to execute the program | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment