Java: Sortable interface with bubbleSort and selectionSort implementations
Java Interface: Exercise-7 with Solution
Write a Java program to create an interface Sortable with a method sort() that sorts an array of integers in ascending order. Create two classes BubbleSort and SelectionSort that implement the Sortable interface and provide their own implementations of the sort() method.
Sample Solution:
Java Code:
// Sortable.java
// Declare the Sortable interface
interface Sortable {
// Declare the abstract method "sort" that classes implementing this interface must provide
void sort(int[] arr);
}
// BubbleSort.java
// Declare the BubbleSort class, which implements the Sortable interface
class BubbleSort implements Sortable {
// Implement the "sort" method required by the Sortable interface
public void sort(int[] arr) {
// Get the length of the array
int n = arr.length;
// Outer loop for the number of passes
for (int i = 0; i < n - 1; i++) {
// Inner loop for comparisons and swaps
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
// SelectionSort.java
// Declare the SelectionSort class, which implements the Sortable interface
class SelectionSort implements Sortable {
// Implement the "sort" method required by the Sortable interface
public void sort(int[] arr) {
// Get the length of the array
int n = arr.length;
// Outer loop for the current element
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
// Inner loop to find the minimum element in the unsorted part of the array
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap arr[i] and arr[minIndex]
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
// Main.java
// Declare the Main class
public class Main {
public static void main(String[] args) {
// Create an array of integers to be sorted
int[] arr = {
4,
2,
0,
3,
1,
6,
8
};
// Create an instance of BubbleSort and perform sorting
Sortable bubbleSort = new BubbleSort();
bubbleSort.sort(arr);
System.out.print("Bubble Sort: ");
printArray(arr);
// Create an instance of SelectionSort and perform sorting
Sortable selectionSort = new SelectionSort();
selectionSort.sort(arr);
System.out.print("Selection Sort: ");
printArray(arr);
}
// Method to print the elements of an array
private static void printArray(int[] arr) {
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}
Sample Output:
Bubble Sort: 0 1 2 3 4 6 8 Selection Sort: 0 1 2 3 4 6 8
Explanation:
In the above exercise –
The "BubbleSort" class and "SelectionSort" class both implement the Sortable interface and provide their own implementations of the sort() method. The "BubbleSort" class uses the bubble sort algorithm, and the SelectionSort class uses the selection sort algorithm.
In the main() method, we create an array of integers and then create instances of the BubbleSort and SelectionSort 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 BubbleSort Java:
Flowchart of SelectionSort Java:
Flowchart of Main Java:
Java Code Editor:
Previous: Drawable interface & shape implementations.
Next: A Playable Interface with Football, Volleyball, and Basketball implementations.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/java-exercises/java-interface-exercise-7.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics