Tất cả các phương thức đó đều nằm trong 1 class ạ
public class SelectionSort {
/**
* Declare a final variable LOGGER to use logging for (@code SelectionSort)
* class
*
*/
private static final Logger LOGGER = Logger.getLogger(SelectionSort.class.getName());
/**
* This method is Selection Sort Algorithm
*
* @param array
*/
private static int[] sortSelection(int array[]) {
for (int i = 0; i < array.length - 1; i++) {
//Find the minimum element in unsorted array
int minIndex = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
//Swap the found minimum element with i-th element
int tempValue = array[minIndex];
array[minIndex] = array[i];
array[i] = tempValue;
}
return array;
}
/**
* This method checks the input that the user enters from the keyboard and
* input must be positive decimal number
*
* @param string
* @param maxLimit
* @param minLimit
* @return input
*/
private int checkEnteredInput(String string, int maxLimit, int minLimit) {
Scanner sc = new Scanner(System.in);
System.out.print(string);
int input = 0;
while (true) {
try {
input = Integer.parseInt(sc.nextLine().trim());
if (input > minLimit || input < maxLimit) {
LOGGER.warning("Invalid input! Please enter again: ");
} else {
return input;
}
} catch (NumberFormatException e) {
LOGGER.warning("Invalid input! Please enter again: ");
}
}
}
private int checkEnteredElements(String string, int maxLimit, int minLimit, int arr[]){
Scanner sc = new Scanner(System.in);
System.out.print(string);
int enteredElement = 0;
for (int i = 0; i < arr.length; i++) {
try {
enteredElement = Integer.parseInt(sc.nextLine().trim());
if (enteredElement > minLimit || enteredElement < maxLimit) {
LOGGER.log(Level.WARNING, "Invalid input! Please enter again arr[{0}]", i);
} else {
return enteredElement;
}
} catch (NumberFormatException e) {
LOGGER.log(Level.WARNING, "Invalid input! Please enter again arr[{0}]", i);
}
}
return enteredElement;
}
/**
* This method print elements of array before and after using Selection Sort
* Algorithm
*
* @param arr
*/
private void printArray(int arr[]) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
System.out.print(arr[i] + "]");
} else {
System.out.print(arr[i] + ", ");
}
}
}
/**
* This is main method
*
* @param args
*/
public static void main(String[] args) {
SelectionSort selectionSort = new SelectionSort();
System.out.print("Enter number of array: ");
int inputArray = selectionSort.checkEnteredInput("", 1, Integer.MAX_VALUE);
int array[] = new int[inputArray]; //Creat an array
for (int i = 0; i < array.length; i++) {
// array[i] = selectionSort.checkEnteredInput("arr[" + i + "]= ", Integer.MIN_VALUE, Integer.MAX_VALUE);
array[i] = selectionSort.checkEnteredElements("arr[" + i + "] = ", Integer.MIN_VALUE, Integer.MAX_VALUE, array);
}
System.out.print("Unsorted array: ");
selectionSort.printArray(array); //Print all elements of array before sorting
array = sortSelection(array);
System.out.println("");
System.out.print("Sorted Array: "); //Print all elements of array after sorting
selectionSort.printArray(array);
}
}