From 554b6cf0062b83dab5375adc49178eec1f266276 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sun, 11 Aug 2024 19:44:22 +0200 Subject: [PATCH] refactor: simple improvements and cleanup for different sorts (#5318) --- .../sorts/DutchNationalFlagSort.java | 22 ++++---- .../com/thealgorithms/sorts/GnomeSort.java | 53 ++----------------- .../com/thealgorithms/sorts/HeapSort.java | 4 +- .../com/thealgorithms/sorts/QuickSort.java | 12 ++--- .../sorts/SelectionSortRecursive.java | 4 +- .../com/thealgorithms/sorts/StoogeSort.java | 24 ++++----- .../com/thealgorithms/sorts/StrandSort.java | 8 +-- .../com/thealgorithms/sorts/SwapSort.java | 7 ++- .../java/com/thealgorithms/sorts/TimSort.java | 12 ++--- 9 files changed, 51 insertions(+), 95 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/DutchNationalFlagSort.java b/src/main/java/com/thealgorithms/sorts/DutchNationalFlagSort.java index 20b8f0ba..abfcb452 100644 --- a/src/main/java/com/thealgorithms/sorts/DutchNationalFlagSort.java +++ b/src/main/java/com/thealgorithms/sorts/DutchNationalFlagSort.java @@ -12,31 +12,31 @@ package com.thealgorithms.sorts; public class DutchNationalFlagSort implements SortAlgorithm { @Override - public > T[] sort(T[] unsorted) { - return dutchNationalFlagSort(unsorted, unsorted[(int) Math.ceil((unsorted.length) / 2.0) - 1]); + public > T[] sort(T[] array) { + return dutchNationalFlagSort(array, array[(int) Math.ceil((array.length) / 2.0) - 1]); } - public > T[] sort(T[] unsorted, T intendedMiddle) { - return dutchNationalFlagSort(unsorted, intendedMiddle); + public > T[] sort(T[] array, T intendedMiddle) { + return dutchNationalFlagSort(array, intendedMiddle); } - private > T[] dutchNationalFlagSort(T[] arr, T intendedMiddle) { + private > T[] dutchNationalFlagSort(final T[] array, final T intendedMiddle) { int i = 0; int j = 0; - int k = arr.length - 1; + int k = array.length - 1; while (j <= k) { - if (0 > arr[j].compareTo(intendedMiddle)) { - SortUtils.swap(arr, i, j); + if (0 > array[j].compareTo(intendedMiddle)) { + SortUtils.swap(array, i, j); j++; i++; - } else if (0 < arr[j].compareTo(intendedMiddle)) { - SortUtils.swap(arr, j, k); + } else if (0 < array[j].compareTo(intendedMiddle)) { + SortUtils.swap(array, j, k); k--; } else { j++; } } - return arr; + return array; } } diff --git a/src/main/java/com/thealgorithms/sorts/GnomeSort.java b/src/main/java/com/thealgorithms/sorts/GnomeSort.java index 9bef6a28..b074c271 100644 --- a/src/main/java/com/thealgorithms/sorts/GnomeSort.java +++ b/src/main/java/com/thealgorithms/sorts/GnomeSort.java @@ -9,63 +9,20 @@ package com.thealgorithms.sorts; public class GnomeSort implements SortAlgorithm { @Override - public > T[] sort(T[] arr) { + public > T[] sort(final T[] array) { int i = 1; int j = 2; - while (i < arr.length) { - if (SortUtils.less(arr[i - 1], arr[i])) { + while (i < array.length) { + if (SortUtils.less(array[i - 1], array[i])) { i = j++; } else { - SortUtils.swap(arr, i - 1, i); + SortUtils.swap(array, i - 1, i); if (--i == 0) { i = j++; } } } - return null; - } - - public static void main(String[] args) { - Integer[] integers = { - 4, - 23, - 6, - 78, - 1, - 26, - 11, - 23, - 0, - -6, - 3, - 54, - 231, - 9, - 12, - }; - String[] strings = { - "c", - "a", - "e", - "b", - "d", - "dd", - "da", - "zz", - "AA", - "aa", - "aB", - "Hb", - "Z", - }; - GnomeSort gnomeSort = new GnomeSort(); - - gnomeSort.sort(integers); - gnomeSort.sort(strings); - - System.out.println("After sort : "); - SortUtils.print(integers); - SortUtils.print(strings); + return array; } } diff --git a/src/main/java/com/thealgorithms/sorts/HeapSort.java b/src/main/java/com/thealgorithms/sorts/HeapSort.java index 91d556b1..e798fb91 100644 --- a/src/main/java/com/thealgorithms/sorts/HeapSort.java +++ b/src/main/java/com/thealgorithms/sorts/HeapSort.java @@ -25,13 +25,13 @@ public class HeapSort implements SortAlgorithm { return array; } - private static > void heapify(T[] array, int n) { + private > void heapify(final T[] array, final int n) { for (int k = n / 2; k >= 1; k--) { siftDown(array, k, n); } } - private static > void siftDown(T[] array, int k, int n) { + private > void siftDown(final T[] array, int k, final int n) { while (2 * k <= n) { int j = 2 * k; if (j < n && SortUtils.less(array[j - 1], array[j])) { diff --git a/src/main/java/com/thealgorithms/sorts/QuickSort.java b/src/main/java/com/thealgorithms/sorts/QuickSort.java index 3ebdd96c..3abb1aae 100644 --- a/src/main/java/com/thealgorithms/sorts/QuickSort.java +++ b/src/main/java/com/thealgorithms/sorts/QuickSort.java @@ -25,9 +25,9 @@ class QuickSort implements SortAlgorithm { * @param right The last index of an array * @param array The array to be sorted */ - private static > void doSort(T[] array, int left, int right) { + private static > void doSort(T[] array, final int left, final int right) { if (left < right) { - int pivot = randomPartition(array, left, right); + final int pivot = randomPartition(array, left, right); doSort(array, left, pivot - 1); doSort(array, pivot, right); } @@ -41,8 +41,8 @@ class QuickSort implements SortAlgorithm { * @param right The last index of an array * @return the partition index of the array */ - private static > int randomPartition(T[] array, int left, int right) { - int randomIndex = left + (int) (Math.random() * (right - left + 1)); + private static > int randomPartition(T[] array, final int left, final int right) { + final int randomIndex = left + (int) (Math.random() * (right - left + 1)); SortUtils.swap(array, randomIndex, right); return partition(array, left, right); } @@ -56,8 +56,8 @@ class QuickSort implements SortAlgorithm { * array */ private static > int partition(T[] array, int left, int right) { - int mid = (left + right) >>> 1; - T pivot = array[mid]; + final int mid = (left + right) >>> 1; + final T pivot = array[mid]; while (left <= right) { while (SortUtils.less(array[left], pivot)) { diff --git a/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java b/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java index 32bd58a1..9d24542d 100644 --- a/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java +++ b/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java @@ -27,7 +27,7 @@ public class SelectionSortRecursive implements SortAlgorithm { * @param index the current index to start sorting from * @param the type of elements in the array (must be Comparable) */ - private static > void recursiveSelectionSort(T[] array, int index) { + private static > void recursiveSelectionSort(T[] array, final int index) { if (index == array.length - 1) { return; } @@ -46,7 +46,7 @@ public class SelectionSortRecursive implements SortAlgorithm { * @param the type of elements in the array * @return the index of the minimum element */ - private static > int findMinIndex(T[] array, int start) { + private static > int findMinIndex(T[] array, final int start) { // Base case: if start is the last index, return start if (start == array.length - 1) { return start; diff --git a/src/main/java/com/thealgorithms/sorts/StoogeSort.java b/src/main/java/com/thealgorithms/sorts/StoogeSort.java index 25830109..2a6e04ce 100644 --- a/src/main/java/com/thealgorithms/sorts/StoogeSort.java +++ b/src/main/java/com/thealgorithms/sorts/StoogeSort.java @@ -15,20 +15,20 @@ public class StoogeSort implements SortAlgorithm { return array; } - public > T[] sort(T[] unsortedArray, int start, int end) { - if (SortUtils.less(unsortedArray[end - 1], unsortedArray[start])) { - T temp = unsortedArray[start]; - unsortedArray[start] = unsortedArray[end - 1]; - unsortedArray[end - 1] = temp; + public > T[] sort(final T[] array, final int start, final int end) { + if (SortUtils.less(array[end - 1], array[start])) { + final T temp = array[start]; + array[start] = array[end - 1]; + array[end - 1] = temp; } - int len = end - start; - if (len > 2) { - int third = len / 3; - sort(unsortedArray, start, end - third); - sort(unsortedArray, start + third, end); - sort(unsortedArray, start, end - third); + final int length = end - start; + if (length > 2) { + int third = length / 3; + sort(array, start, end - third); + sort(array, start + third, end); + sort(array, start, end - third); } - return unsortedArray; + return array; } } diff --git a/src/main/java/com/thealgorithms/sorts/StrandSort.java b/src/main/java/com/thealgorithms/sorts/StrandSort.java index 58cd3562..45bbe88d 100644 --- a/src/main/java/com/thealgorithms/sorts/StrandSort.java +++ b/src/main/java/com/thealgorithms/sorts/StrandSort.java @@ -13,14 +13,14 @@ public final class StrandSort implements SortAlgorithm { * Sorts the given array using the Strand Sort algorithm. * * @param The type of elements to be sorted, must be Comparable. - * @param unsorted The array to be sorted. + * @param array The array to be sorted. * @return The sorted array. */ @Override - public > T[] sort(T[] unsorted) { - List unsortedList = new ArrayList<>(Arrays.asList(unsorted)); + public > T[] sort(T[] array) { + List unsortedList = new ArrayList<>(Arrays.asList(array)); List sortedList = strandSort(unsortedList); - return sortedList.toArray(unsorted); + return sortedList.toArray(array); } /** diff --git a/src/main/java/com/thealgorithms/sorts/SwapSort.java b/src/main/java/com/thealgorithms/sorts/SwapSort.java index fe3597c0..b5c1c389 100644 --- a/src/main/java/com/thealgorithms/sorts/SwapSort.java +++ b/src/main/java/com/thealgorithms/sorts/SwapSort.java @@ -11,11 +11,10 @@ public class SwapSort implements SortAlgorithm { @Override public > T[] sort(T[] array) { - int len = array.length; int index = 0; - while (index < len - 1) { - int amountSmallerElements = this.getSmallerElementCount(array, index); + while (index < array.length - 1) { + final int amountSmallerElements = this.getSmallerElementCount(array, index); if (amountSmallerElements > 0) { SortUtils.swap(array, index, index + amountSmallerElements); @@ -27,7 +26,7 @@ public class SwapSort implements SortAlgorithm { return array; } - private > int getSmallerElementCount(T[] array, int index) { + private > int getSmallerElementCount(final T[] array, final int index) { int counter = 0; for (int i = index + 1; i < array.length; i++) { if (SortUtils.less(array[i], array[index])) { diff --git a/src/main/java/com/thealgorithms/sorts/TimSort.java b/src/main/java/com/thealgorithms/sorts/TimSort.java index 85e16636..2d5bca2e 100644 --- a/src/main/java/com/thealgorithms/sorts/TimSort.java +++ b/src/main/java/com/thealgorithms/sorts/TimSort.java @@ -12,25 +12,25 @@ class TimSort implements SortAlgorithm { private Comparable[] aux; @Override - public > T[] sort(T[] a) { - int n = a.length; + public > T[] sort(T[] array) { + final int n = array.length; InsertionSort insertionSort = new InsertionSort(); for (int i = 0; i < n; i += SUB_ARRAY_SIZE) { - insertionSort.sort(a, i, Math.min(i + SUB_ARRAY_SIZE, n)); + insertionSort.sort(array, i, Math.min(i + SUB_ARRAY_SIZE, n)); } aux = new Comparable[n]; for (int sz = SUB_ARRAY_SIZE; sz < n; sz = sz + sz) { for (int lo = 0; lo < n - sz; lo += sz + sz) { - merge(a, lo, lo + sz - 1, Math.min(lo + sz + sz - 1, n - 1)); + merge(array, lo, lo + sz - 1, Math.min(lo + sz + sz - 1, n - 1)); } } - return a; + return array; } - private > void merge(T[] a, int lo, int mid, int hi) { + private > void merge(T[] a, final int lo, final int mid, final int hi) { int i = lo; int j = mid + 1; System.arraycopy(a, lo, aux, lo, hi + 1 - lo);