Java: Sortable interface for quickSort and mergeSort
Write a Java program to create an interface Sortable with a method sort (int[] array) that sorts an array of integers in descending order. Create two classes QuickSort and MergeSort that implement the Sortable interface and provide their own implementations of the sort() method.
Sample Solution:
Java Code:
Sample Output:
Quick Sort (Descending Order): 9 8 6 5 3 2 1 0 Merge Sort (Descending Order): 9 8 6 5 3 2 1 0
Explanation:
In the above exercise –
- The "QuickSort" class and "MergeSort" class both implement the Sortable interface and provide their own implementations of the sort() method. The "QuickSort" class uses the quick sort algorithm, and the "MergeSort" class uses the merge sort algorithm.
- In the main() method, we create an array of integers and then create instances of the "QuickSort" and "MergeSort" classes. We call the sort() method on each instance, pass in the array, and then print the sorted array using the printArray() method.
Flowchart of Sortable Java:
Flowchart of QuickSort Java:
Flowchart of MergeSort Java:
For more Practice: Solve these Related Problems:
- Write a Java program to implement a Sortable interface that sorts an array of integers in descending order using heap sort.
- Write a Java program to create a Sortable interface and implement it in classes for descending order QuickSort and MergeSort.
- Write a Java program to sort an array of custom objects in descending order using the Sortable interface with a custom comparator.
- Write a Java program to dynamically switch between different sorting algorithms for descending order based on input size using the Sortable interface.
Java Code Editor:
What is the difficulty level of this exercise?