refactor: simple improvements and cleanup for different sorts (#5320)

This commit is contained in:
Alex Klymenko 2024-08-13 18:26:48 +02:00 committed by GitHub
parent 2837585705
commit 41f76e0e89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 55 additions and 136 deletions

View File

@ -7,25 +7,29 @@ public class CircleSort implements SortAlgorithm {
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
int n = array.length;
if (n == 0) {
if (array.length == 0) {
return array;
}
while (doSort(array, 0, n - 1)) {
while (doSort(array, 0, array.length - 1)) {
}
return array;
}
/* This method implements the cyclic sort recursive version
/**
* Recursively sorts the array in a circular manner by comparing elements
* from the start and end of the current segment.
*
* @param <T> The type of elements in the array, which must be comparable
* @param array The array to be sorted
* @param the left boundary of the part currently being sorted
* @param the right boundary of the part currently being sorted
* @param left The left boundary of the current segment being sorted
* @param right The right boundary of the current segment being sorted
* @return true if any elements were swapped during the sort; false otherwise
*/
private <T extends Comparable<T>> Boolean doSort(T[] array, int left, int right) {
private <T extends Comparable<T>> boolean doSort(final T[] array, final int left, final int right) {
boolean swapped = false;
if (left == right) {
return Boolean.FALSE;
return false;
}
int low = left;
@ -45,10 +49,10 @@ public class CircleSort implements SortAlgorithm {
swapped = true;
}
int mid = left + (right - left) / 2;
Boolean leftHalf = doSort(array, left, mid);
Boolean rightHalf = doSort(array, mid + 1, right);
final int mid = left + (right - left) / 2;
final boolean leftHalfSwapped = doSort(array, left, mid);
final boolean rightHalfSwapped = doSort(array, mid + 1, right);
return swapped || leftHalf || rightHalf;
return swapped || leftHalfSwapped || rightHalfSwapped;
}
}

View File

@ -13,29 +13,39 @@ public final class OddEvenSort implements SortAlgorithm {
* Sorts the given array using the Odd-Even Sort algorithm.
*
* @param <T> the type of elements in the array, which must implement the Comparable interface
* @param arr 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[] arr) {
public <T extends Comparable<T>> T[] sort(T[] array) {
boolean sorted = false;
while (!sorted) {
sorted = true;
sorted = performOddSort(array);
sorted = performEvenSort(array) && sorted;
}
for (int i = 1; i < arr.length - 1; i += 2) {
if (arr[i].compareTo(arr[i + 1]) > 0) {
SortUtils.swap(arr, i, i + 1);
sorted = false;
}
}
return array;
}
for (int i = 0; i < arr.length - 1; i += 2) {
if (arr[i].compareTo(arr[i + 1]) > 0) {
SortUtils.swap(arr, i, i + 1);
sorted = false;
}
private <T extends Comparable<T>> boolean performOddSort(T[] array) {
boolean sorted = true;
for (int i = 1; i < array.length - 1; i += 2) {
if (array[i].compareTo(array[i + 1]) > 0) {
SortUtils.swap(array, i, i + 1);
sorted = false;
}
}
return arr;
return sorted;
}
private <T extends Comparable<T>> boolean performEvenSort(T[] array) {
boolean sorted = true;
for (int i = 0; i < array.length - 1; i += 2) {
if (array[i].compareTo(array[i + 1]) > 0) {
SortUtils.swap(array, i, i + 1);
sorted = false;
}
}
return sorted;
}
}

View File

@ -10,18 +10,17 @@ public class SelectionSort implements SortAlgorithm {
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
// One by one move the boundary of the unsorted subarray
for (int i = 0; i < array.length - 1; i++) {
// Swap the remaining minimum element with the current element
SortUtils.swap(array, i, findIndexOfMin(array, i));
for (int i = 0; i < array.length - 1; i++) {
final int minIndex = findIndexOfMin(array, i);
SortUtils.swap(array, i, minIndex);
}
return array;
}
private static <T extends Comparable<T>> int findIndexOfMin(T[] array, final int start) {
int minIndex = start;
for (int i = start + 1; i < array.length; i++) {
private static <T extends Comparable<T>> int findIndexOfMin(T[] array, final int startIndex) {
int minIndex = startIndex;
for (int i = startIndex + 1; i < array.length; i++) {
if (array[i].compareTo(array[minIndex]) < 0) {
minIndex = i;
}

View File

@ -1,51 +1,15 @@
package com.thealgorithms.sorts;
public class SimpleSort implements SortAlgorithm {
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
final int length = array.length;
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
if (SortUtils.less(array[j], array[i])) {
SortUtils.swap(array, i, j);
}
}
}
return array;
}
public static void main(String[] args) {
// ==== Int =======
Integer[] a = {3, 7, 45, 1, 33, 5, 2, 9};
System.out.print("unsorted: ");
SortUtils.print(a);
System.out.println();
new SimpleSort().sort(a);
System.out.print("sorted: ");
SortUtils.print(a);
System.out.println();
// ==== String =======
String[] b = {
"banana",
"berry",
"orange",
"grape",
"peach",
"cherry",
"apple",
"pineapple",
};
System.out.print("unsorted: ");
SortUtils.print(b);
System.out.println();
new SimpleSort().sort(b);
System.out.print("sorted: ");
SortUtils.print(b);
}
}

View File

@ -38,9 +38,9 @@ public final class StrandSort implements SortAlgorithm {
List<T> result = new ArrayList<>();
while (!list.isEmpty()) {
final List<T> sorted = new ArrayList<>();
sorted.add(list.remove(0));
sorted.add(list.removeFirst());
for (int i = 0; i < list.size();) {
if (sorted.get(sorted.size() - 1).compareTo(list.get(i)) <= 0) {
if (sorted.getLast().compareTo(list.get(i)) <= 0) {
sorted.add(list.remove(i));
} else {
i++;

View File

@ -1,66 +1,8 @@
package com.thealgorithms.sorts;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
public class SimpleSortTest {
private SimpleSort simpleSort = new SimpleSort();
@Test
public void simpleSortEmptyArray() {
Integer[] inputArray = {};
Integer[] outputArray = simpleSort.sort(inputArray);
Integer[] expectedOutput = {};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void simpleSortSingleIntegerArray() {
Integer[] inputArray = {4};
Integer[] outputArray = simpleSort.sort(inputArray);
Integer[] expectedOutput = {4};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void simpleSortSingleStringArray() {
String[] inputArray = {"s"};
String[] outputArray = simpleSort.sort(inputArray);
String[] expectedOutput = {"s"};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void simpleSortNonDuplicateIntegerArray() {
Integer[] inputArray = {6, -1, 99, 27, -15, 23, -36};
Integer[] outputArray = simpleSort.sort(inputArray);
Integer[] expectedOutput = {-36, -15, -1, 6, 23, 27, 99};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void simpleSortDuplicateIntegerArray() {
Integer[] inputArray = {6, -1, 27, -15, 23, 27, -36, 23};
Integer[] outputArray = simpleSort.sort(inputArray);
Integer[] expectedOutput = {-36, -15, -1, 6, 23, 23, 27, 27};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void simpleSortNonDuplicateStringArray() {
String[] inputArray = {"s", "b", "k", "a", "d", "c", "h"};
String[] outputArray = simpleSort.sort(inputArray);
String[] expectedOutput = {"a", "b", "c", "d", "h", "k", "s"};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void simpleSortDuplicateStringArray() {
String[] inputArray = {"s", "b", "d", "a", "d", "c", "h", "b"};
String[] outputArray = simpleSort.sort(inputArray);
String[] expectedOutput = {"a", "b", "b", "c", "d", "d", "h", "s"};
assertArrayEquals(outputArray, expectedOutput);
public class SimpleSortTest extends SortingAlgorithmTest {
@Override
SortAlgorithm getSortAlgorithm() {
return new SimpleSort();
}
}