From b3df4340903143aed24a3e70d70a9c09e985dae9 Mon Sep 17 00:00:00 2001 From: shellhub Date: Mon, 12 Apr 2021 19:13:32 +0800 Subject: [PATCH 1/2] selection sort --- Sorts/SelectionSort.java | 62 ++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 41 deletions(-) diff --git a/Sorts/SelectionSort.java b/Sorts/SelectionSort.java index 90f852df95cc..2401258d7aa7 100644 --- a/Sorts/SelectionSort.java +++ b/Sorts/SelectionSort.java @@ -1,68 +1,48 @@ package Sorts; -/** - * @author Varun Upadhyay (https://github.com/varunu28) - * @author Podshivalov Nikita (https://github.com/nikitap492) - * @see SortAlgorithm - */ public class SelectionSort implements SortAlgorithm { /** - * This method swaps the two elements in the array - * - * @param - * @param arr, i, j The array for the swap and the indexes of the to-swap elements - */ - public 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 + * Generic selection sort algorithm in increasing order. + * @param arr the array to be sorted. + * @param the class of array. + * @return sorted array. */ @Override public > T[] sort(T[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { - // Initial index of min - int min = i; - + int minIndex = i; for (int j = i + 1; j < n; j++) { - if (arr[min].compareTo(arr[j]) > 0) { - min = j; + if (arr[minIndex].compareTo(arr[j]) > 0) { + minIndex = j; } } - - // Swapping if index of min is changed - if (min != i) { - swap(arr, i, min); + if (minIndex != i) { + T temp = arr[i]; + arr[i] = arr[minIndex]; + arr[minIndex] = temp; } } - return arr; } - // Driver Program + /** + * Driver Code + */ public static void main(String[] args) { Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12}; - SelectionSort selectionSort = new SelectionSort(); - Integer[] sorted = selectionSort.sort(arr); + for (int i = 0; i < sorted.length - 1; ++i) { + assert sorted[i] <= sorted[i + 1]; + } - // Output => 1 4 6 9 12 23 54 78 231 - SortUtils.print(sorted); - - // String Input String[] strings = {"c", "a", "e", "b", "d"}; String[] sortedStrings = selectionSort.sort(strings); - - // Output => a b c d e - SortUtils.print(sortedStrings); + for (int i = 0; i < sortedStrings.length - 1; ++i) { + assert strings[i].compareTo(strings[i + 1]) <= 0; + } } -} +} \ No newline at end of file From b37e7b9fc0bf1040d52bbec859567928a335f268 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 12 Apr 2021 11:14:31 +0000 Subject: [PATCH 2/2] Formatted with Google Java Formatter --- Sorts/SelectionSort.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Sorts/SelectionSort.java b/Sorts/SelectionSort.java index 2401258d7aa7..df0e4767231f 100644 --- a/Sorts/SelectionSort.java +++ b/Sorts/SelectionSort.java @@ -4,6 +4,7 @@ public class SelectionSort implements SortAlgorithm { /** * Generic selection sort algorithm in increasing order. + * * @param arr the array to be sorted. * @param the class of array. * @return sorted array. @@ -27,9 +28,7 @@ public > T[] sort(T[] arr) { return arr; } - /** - * Driver Code - */ + /** Driver Code */ public static void main(String[] args) { Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12}; @@ -45,4 +44,4 @@ public static void main(String[] args) { assert strings[i].compareTo(strings[i + 1]) <= 0; } } -} \ No newline at end of file +}