Skip to content

Commit b0172d4

Browse files
committed
Add TwinSort to Run All (and improve visualization of NaturalMergeSort)
1 parent bf7fe3e commit b0172d4

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

src/sorts/merge/NaturalMergeSort.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import sorts.templates.Sort;
55

66
final public class NaturalMergeSort extends Sort {
7+
int[] merged;
8+
79
public NaturalMergeSort(ArrayVisualizer arrayVisualizer) {
810
super(arrayVisualizer);
911
this.setSortListName("Natural Merge");
@@ -19,7 +21,6 @@ public NaturalMergeSort(ArrayVisualizer arrayVisualizer) {
1921
}
2022

2123
private void merge(int[] arr, int left, int right, int stop) {
22-
int[] merged = Writes.createExternalArray(stop - left);
2324
boolean first = true;
2425
int index = 0;
2526
int start = 0;
@@ -41,13 +42,11 @@ private void merge(int[] arr, int left, int right, int stop) {
4142
Highlights.clearMark(2);
4243
for (int i = start; i < index; i++)
4344
Writes.write(arr, i + origLeft, merged[i], 1, true, false);
44-
for (int i = start; i < index; i++)
45-
merged[i] = 0;
46-
Writes.deleteExternalArray(merged);
4745
}
4846

4947
@Override
5048
public void runSort(int[] arr, int length, int bucketCount) {
49+
merged = Writes.createExternalArray(length);
5150
boolean done = false;
5251
int start = 0;
5352
int stop = length - 1;
@@ -75,5 +74,6 @@ public void runSort(int[] arr, int length, int bucketCount) {
7574
stop = left;
7675
}
7776
}
77+
Writes.deleteExternalArray(merged);
7878
}
7979
}

src/threads/RunMergeSorts.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ of this software and associated documentation files (the "Software"), to deal
3434
final public class RunMergeSorts extends MultipleSortThread {
3535
private Sort MergeSort;
3636
private Sort BottomUpMergeSort;
37-
private Sort NaturalMergeSort;
3837
private Sort PartialMergeSort;
38+
private Sort TwinSort;
39+
private Sort NaturalMergeSort;
3940
private Sort PDMergeSort;
4041
private Sort InPlaceMergeSort;
4142
private Sort ImprovedInPlaceMergeSort;
@@ -50,13 +51,14 @@ final public class RunMergeSorts extends MultipleSortThread {
5051

5152
public RunMergeSorts(ArrayVisualizer arrayVisualizer) {
5253
super(arrayVisualizer);
53-
this.sortCount = 15;
54+
this.sortCount = 16;
5455
this.categoryCount = this.sortCount;
5556

5657
MergeSort = new MergeSort(this.arrayVisualizer);
5758
BottomUpMergeSort = new BottomUpMergeSort(this.arrayVisualizer);
58-
NaturalMergeSort = new NaturalMergeSort(this.arrayVisualizer);
5959
PartialMergeSort = new PartialMergeSort(this.arrayVisualizer);
60+
TwinSort = new TwinSort(this.arrayVisualizer);
61+
NaturalMergeSort = new NaturalMergeSort(this.arrayVisualizer);
6062
PDMergeSort = new PDMergeSort(this.arrayVisualizer);
6163
InPlaceMergeSort = new InPlaceMergeSort(this.arrayVisualizer);
6264
ImprovedInPlaceMergeSort = new ImprovedInPlaceMergeSort(this.arrayVisualizer);
@@ -74,8 +76,9 @@ public RunMergeSorts(ArrayVisualizer arrayVisualizer) {
7476
protected synchronized void executeSortList(int[] array) throws Exception {
7577
RunMergeSorts.this.runIndividualSort(MergeSort, 0, array, 2048, 1.5, false);
7678
RunMergeSorts.this.runIndividualSort(BottomUpMergeSort, 0, array, 2048, 1.5, false);
77-
RunMergeSorts.this.runIndividualSort(NaturalMergeSort, 0, array, 2048, 1.5, false);
7879
RunMergeSorts.this.runIndividualSort(PartialMergeSort, 0, array, 2048, 1.5, false);
80+
RunMergeSorts.this.runIndividualSort(TwinSort, 0, array, 2048, 1.5, false);
81+
RunMergeSorts.this.runIndividualSort(NaturalMergeSort, 0, array, 2048, 1.5, false);
7982
RunMergeSorts.this.runIndividualSort(PDMergeSort, 0, array, 2048, 1, false);
8083
RunMergeSorts.this.runIndividualSort(InPlaceMergeSort, 0, array, 2048, 1.5, false);
8184
RunMergeSorts.this.runIndividualSort(ImprovedInPlaceMergeSort, 0, array, 2048, 1.5, false);

0 commit comments

Comments
 (0)