Skip to content

Commit

Permalink
Add Insertion Sorts to Run All
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed May 5, 2021
1 parent d0303c1 commit d831193
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 93 deletions.
59 changes: 31 additions & 28 deletions src/sorts/insert/GambitInsertionSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,42 @@ public GambitInsertionSort(ArrayVisualizer arrayVisualizer) {
setBogoSort(false);

}

private int binSearch(int[] array, int begin, int end, int target) {
while (true) {
int delta = end - begin;
if (delta <= 0)
break;
int p = begin + delta / 2;
if (this.Reads.compareIndices(array, p, target, 0.5D, true) == 0) return p;

if (this.Reads.compareIndices(array, p, target, 0.5D, true) > 0) { end = p; continue; }
begin = p + 1;
}
return end;
while (true) {
int delta = end - begin;
if (delta <= 0)
break;
int p = begin + delta / 2;
if (this.Reads.compareIndices(array, p, target, 0.5D, true) == 0)
return p;

if (this.Reads.compareIndices(array, p, target, 0.5D, true) > 0) {
end = p;
continue;
}
begin = p + 1;
}
return end;
}

private void binInsert(int[] array, int len, int start, int end) {
int offset = 1;
for (; offset * offset < len; offset *= 2);

for (int bStart = 0, bEnd = end, i = start + offset; i < end; i++) {
int target = binSearch(array, bStart, bEnd, i);

int tmp = array[i];
int j = i - 1;
while (j >= target && array[j] > tmp) {
this.Writes.write(array, j + 1, array[j], 0.125D, true, false);
j--;
}
array[j + 1] = tmp;
}
}
int offset = 1;
for (; offset * offset < len; offset *= 2)
;

for (int bStart = 0, bEnd = end, i = start + offset; i < end; i++) {
int target = binSearch(array, bStart, bEnd, i);

int tmp = array[i];
int j = i - 1;
while (j >= target && array[j] > tmp) {
this.Writes.write(array, j + 1, array[j], 0.125D, true, false);
j--;
}
array[j + 1] = tmp;
}
}

@Override
public void runSort(int[] array, int sortLength, int bucketCount) {
Expand Down
43 changes: 20 additions & 23 deletions src/sorts/insert/ReverseInsertionSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,31 @@ public final class ReverseInsertionSort extends Sort {
public ReverseInsertionSort(ArrayVisualizer arrayVisualizer) {
super(arrayVisualizer);
// TODO Auto-generated constructor stub
this.setSortListName("Reverse Insertion");
this.setRunAllSortsName("Reverse Insertion Sort");
this.setRunSortName("Reverse Insertsort");
this.setCategory("Insertion Sorts");
this.setComparisonBased(true);
this.setBucketSort(false);
this.setRadixSort(false);
this.setUnreasonablySlow(false);
this.setUnreasonableLimit(0);
this.setBogoSort(false);
this.setSortListName("Reverse Insertion");
this.setRunAllSortsName("Reverse Insertion Sort");
this.setRunSortName("Reverse Insertsort");
this.setCategory("Insertion Sorts");
this.setComparisonBased(true);
this.setBucketSort(false);
this.setRadixSort(false);
this.setUnreasonablySlow(false);
this.setUnreasonableLimit(0);
this.setBogoSort(false);
}

@Override
public void runSort(int[] array, int length, int bucketCount) throws Exception {
// TODO Auto-generated method stub
for (int i = length - 1; i >= 0; i--) {
int current = array[i];
int pos = i + 1;

while (pos <= length - 1 && Reads.compareValues(array[pos], current) < 0) {
Writes.write(array, pos - 1, array[pos], 0.015D, true, false);
pos++;
}
Writes.write(array, pos - 1, current, 0.015D, true, false);
}
}
for (int i = length - 1; i >= 0; i--) {
int current = array[i];
int pos = i + 1;



while (pos <= length - 1 && Reads.compareValues(array[pos], current) < 0) {
Writes.write(array, pos - 1, array[pos], 0.015D, true, false);
pos++;
}
Writes.write(array, pos - 1, current, 0.015D, true, false);
}
}

}
93 changes: 51 additions & 42 deletions src/threads/RunInsertionSorts.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,58 +54,67 @@ final public class RunInsertionSorts extends MultipleSortThread {
private Sort ShellSortParallel;
private Sort ShuffledTreeSort;
private Sort TreeSort;
private Sort AdaptiveBinaryInsertionSort;
private Sort GambitInsertionSort;
private Sort ReverseInsertionSort;

public RunInsertionSorts(ArrayVisualizer arrayVisualizer) {
super(arrayVisualizer);
this.sortCount = 21;
this.categoryCount = this.sortCount;

InsertionSort = new InsertionSort(this.arrayVisualizer);
DoubleInsertionSort = new DoubleInsertionSort(this.arrayVisualizer);
BinaryInsertionSort = new BinaryInsertionSort(this.arrayVisualizer);
TriSearchInsertionSort = new TriSearchInsertionSort(this.arrayVisualizer);
FibonacciInsertionSort = new FibonacciInsertionSort(this.arrayVisualizer);
UnstableInsertionSort = new UnstableInsertionSort(this.arrayVisualizer);
ShellSort = new ShellSort(this.arrayVisualizer);
RecursiveShellSort = new RecursiveShellSort(this.arrayVisualizer);
RendezvousSort = new RendezvousSort(this.arrayVisualizer);
RoomSort = new RoomSort(this.arrayVisualizer);
LibrarySort = new LibrarySort(this.arrayVisualizer);
PatienceSort = new PatienceSort(this.arrayVisualizer);
ClassicTreeSort = new ClassicTreeSort(this.arrayVisualizer);
AATreeSort = new AATreeSort(this.arrayVisualizer);
AVLTreeSort = new AVLTreeSort(this.arrayVisualizer);
SplaySort = new SplaySort(this.arrayVisualizer);
ClassicLibrarySort = new ClassicLibrarySort(this.arrayVisualizer);
CocktailShellSort = new CocktailShellSort(this.arrayVisualizer);
ShellSortParallel = new ShellSortParallel(this.arrayVisualizer);
ShuffledTreeSort = new ShuffledTreeSort(this.arrayVisualizer);
TreeSort = new TreeSort(this.arrayVisualizer);
InsertionSort = new InsertionSort(this.arrayVisualizer);
DoubleInsertionSort = new DoubleInsertionSort(this.arrayVisualizer);
BinaryInsertionSort = new BinaryInsertionSort(this.arrayVisualizer);
TriSearchInsertionSort = new TriSearchInsertionSort(this.arrayVisualizer);
FibonacciInsertionSort = new FibonacciInsertionSort(this.arrayVisualizer);
UnstableInsertionSort = new UnstableInsertionSort(this.arrayVisualizer);
ShellSort = new ShellSort(this.arrayVisualizer);
RecursiveShellSort = new RecursiveShellSort(this.arrayVisualizer);
RendezvousSort = new RendezvousSort(this.arrayVisualizer);
RoomSort = new RoomSort(this.arrayVisualizer);
LibrarySort = new LibrarySort(this.arrayVisualizer);
PatienceSort = new PatienceSort(this.arrayVisualizer);
ClassicTreeSort = new ClassicTreeSort(this.arrayVisualizer);
AATreeSort = new AATreeSort(this.arrayVisualizer);
AVLTreeSort = new AVLTreeSort(this.arrayVisualizer);
SplaySort = new SplaySort(this.arrayVisualizer);
ClassicLibrarySort = new ClassicLibrarySort(this.arrayVisualizer);
CocktailShellSort = new CocktailShellSort(this.arrayVisualizer);
ShellSortParallel = new ShellSortParallel(this.arrayVisualizer);
ShuffledTreeSort = new ShuffledTreeSort(this.arrayVisualizer);
TreeSort = new TreeSort(this.arrayVisualizer);
AdaptiveBinaryInsertionSort = new AdaptiveBinaryInsertionSort(this.arrayVisualizer);
GambitInsertionSort = new GambitInsertionSort(this.arrayVisualizer);
ReverseInsertionSort = new ReverseInsertionSort(this.arrayVisualizer);
}

@Override
protected synchronized void executeSortList(int[] array) throws Exception {
RunInsertionSorts.this.runIndividualSort(InsertionSort, 0, array, 128, 0.005, false);
RunInsertionSorts.this.runIndividualSort(DoubleInsertionSort, 0, array, 128, 0.002, false);
RunInsertionSorts.this.runIndividualSort(BinaryInsertionSort, 0, array, 128, 0.025, false);
RunInsertionSorts.this.runIndividualSort(TriSearchInsertionSort, 0, array, 128, 1, false);
RunInsertionSorts.this.runIndividualSort(FibonacciInsertionSort, 0, array, 128, 0.025, false);
RunInsertionSorts.this.runIndividualSort(UnstableInsertionSort, 0, array, 128, 0.2, false);
RunInsertionSorts.this.runIndividualSort(ShellSort, 0, array, 256, 0.1, false);
RunInsertionSorts.this.runIndividualSort(CocktailShellSort, 0, array, 256, 0.1, false);
RunInsertionSorts.this.runIndividualSort(RecursiveShellSort, 0, array, 256, 0.1, false);
RunInsertionSorts.this.runIndividualSort(ShellSortParallel, 0, array, 256, 0.1, false);
RunInsertionSorts.this.runIndividualSort(RendezvousSort, 0, array, 256, 0.1, false);
RunInsertionSorts.this.runIndividualSort(RoomSort, 0, array, 512, 0.05, false);
RunInsertionSorts.this.runIndividualSort(LibrarySort, 0, array, 2048, 1, false);
RunInsertionSorts.this.runIndividualSort(ClassicLibrarySort, 0, array, 2048, 1, false);
RunInsertionSorts.this.runIndividualSort(PatienceSort, 0, array, 2048, 1, false);
RunInsertionSorts.this.runIndividualSort(ClassicTreeSort, 0, array, 2048, arrayManager.getShuffle() == Shuffles.RANDOM ? 1 : 5, false);
RunInsertionSorts.this.runIndividualSort(TreeSort, 0, array, 2048, arrayManager.getShuffle() == Shuffles.RANDOM ? 1 : 5, false);
RunInsertionSorts.this.runIndividualSort(ShuffledTreeSort, 0, array, 2048, 1, false);
RunInsertionSorts.this.runIndividualSort(AATreeSort, 0, array, 2048, 1, false);
RunInsertionSorts.this.runIndividualSort(AVLTreeSort, 0, array, 2048, 1, false);
RunInsertionSorts.this.runIndividualSort(SplaySort, 0, array, 2048, 1, false);
RunInsertionSorts.this.runIndividualSort(InsertionSort, 0, array, 128, 0.005, false);
RunInsertionSorts.this.runIndividualSort(ReverseInsertionSort, 0, array, 128, 0.005, false);
RunInsertionSorts.this.runIndividualSort(DoubleInsertionSort, 0, array, 128, 0.002, false);
RunInsertionSorts.this.runIndividualSort(BinaryInsertionSort, 0, array, 128, 0.025, false);
RunInsertionSorts.this.runIndividualSort(AdaptiveBinaryInsertionSort, 0, array, 128, 0.025, false);
RunInsertionSorts.this.runIndividualSort(TriSearchInsertionSort, 0, array, 128, 1, false);
RunInsertionSorts.this.runIndividualSort(FibonacciInsertionSort, 0, array, 128, 0.025, false);
RunInsertionSorts.this.runIndividualSort(GambitInsertionSort, 0, array, 128, 0.025, false);
RunInsertionSorts.this.runIndividualSort(UnstableInsertionSort, 0, array, 128, 0.2, false);
RunInsertionSorts.this.runIndividualSort(ShellSort, 0, array, 256, 0.1, false);
RunInsertionSorts.this.runIndividualSort(CocktailShellSort, 0, array, 256, 0.1, false);
RunInsertionSorts.this.runIndividualSort(RecursiveShellSort, 0, array, 256, 0.1, false);
RunInsertionSorts.this.runIndividualSort(ShellSortParallel, 0, array, 256, 0.1, false);
RunInsertionSorts.this.runIndividualSort(RendezvousSort, 0, array, 256, 0.1, false);
RunInsertionSorts.this.runIndividualSort(RoomSort, 0, array, 512, 0.05, false);
RunInsertionSorts.this.runIndividualSort(LibrarySort, 0, array, 2048, 1, false);
RunInsertionSorts.this.runIndividualSort(ClassicLibrarySort, 0, array, 2048, 1, false);
RunInsertionSorts.this.runIndividualSort(PatienceSort, 0, array, 2048, 1, false);
RunInsertionSorts.this.runIndividualSort(ClassicTreeSort, 0, array, 2048, arrayManager.getShuffle() == Shuffles.RANDOM ? 1 : 5, false);
RunInsertionSorts.this.runIndividualSort(TreeSort, 0, array, 2048, arrayManager.getShuffle() == Shuffles.RANDOM ? 1 : 5, false);
RunInsertionSorts.this.runIndividualSort(ShuffledTreeSort, 0, array, 2048, 1, false);
RunInsertionSorts.this.runIndividualSort(AATreeSort, 0, array, 2048, 1, false);
RunInsertionSorts.this.runIndividualSort(AVLTreeSort, 0, array, 2048, 1, false);
RunInsertionSorts.this.runIndividualSort(SplaySort, 0, array, 2048, 1, false);
}

@Override
Expand Down

0 comments on commit d831193

Please sign in to comment.