Skip to content

Commit 15d2e70

Browse files
alxkmalx
and
alx
authored
Refactoring and code improving for StrandSort (#5243)
* Refactoring and code improving for StrandSort * Fix java checkstyle * Fix "Each variable declaration must be in its own statement" * Fix "uses integer based for loops to iterate over a List" --------- Co-authored-by: alx <[email protected]>
1 parent 91101ec commit 15d2e70

File tree

2 files changed

+62
-55
lines changed

2 files changed

+62
-55
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,79 @@
11
package com.thealgorithms.sorts;
22

3-
import java.util.Iterator;
4-
import java.util.LinkedList;
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
56

6-
public final class StrandSort {
7-
private StrandSort() {
7+
/**
8+
* StrandSort class implementing the SortAlgorithm interface using arrays.
9+
*/
10+
public final class StrandSort implements SortAlgorithm {
11+
12+
/**
13+
* Sorts the given array using the Strand Sort algorithm.
14+
*
15+
* @param <T> The type of elements to be sorted, must be Comparable.
16+
* @param unsorted The array to be sorted.
17+
* @return The sorted array.
18+
*/
19+
@Override
20+
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
21+
List<T> unsortedList = new ArrayList<>(Arrays.asList(unsorted));
22+
List<T> sortedList = strandSort(unsortedList);
23+
return sortedList.toArray(unsorted);
824
}
925

10-
// note: the input list is destroyed
11-
public static <E extends Comparable<? super E>> LinkedList<E> strandSort(LinkedList<E> list) {
26+
/**
27+
* Strand Sort algorithm that sorts a list.
28+
*
29+
* @param <T> The type of elements to be sorted, must be Comparable.
30+
* @param list The list to be sorted.
31+
* @return The sorted list.
32+
*/
33+
private static <T extends Comparable<? super T>> List<T> strandSort(List<T> list) {
1234
if (list.size() <= 1) {
1335
return list;
1436
}
1537

16-
LinkedList<E> result = new LinkedList<E>();
17-
while (list.size() > 0) {
18-
LinkedList<E> sorted = new LinkedList<E>();
19-
sorted.add(list.removeFirst()); // same as remove() or remove(0)
20-
for (Iterator<E> it = list.iterator(); it.hasNext();) {
21-
E elem = it.next();
22-
if (sorted.peekLast().compareTo(elem) <= 0) {
23-
sorted.addLast(elem); // same as add(elem) or add(0, elem)
24-
it.remove();
38+
List<T> result = new ArrayList<>();
39+
while (!list.isEmpty()) {
40+
final List<T> sorted = new ArrayList<>();
41+
sorted.add(list.remove(0));
42+
for (int i = 0; i < list.size();) {
43+
if (sorted.get(sorted.size() - 1).compareTo(list.get(i)) <= 0) {
44+
sorted.add(list.remove(i));
45+
} else {
46+
i++;
2547
}
2648
}
27-
result = merge(sorted, result);
49+
result = merge(result, sorted);
2850
}
2951
return result;
3052
}
3153

32-
private static <E extends Comparable<? super E>> LinkedList<E> merge(LinkedList<E> left, LinkedList<E> right) {
33-
LinkedList<E> result = new LinkedList<E>();
34-
while (!left.isEmpty() && !right.isEmpty()) {
35-
// change the direction of this comparison to change the direction of the sort
36-
if (left.peek().compareTo(right.peek()) <= 0) {
37-
result.add(left.remove());
54+
/**
55+
* Merges two sorted lists into one sorted list.
56+
*
57+
* @param <T> The type of elements to be sorted, must be Comparable.
58+
* @param left The first sorted list.
59+
* @param right The second sorted list.
60+
* @return The merged sorted list.
61+
*/
62+
private static <T extends Comparable<? super T>> List<T> merge(List<T> left, List<T> right) {
63+
List<T> result = new ArrayList<>();
64+
int i = 0;
65+
int j = 0;
66+
while (i < left.size() && j < right.size()) {
67+
if (left.get(i).compareTo(right.get(j)) <= 0) {
68+
result.add(left.get(i));
69+
i++;
3870
} else {
39-
result.add(right.remove());
71+
result.add(right.get(j));
72+
j++;
4073
}
4174
}
42-
result.addAll(left);
43-
result.addAll(right);
75+
result.addAll(left.subList(i, left.size()));
76+
result.addAll(right.subList(j, right.size()));
4477
return result;
4578
}
4679
}
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,8 @@
11
package com.thealgorithms.sorts;
22

3-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4-
5-
import java.util.Arrays;
6-
import java.util.LinkedList;
7-
import org.junit.jupiter.api.Test;
8-
9-
class StrandSortTest {
10-
11-
@Test
12-
// valid test case
13-
public void strandSortNonDuplicateTest() {
14-
int[] expectedArray = {1, 2, 3, 4, 5};
15-
LinkedList<Integer> actualList = StrandSort.strandSort(new LinkedList<Integer>(Arrays.asList(3, 1, 2, 4, 5)));
16-
int[] actualArray = new int[actualList.size()];
17-
for (int i = 0; i < actualList.size(); i++) {
18-
actualArray[i] = actualList.get(i);
19-
}
20-
assertArrayEquals(expectedArray, actualArray);
21-
}
22-
23-
@Test
24-
// valid test case
25-
public void strandSortDuplicateTest() {
26-
int[] expectedArray = {2, 2, 2, 5, 7};
27-
LinkedList<Integer> actualList = StrandSort.strandSort(new LinkedList<Integer>(Arrays.asList(7, 2, 2, 2, 5)));
28-
int[] actualArray = new int[actualList.size()];
29-
for (int i = 0; i < actualList.size(); i++) {
30-
actualArray[i] = actualList.get(i);
31-
}
32-
assertArrayEquals(expectedArray, actualArray);
3+
class StrandSortTest extends SortingAlgorithmTest {
4+
@Override
5+
SortAlgorithm getSortAlgorithm() {
6+
return new StrandSort();
337
}
348
}

0 commit comments

Comments
 (0)