Skip to content

refactor: simple improvements and cleanup for different sorts #5320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/main/java/com/thealgorithms/sorts/CircleSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,10 +49,10 @@ private <T extends Comparable<T>> Boolean doSort(T[] array, int left, int right)
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;
}
}
38 changes: 24 additions & 14 deletions src/main/java/com/thealgorithms/sorts/OddEvenSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

return array;
}

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;
}
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 sorted;
}

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 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 arr;
return sorted;
}
}
13 changes: 6 additions & 7 deletions src/main/java/com/thealgorithms/sorts/SelectionSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
40 changes: 2 additions & 38 deletions src/main/java/com/thealgorithms/sorts/SimpleSort.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/thealgorithms/sorts/StrandSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ private static <T extends Comparable<? super T>> List<T> strandSort(List<T> list
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++;
Expand Down
66 changes: 4 additions & 62 deletions src/test/java/com/thealgorithms/sorts/SimpleSortTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}