Skip to content

Commit 326537a

Browse files
authored
Merge branch 'master' into fix/swapsort-duplicates
2 parents 11c1726 + 12f69b3 commit 326537a

2 files changed

Lines changed: 38 additions & 24 deletions

File tree

‎src/main/java/com/thealgorithms/sorts/TreeSort.java‎

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static com.thealgorithms.sorts.SortUtils.print;
44

55
import com.thealgorithms.datastructures.trees.BSTRecursiveGeneric;
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
68
import java.util.List;
79

810
/**
@@ -30,38 +32,39 @@ public <T extends Comparable<T>> List<T> sort(List<T> unsortedList) {
3032
}
3133

3234
private <T extends Comparable<T>> T[] doTreeSortArray(T[] unsortedArray) {
33-
// create a generic BST tree
34-
BSTRecursiveGeneric<T> tree = new BSTRecursiveGeneric<T>();
35-
36-
// add all elements to the tree
37-
for (T element : unsortedArray) {
38-
tree.add(element);
39-
}
40-
41-
// get the sorted list by inorder traversal of the tree
42-
List<T> sortedList = tree.inorderSort();
43-
44-
// add the elements back to the initial array
45-
int i = 0;
46-
for (T element : sortedList) {
47-
unsortedArray[i++] = element;
48-
}
49-
50-
// return the array
51-
return unsortedArray;
35+
// sort through the list path and copy the result back into the initial array
36+
List<T> sortedList = doTreeSortList(Arrays.asList(unsortedArray));
37+
return sortedList.toArray(unsortedArray);
5238
}
5339

5440
private <T extends Comparable<T>> List<T> doTreeSortList(Iterable<T> unsortedList) {
55-
// create a generic BST tree
56-
BSTRecursiveGeneric<T> tree = new BSTRecursiveGeneric<T>();
41+
// the BST ignores duplicate keys, so pair every element with its position to keep equal values apart
42+
BSTRecursiveGeneric<Keyed<T>> tree = new BSTRecursiveGeneric<>();
5743

5844
// add all elements to the tree
45+
int position = 0;
5946
for (T element : unsortedList) {
60-
tree.add(element);
47+
tree.add(new Keyed<>(element, position++));
6148
}
6249

6350
// get the sorted list by inorder traversal of the tree and return it
64-
return tree.inorderSort();
51+
List<T> sortedList = new ArrayList<>();
52+
for (Keyed<T> keyed : tree.inorderSort()) {
53+
sortedList.add(keyed.value());
54+
}
55+
return sortedList;
56+
}
57+
58+
/**
59+
* An element plus its original position. Equal elements are ordered by position,
60+
* which also makes the sort stable.
61+
*/
62+
private record Keyed<T extends Comparable<T>>(T value, int position) implements Comparable<Keyed<T>> {
63+
@Override
64+
public int compareTo(Keyed<T> other) {
65+
int byValue = value.compareTo(other.value);
66+
return byValue != 0 ? byValue : Integer.compare(position, other.position);
67+
}
6568
}
6669

6770
public static void main(String[] args) {

‎src/test/java/com/thealgorithms/sorts/TreeSortTest.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,20 @@
99
* @see TreeSort
1010
*/
1111

12-
public class TreeSortTest {
12+
public class TreeSortTest extends SortingAlgorithmTest {
1313
private TreeSort treeSort = new TreeSort();
1414

15+
@Override
16+
SortAlgorithm getSortAlgorithm() {
17+
return new TreeSort();
18+
}
19+
20+
@Test
21+
public void treeSortKeepsDuplicates() {
22+
Integer[] array = {-1, -1, 0};
23+
assertArrayEquals(new Integer[] {-1, -1, 0}, treeSort.sort(array));
24+
}
25+
1526
@Test
1627
public void treeSortEmptyArray() {
1728
Integer[] inputArray = {};

0 commit comments

Comments
 (0)