refactor: simple improvements and cleanup for different sorts (#5318)
This commit is contained in:
parent
197718842f
commit
554b6cf006
@ -12,31 +12,31 @@ package com.thealgorithms.sorts;
|
||||
public class DutchNationalFlagSort implements SortAlgorithm {
|
||||
|
||||
@Override
|
||||
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
|
||||
return dutchNationalFlagSort(unsorted, unsorted[(int) Math.ceil((unsorted.length) / 2.0) - 1]);
|
||||
public <T extends Comparable<T>> T[] sort(T[] array) {
|
||||
return dutchNationalFlagSort(array, array[(int) Math.ceil((array.length) / 2.0) - 1]);
|
||||
}
|
||||
|
||||
public <T extends Comparable<T>> T[] sort(T[] unsorted, T intendedMiddle) {
|
||||
return dutchNationalFlagSort(unsorted, intendedMiddle);
|
||||
public <T extends Comparable<T>> T[] sort(T[] array, T intendedMiddle) {
|
||||
return dutchNationalFlagSort(array, intendedMiddle);
|
||||
}
|
||||
|
||||
private <T extends Comparable<T>> T[] dutchNationalFlagSort(T[] arr, T intendedMiddle) {
|
||||
private <T extends Comparable<T>> 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;
|
||||
}
|
||||
}
|
||||
|
@ -9,63 +9,20 @@ package com.thealgorithms.sorts;
|
||||
public class GnomeSort implements SortAlgorithm {
|
||||
|
||||
@Override
|
||||
public <T extends Comparable<T>> T[] sort(T[] arr) {
|
||||
public <T extends Comparable<T>> 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;
|
||||
}
|
||||
}
|
||||
|
@ -25,13 +25,13 @@ public class HeapSort implements SortAlgorithm {
|
||||
return array;
|
||||
}
|
||||
|
||||
private static <T extends Comparable<T>> void heapify(T[] array, int n) {
|
||||
private <T extends Comparable<T>> void heapify(final T[] array, final int n) {
|
||||
for (int k = n / 2; k >= 1; k--) {
|
||||
siftDown(array, k, n);
|
||||
}
|
||||
}
|
||||
|
||||
private static <T extends Comparable<T>> void siftDown(T[] array, int k, int n) {
|
||||
private <T extends Comparable<T>> 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])) {
|
||||
|
@ -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 <T extends Comparable<T>> void doSort(T[] array, int left, int right) {
|
||||
private static <T extends Comparable<T>> 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 <T extends Comparable<T>> int randomPartition(T[] array, int left, int right) {
|
||||
int randomIndex = left + (int) (Math.random() * (right - left + 1));
|
||||
private static <T extends Comparable<T>> 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 <T extends Comparable<T>> 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)) {
|
||||
|
@ -27,7 +27,7 @@ public class SelectionSortRecursive implements SortAlgorithm {
|
||||
* @param index the current index to start sorting from
|
||||
* @param <T> the type of elements in the array (must be Comparable)
|
||||
*/
|
||||
private static <T extends Comparable<T>> void recursiveSelectionSort(T[] array, int index) {
|
||||
private static <T extends Comparable<T>> void recursiveSelectionSort(T[] array, final int index) {
|
||||
if (index == array.length - 1) {
|
||||
return;
|
||||
}
|
||||
@ -46,7 +46,7 @@ public class SelectionSortRecursive implements SortAlgorithm {
|
||||
* @param <T> the type of elements in the array
|
||||
* @return the index of the minimum element
|
||||
*/
|
||||
private static <T extends Comparable<T>> int findMinIndex(T[] array, int start) {
|
||||
private static <T extends Comparable<T>> int findMinIndex(T[] array, final int start) {
|
||||
// Base case: if start is the last index, return start
|
||||
if (start == array.length - 1) {
|
||||
return start;
|
||||
|
@ -15,20 +15,20 @@ public class StoogeSort implements SortAlgorithm {
|
||||
return array;
|
||||
}
|
||||
|
||||
public <T extends Comparable<T>> 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 extends Comparable<T>> 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;
|
||||
}
|
||||
}
|
||||
|
@ -13,14 +13,14 @@ public final class StrandSort implements SortAlgorithm {
|
||||
* Sorts the given array using the Strand Sort algorithm.
|
||||
*
|
||||
* @param <T> 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 extends Comparable<T>> T[] sort(T[] unsorted) {
|
||||
List<T> unsortedList = new ArrayList<>(Arrays.asList(unsorted));
|
||||
public <T extends Comparable<T>> T[] sort(T[] array) {
|
||||
List<T> unsortedList = new ArrayList<>(Arrays.asList(array));
|
||||
List<T> sortedList = strandSort(unsortedList);
|
||||
return sortedList.toArray(unsorted);
|
||||
return sortedList.toArray(array);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -11,11 +11,10 @@ public class SwapSort implements SortAlgorithm {
|
||||
|
||||
@Override
|
||||
public <T extends Comparable<T>> 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 <T extends Comparable<T>> int getSmallerElementCount(T[] array, int index) {
|
||||
private <T extends Comparable<T>> 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])) {
|
||||
|
@ -12,25 +12,25 @@ class TimSort implements SortAlgorithm {
|
||||
private Comparable[] aux;
|
||||
|
||||
@Override
|
||||
public <T extends Comparable<T>> T[] sort(T[] a) {
|
||||
int n = a.length;
|
||||
public <T extends Comparable<T>> 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 <T extends Comparable<T>> void merge(T[] a, int lo, int mid, int hi) {
|
||||
private <T extends Comparable<T>> 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);
|
||||
|
Loading…
Reference in New Issue
Block a user