mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 09:45:04 +08:00
refactor: simple improvements and cleanup for different sorts (#5318)
This commit is contained in:
@ -12,31 +12,31 @@ package com.thealgorithms.sorts;
|
|||||||
public class DutchNationalFlagSort implements SortAlgorithm {
|
public class DutchNationalFlagSort implements SortAlgorithm {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
|
public <T extends Comparable<T>> T[] sort(T[] array) {
|
||||||
return dutchNationalFlagSort(unsorted, unsorted[(int) Math.ceil((unsorted.length) / 2.0) - 1]);
|
return dutchNationalFlagSort(array, array[(int) Math.ceil((array.length) / 2.0) - 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends Comparable<T>> T[] sort(T[] unsorted, T intendedMiddle) {
|
public <T extends Comparable<T>> T[] sort(T[] array, T intendedMiddle) {
|
||||||
return dutchNationalFlagSort(unsorted, 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 i = 0;
|
||||||
int j = 0;
|
int j = 0;
|
||||||
int k = arr.length - 1;
|
int k = array.length - 1;
|
||||||
|
|
||||||
while (j <= k) {
|
while (j <= k) {
|
||||||
if (0 > arr[j].compareTo(intendedMiddle)) {
|
if (0 > array[j].compareTo(intendedMiddle)) {
|
||||||
SortUtils.swap(arr, i, j);
|
SortUtils.swap(array, i, j);
|
||||||
j++;
|
j++;
|
||||||
i++;
|
i++;
|
||||||
} else if (0 < arr[j].compareTo(intendedMiddle)) {
|
} else if (0 < array[j].compareTo(intendedMiddle)) {
|
||||||
SortUtils.swap(arr, j, k);
|
SortUtils.swap(array, j, k);
|
||||||
k--;
|
k--;
|
||||||
} else {
|
} else {
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return arr;
|
return array;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,63 +9,20 @@ package com.thealgorithms.sorts;
|
|||||||
public class GnomeSort implements SortAlgorithm {
|
public class GnomeSort implements SortAlgorithm {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Comparable<T>> T[] sort(T[] arr) {
|
public <T extends Comparable<T>> T[] sort(final T[] array) {
|
||||||
int i = 1;
|
int i = 1;
|
||||||
int j = 2;
|
int j = 2;
|
||||||
while (i < arr.length) {
|
while (i < array.length) {
|
||||||
if (SortUtils.less(arr[i - 1], arr[i])) {
|
if (SortUtils.less(array[i - 1], array[i])) {
|
||||||
i = j++;
|
i = j++;
|
||||||
} else {
|
} else {
|
||||||
SortUtils.swap(arr, i - 1, i);
|
SortUtils.swap(array, i - 1, i);
|
||||||
if (--i == 0) {
|
if (--i == 0) {
|
||||||
i = j++;
|
i = j++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return array;
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,13 +25,13 @@ public class HeapSort implements SortAlgorithm {
|
|||||||
return array;
|
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--) {
|
for (int k = n / 2; k >= 1; k--) {
|
||||||
siftDown(array, k, n);
|
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) {
|
while (2 * k <= n) {
|
||||||
int j = 2 * k;
|
int j = 2 * k;
|
||||||
if (j < n && SortUtils.less(array[j - 1], array[j])) {
|
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 right The last index of an array
|
||||||
* @param array The array to be sorted
|
* @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) {
|
if (left < right) {
|
||||||
int pivot = randomPartition(array, left, right);
|
final int pivot = randomPartition(array, left, right);
|
||||||
doSort(array, left, pivot - 1);
|
doSort(array, left, pivot - 1);
|
||||||
doSort(array, pivot, right);
|
doSort(array, pivot, right);
|
||||||
}
|
}
|
||||||
@ -41,8 +41,8 @@ class QuickSort implements SortAlgorithm {
|
|||||||
* @param right The last index of an array
|
* @param right The last index of an array
|
||||||
* @return the partition index of the array
|
* @return the partition index of the array
|
||||||
*/
|
*/
|
||||||
private static <T extends Comparable<T>> int randomPartition(T[] array, int left, int right) {
|
private static <T extends Comparable<T>> int randomPartition(T[] array, final int left, final int right) {
|
||||||
int randomIndex = left + (int) (Math.random() * (right - left + 1));
|
final int randomIndex = left + (int) (Math.random() * (right - left + 1));
|
||||||
SortUtils.swap(array, randomIndex, right);
|
SortUtils.swap(array, randomIndex, right);
|
||||||
return partition(array, left, right);
|
return partition(array, left, right);
|
||||||
}
|
}
|
||||||
@ -56,8 +56,8 @@ class QuickSort implements SortAlgorithm {
|
|||||||
* array
|
* array
|
||||||
*/
|
*/
|
||||||
private static <T extends Comparable<T>> int partition(T[] array, int left, int right) {
|
private static <T extends Comparable<T>> int partition(T[] array, int left, int right) {
|
||||||
int mid = (left + right) >>> 1;
|
final int mid = (left + right) >>> 1;
|
||||||
T pivot = array[mid];
|
final T pivot = array[mid];
|
||||||
|
|
||||||
while (left <= right) {
|
while (left <= right) {
|
||||||
while (SortUtils.less(array[left], pivot)) {
|
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 index the current index to start sorting from
|
||||||
* @param <T> the type of elements in the array (must be Comparable)
|
* @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) {
|
if (index == array.length - 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -46,7 +46,7 @@ public class SelectionSortRecursive implements SortAlgorithm {
|
|||||||
* @param <T> the type of elements in the array
|
* @param <T> the type of elements in the array
|
||||||
* @return the index of the minimum element
|
* @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
|
// Base case: if start is the last index, return start
|
||||||
if (start == array.length - 1) {
|
if (start == array.length - 1) {
|
||||||
return start;
|
return start;
|
||||||
|
@ -15,20 +15,20 @@ public class StoogeSort implements SortAlgorithm {
|
|||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends Comparable<T>> T[] sort(T[] unsortedArray, int start, int end) {
|
public <T extends Comparable<T>> T[] sort(final T[] array, final int start, final int end) {
|
||||||
if (SortUtils.less(unsortedArray[end - 1], unsortedArray[start])) {
|
if (SortUtils.less(array[end - 1], array[start])) {
|
||||||
T temp = unsortedArray[start];
|
final T temp = array[start];
|
||||||
unsortedArray[start] = unsortedArray[end - 1];
|
array[start] = array[end - 1];
|
||||||
unsortedArray[end - 1] = temp;
|
array[end - 1] = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
int len = end - start;
|
final int length = end - start;
|
||||||
if (len > 2) {
|
if (length > 2) {
|
||||||
int third = len / 3;
|
int third = length / 3;
|
||||||
sort(unsortedArray, start, end - third);
|
sort(array, start, end - third);
|
||||||
sort(unsortedArray, start + third, end);
|
sort(array, start + third, end);
|
||||||
sort(unsortedArray, start, end - third);
|
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.
|
* Sorts the given array using the Strand Sort algorithm.
|
||||||
*
|
*
|
||||||
* @param <T> The type of elements to be sorted, must be Comparable.
|
* @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.
|
* @return The sorted array.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
|
public <T extends Comparable<T>> T[] sort(T[] array) {
|
||||||
List<T> unsortedList = new ArrayList<>(Arrays.asList(unsorted));
|
List<T> unsortedList = new ArrayList<>(Arrays.asList(array));
|
||||||
List<T> sortedList = strandSort(unsortedList);
|
List<T> sortedList = strandSort(unsortedList);
|
||||||
return sortedList.toArray(unsorted);
|
return sortedList.toArray(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -11,11 +11,10 @@ public class SwapSort implements SortAlgorithm {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Comparable<T>> T[] sort(T[] array) {
|
public <T extends Comparable<T>> T[] sort(T[] array) {
|
||||||
int len = array.length;
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
|
||||||
while (index < len - 1) {
|
while (index < array.length - 1) {
|
||||||
int amountSmallerElements = this.getSmallerElementCount(array, index);
|
final int amountSmallerElements = this.getSmallerElementCount(array, index);
|
||||||
|
|
||||||
if (amountSmallerElements > 0) {
|
if (amountSmallerElements > 0) {
|
||||||
SortUtils.swap(array, index, index + amountSmallerElements);
|
SortUtils.swap(array, index, index + amountSmallerElements);
|
||||||
@ -27,7 +26,7 @@ public class SwapSort implements SortAlgorithm {
|
|||||||
return array;
|
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;
|
int counter = 0;
|
||||||
for (int i = index + 1; i < array.length; i++) {
|
for (int i = index + 1; i < array.length; i++) {
|
||||||
if (SortUtils.less(array[i], array[index])) {
|
if (SortUtils.less(array[i], array[index])) {
|
||||||
|
@ -12,25 +12,25 @@ class TimSort implements SortAlgorithm {
|
|||||||
private Comparable[] aux;
|
private Comparable[] aux;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Comparable<T>> T[] sort(T[] a) {
|
public <T extends Comparable<T>> T[] sort(T[] array) {
|
||||||
int n = a.length;
|
final int n = array.length;
|
||||||
|
|
||||||
InsertionSort insertionSort = new InsertionSort();
|
InsertionSort insertionSort = new InsertionSort();
|
||||||
for (int i = 0; i < n; i += SUB_ARRAY_SIZE) {
|
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];
|
aux = new Comparable[n];
|
||||||
for (int sz = SUB_ARRAY_SIZE; sz < n; sz = sz + sz) {
|
for (int sz = SUB_ARRAY_SIZE; sz < n; sz = sz + sz) {
|
||||||
for (int lo = 0; lo < n - sz; lo += 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 i = lo;
|
||||||
int j = mid + 1;
|
int j = mid + 1;
|
||||||
System.arraycopy(a, lo, aux, lo, hi + 1 - lo);
|
System.arraycopy(a, lo, aux, lo, hi + 1 - lo);
|
||||||
|
Reference in New Issue
Block a user