diff --git a/src/main/java/com/thealgorithms/sorts/StrandSort.java b/src/main/java/com/thealgorithms/sorts/StrandSort.java index 51600812bbb1..58cd35628506 100644 --- a/src/main/java/com/thealgorithms/sorts/StrandSort.java +++ b/src/main/java/com/thealgorithms/sorts/StrandSort.java @@ -1,46 +1,79 @@ package com.thealgorithms.sorts; -import java.util.Iterator; -import java.util.LinkedList; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; -public final class StrandSort { - private StrandSort() { +/** + * StrandSort class implementing the SortAlgorithm interface using arrays. + */ +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. + * @return The sorted array. + */ + @Override + public > T[] sort(T[] unsorted) { + List unsortedList = new ArrayList<>(Arrays.asList(unsorted)); + List sortedList = strandSort(unsortedList); + return sortedList.toArray(unsorted); } - // note: the input list is destroyed - public static > LinkedList strandSort(LinkedList list) { + /** + * Strand Sort algorithm that sorts a list. + * + * @param The type of elements to be sorted, must be Comparable. + * @param list The list to be sorted. + * @return The sorted list. + */ + private static > List strandSort(List list) { if (list.size() <= 1) { return list; } - LinkedList result = new LinkedList(); - while (list.size() > 0) { - LinkedList sorted = new LinkedList(); - sorted.add(list.removeFirst()); // same as remove() or remove(0) - for (Iterator it = list.iterator(); it.hasNext();) { - E elem = it.next(); - if (sorted.peekLast().compareTo(elem) <= 0) { - sorted.addLast(elem); // same as add(elem) or add(0, elem) - it.remove(); + List result = new ArrayList<>(); + while (!list.isEmpty()) { + final List sorted = new ArrayList<>(); + sorted.add(list.remove(0)); + for (int i = 0; i < list.size();) { + if (sorted.get(sorted.size() - 1).compareTo(list.get(i)) <= 0) { + sorted.add(list.remove(i)); + } else { + i++; } } - result = merge(sorted, result); + result = merge(result, sorted); } return result; } - private static > LinkedList merge(LinkedList left, LinkedList right) { - LinkedList result = new LinkedList(); - while (!left.isEmpty() && !right.isEmpty()) { - // change the direction of this comparison to change the direction of the sort - if (left.peek().compareTo(right.peek()) <= 0) { - result.add(left.remove()); + /** + * Merges two sorted lists into one sorted list. + * + * @param The type of elements to be sorted, must be Comparable. + * @param left The first sorted list. + * @param right The second sorted list. + * @return The merged sorted list. + */ + private static > List merge(List left, List right) { + List result = new ArrayList<>(); + int i = 0; + int j = 0; + while (i < left.size() && j < right.size()) { + if (left.get(i).compareTo(right.get(j)) <= 0) { + result.add(left.get(i)); + i++; } else { - result.add(right.remove()); + result.add(right.get(j)); + j++; } } - result.addAll(left); - result.addAll(right); + result.addAll(left.subList(i, left.size())); + result.addAll(right.subList(j, right.size())); return result; } } diff --git a/src/test/java/com/thealgorithms/sorts/StrandSortTest.java b/src/test/java/com/thealgorithms/sorts/StrandSortTest.java index 91e85c81e5ec..a7250acf9bec 100644 --- a/src/test/java/com/thealgorithms/sorts/StrandSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/StrandSortTest.java @@ -1,34 +1,8 @@ package com.thealgorithms.sorts; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; - -import java.util.Arrays; -import java.util.LinkedList; -import org.junit.jupiter.api.Test; - -class StrandSortTest { - - @Test - // valid test case - public void strandSortNonDuplicateTest() { - int[] expectedArray = {1, 2, 3, 4, 5}; - LinkedList actualList = StrandSort.strandSort(new LinkedList(Arrays.asList(3, 1, 2, 4, 5))); - int[] actualArray = new int[actualList.size()]; - for (int i = 0; i < actualList.size(); i++) { - actualArray[i] = actualList.get(i); - } - assertArrayEquals(expectedArray, actualArray); - } - - @Test - // valid test case - public void strandSortDuplicateTest() { - int[] expectedArray = {2, 2, 2, 5, 7}; - LinkedList actualList = StrandSort.strandSort(new LinkedList(Arrays.asList(7, 2, 2, 2, 5))); - int[] actualArray = new int[actualList.size()]; - for (int i = 0; i < actualList.size(); i++) { - actualArray[i] = actualList.get(i); - } - assertArrayEquals(expectedArray, actualArray); +class StrandSortTest extends SortingAlgorithmTest { + @Override + SortAlgorithm getSortAlgorithm() { + return new StrandSort(); } }