mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Formatted with Google Java Formatter
This commit is contained in:
@@ -5,67 +5,64 @@ package Sorts;
|
||||
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
||||
* @see SortAlgorithm
|
||||
*/
|
||||
|
||||
public class SelectionSort implements SortAlgorithm {
|
||||
|
||||
/**
|
||||
* This method swaps the two elements in the array
|
||||
* @param <T>
|
||||
* @param arr, i, j The array for the swap and
|
||||
the indexes of the to-swap elements
|
||||
*/
|
||||
|
||||
public <T> void swap(T[] arr, int i, int j) {
|
||||
T temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method implements the Generic Selection Sort
|
||||
*
|
||||
* @param arr The array to be sorted
|
||||
* Sorts the array in increasing order
|
||||
**/
|
||||
@Override
|
||||
public <T extends Comparable<T>> T[] sort(T[] arr) {
|
||||
int n = arr.length;
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
// Initial index of min
|
||||
int min = i;
|
||||
/**
|
||||
* This method swaps the two elements in the array
|
||||
*
|
||||
* @param <T>
|
||||
* @param arr, i, j The array for the swap and the indexes of the to-swap elements
|
||||
*/
|
||||
public <T> void swap(T[] arr, int i, int j) {
|
||||
T temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
if (arr[min].compareTo(arr[j]) < 0) {
|
||||
min = j;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* This method implements the Generic Selection Sort
|
||||
*
|
||||
* @param arr The array to be sorted Sorts the array in increasing order
|
||||
*/
|
||||
@Override
|
||||
public <T extends Comparable<T>> T[] sort(T[] arr) {
|
||||
int n = arr.length;
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
// Initial index of min
|
||||
int min = i;
|
||||
|
||||
// Swapping if index of min is changed
|
||||
if (min != i) {
|
||||
swap(arr, i, min);
|
||||
}
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
if (arr[min].compareTo(arr[j]) < 0) {
|
||||
min = j;
|
||||
}
|
||||
}
|
||||
|
||||
return arr;
|
||||
// Swapping if index of min is changed
|
||||
if (min != i) {
|
||||
swap(arr, i, min);
|
||||
}
|
||||
}
|
||||
|
||||
// Driver Program
|
||||
public static void main(String[] args) {
|
||||
return arr;
|
||||
}
|
||||
|
||||
Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12};
|
||||
// Driver Program
|
||||
public static void main(String[] args) {
|
||||
|
||||
SelectionSort selectionSort = new SelectionSort();
|
||||
Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12};
|
||||
|
||||
Integer[] sorted = selectionSort.sort(arr);
|
||||
SelectionSort selectionSort = new SelectionSort();
|
||||
|
||||
// Output => 1 4 6 9 12 23 54 78 231
|
||||
SortUtils.print(sorted);
|
||||
Integer[] sorted = selectionSort.sort(arr);
|
||||
|
||||
// String Input
|
||||
String[] strings = {"c", "a", "e", "b", "d"};
|
||||
String[] sortedStrings = selectionSort.sort(strings);
|
||||
// Output => 1 4 6 9 12 23 54 78 231
|
||||
SortUtils.print(sorted);
|
||||
|
||||
//Output => a b c d e
|
||||
SortUtils.print(sortedStrings);
|
||||
}
|
||||
// String Input
|
||||
String[] strings = {"c", "a", "e", "b", "d"};
|
||||
String[] sortedStrings = selectionSort.sort(strings);
|
||||
|
||||
// Output => a b c d e
|
||||
SortUtils.print(sortedStrings);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user