I'm writing a java program where the user can select a sorting algorithm from a list and specify the size of an array to be sorted. The program then generates an array of the specified size filled with random integers and uses a switch statement with a case for each sorting algorithm to call the method for the user's chosen algorithm using the generated array as a parameter.
I want to update my code to allow the user to specify how many arrays are to be sorted with their chosen algorithm. I want to use a for loop to randomly generate and sort the arrays however this doesn't seem ideal because I would have to either:
Place the switch block inside the for loop and check it every loop, even though the same sorting algorithm will be used each time.
Place a for loop in each case within the switch block and have a lot of repeated code in the form of for loops.
Is there a better implementation for this than using switch cases and for loops?
Define an interface (perhaps even a functional interface) named Sorter
that has a method
int [] sort( final int [] values )
or
var sort( final int [] values )
if you sort the array in-place.
Implement that interface for each sorting algorithm.
Then you can have a variable Sorter sorter
in your program that holds the implementation; it will be initialised in your switch/case
statement, based on the user selection.
In your for
loop, you will call sorter.sort()
for each array to sort.
You can even avoid the switch/case
statement by creating a Map<String,Sorter>
data structure that is initialised with the name of the sort algorithm as the key and instances of the implementation of Sorter
as the value. If Sorter
is a functional interface, you can just assign a reference to the respective sort()
methods to the Map
.
However, this is known as the Strategy pattern …
I decided to try throwing together an example of how you could go about this based off of tquadrat
's idea of using a functional interface. I hope it helps!
import java.util.ArrayList;
import java.util.Random;
public class Sorting {
@FunctionalInterface
interface SortType {
Integer[] sort(Integer[] array);
}
public static void main(String[] args) {
int numArrays = 5;
int numValues = 10;
ArrayList<Integer[]> unsortedArrays = generateArrays(numArrays, numValues);
System.out.println("Unsorted:");
print(unsortedArrays);
ArrayList<Integer[]> sortedArrays = sortArrays(unsortedArrays, Sorting::bubbleSort);
System.out.println("\nSorted:");
print(sortedArrays);
}
//Put together random values
private static ArrayList<Integer[]> generateArrays(int numArrays, int numValues) {
ArrayList<Integer[]> unsortedArrays = new ArrayList<>();
Random rand = new Random();
for (int i = 0; i < numArrays; i++) {
Integer[] array = new Integer[numValues];
for (int j = 0; j < numValues; j++) {
array[j] = rand.nextInt(100);
}
unsortedArrays.add(array);
}
return unsortedArrays;
}
//Loop through using the given sorting method on each array
private static ArrayList<Integer[]> sortArrays(ArrayList<Integer[]> arrays, SortType sortType) {
ArrayList<Integer[]> sortedArrays = new ArrayList<>();
for (Integer[] array : arrays) {
sortedArrays.add(sortType.sort(array));
}
return sortedArrays;
}
//Example sort to use with parameters and return matching the interface
private static Integer[] bubbleSort(Integer[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
return array;
}
//Method to print the results
private static void print(ArrayList<Integer[]> arrays) {
for (Integer[] array : arrays) {
for (Integer i : array)
System.out.print(i + ", ");
System.out.println();
}
}
}
Sample output:
Unsorted:
67, 54, 83, 67, 62, 96, 6, 24, 66, 19,
3, 37, 45, 36, 81, 45, 5, 46, 5, 84,
10, 8, 95, 50, 82, 38, 36, 18, 80, 98,
52, 27, 18, 17, 77, 51, 18, 72, 55, 76,
79, 84, 92, 85, 61, 74, 64, 29, 95, 64,
Sorted:
6, 19, 24, 54, 62, 66, 67, 67, 83, 96,
3, 5, 5, 36, 37, 45, 45, 46, 81, 84,
8, 10, 18, 36, 38, 50, 80, 82, 95, 98,
17, 18, 18, 27, 51, 52, 55, 72, 76, 77,
29, 61, 64, 64, 74, 79, 84, 85, 92, 95,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.