Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A modification of Bitonic Sort made for almost/already sorted arrays and backwards arrays #3

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions src/sorts/ApollyonSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
///ApollyonSort is a modification of BitonicSort that replaces one operation with CircleSort, and then once the array is almost sorted to an extent, it uses an InsertionSort with a LinearSearch
///It is usually just as fast as BitonicSort, if not 1 ms slower. It is made for real world data such as almost sorted arrays and backwards arrays. On these types of arrays ApollyonSort outperforms BitonicSort
///Features: In-Place, Parallel, Recursive (can be modified to iterative), Unstable, Uses comparisons.
package sorts;

import templates.Sort;
import templates.CircleSorting;
import utils.Delays;
import utils.Highlights;
import utils.Reads;
import utils.Writes;

/*
* This version of Bitonic Sort was taken from here, written by H.W. Lang:
* http://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/bitonic/oddn.htm
*/

final public class ApollyonSort extends CircleSorting {
private boolean direction = true;

public ApollyonSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
super(delayOps, markOps, readOps, writeOps);

this.setSortPromptID("Apollyon");
this.setRunAllID("Apollyon's Bitonic Sort");
this.setReportSortID("Apollyon's Bitonic Sort");
this.setCategory("Hybrid Sorts");
this.isComparisonBased(true);
this.isBucketSort(false);
this.isRadixSort(false);
this.isUnreasonablySlow(false);
this.setUnreasonableLimit(0);
this.isBogoSort(false);
}

private static int greatestPowerOfTwoLessThan(int n){
int k = 1;
while (k < n) {
k = k << 1;
}
return k >> 1;
}

private void compare(int[] A, int i, int j, boolean dir)
{
Highlights.markArray(1, i);
Highlights.markArray(2, j);

Delays.sleep(0.5);

int cmp = Reads.compare(A[i], A[j]);

if (dir == (cmp == 1)) Writes.swap(A, i, j, 1, true, false);
}

private void bitonicMerge(int[] A, int lo, int n, boolean dir)
{
if (n > 1)
{
int m = ApollyonSort.greatestPowerOfTwoLessThan(n);

for (int i = lo; i < lo + n - m; i++) {
this.compare(A, i, i+m, dir);
}
this.bitonicMerge(A, lo, m, dir);
this.bitonicMerge(A, lo + m, n - m, dir);
}
}

private void bitonicSort(int[] A, int lo, int n, boolean dir)
{
if (n > 1)
{
int m = n / 2;
this.bitonicSort(A, lo, m, !dir);
this.bitonicMerge(A, lo, n, dir);
}
}

public void changeDirection(String choice) throws Exception {
if(choice.equals("forward")) this.direction = true;
else if(choice.equals("backward")) this.direction = false;
else throw new Exception("Invalid direction for Bitonic Sort!");
}

@Override
public void runSort(int[] array, int currentLength, int bucketCount) {
int iterations = 0;
int threshold = (int) (Math.log(currentLength) / Math.log(2)) / 2;

this.bitonicSort(array, 0, currentLength, this.direction);
do {
iterations++;

if(iterations >= threshold) {
InsertionSort linearInserter = new InsertionSort(this.Delays, this.Highlights, this.Reads, this.Writes);
linearInserter.customInsertSort(array, 0, currentLength, 0.1, false);
break;
}
} while (this.circleSortRoutine(array, 0, currentLength - 1, 0) != 0);
}
}
106 changes: 106 additions & 0 deletions src/sorts/DrunkMergeSort
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
///Taken from https://github.com/PiotrGrochowski/ArrayVisualizer/blob/master/src/sorts/DrunkMergeSort.java. I also made this sort back on scratch.

package sorts;

import templates.Sort;
import utils.Delays;
import utils.Highlights;
import utils.Reads;
import utils.Writes;

/*
*
MIT License

Copyright (c) 2019 w0rthy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*
*/

final public class DrunkMergeSort extends Sort {
public DrunkMergeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
super(delayOps, markOps, readOps, writeOps);

this.setSortPromptID("Drunk Merge");
this.setRunAllID("Drunk Merge Sort");
this.setReportSortID("Drunk Mergesort");
this.setCategory("Hybrid Sorts");
this.isComparisonBased(true);
this.isBucketSort(false);
this.isRadixSort(false);
this.isUnreasonablySlow(false);
this.setUnreasonableLimit(0);
this.isBogoSort(false);
}

private void drunkInsert(int[] arr, int start, int end) {
int pos;

for(int j = start; j < end; j++){
pos = j;

Highlights.markArray(1, j);
Highlights.clearMark(2);

while(pos > start && Reads.compare(arr[pos], arr[pos - 1]) < 1) {
Writes.swap(arr, pos, pos - 1, 0.2, true, false);
pos--;
}
}
}

private void drunkMerge(int[] arr, int min, int max, int mid) {
int i = 1;
int target = (mid - min);

while(i <= target) {
Writes.multiSwap(arr, mid + i, min + (i * 2) - 1, 0.05, true, false);
i++;
}

this.drunkInsert(arr, min, max + 1);
}

private void drunkMergeSort(int[] array, int min, int max) {
if(max - min == 0) { //only one element.
Delays.sleep(1); //no swap
}
else if(max - min == 1) { //only two elements and swaps them
if(Reads.compare(array[min], array[max]) == 1) {
Writes.swap(array, min, max, 0.01, true, false);
}
}
else {
int mid = (int) Math.floor((min + max) / 2); //The midpoint

this.drunkMergeSort(array, min, mid); //sort the left side
this.drunkMergeSort(array, mid + 1, max); //sort the right side
this.drunkMerge(array, min, max, mid); //combines them
this.drunkMergeSort(array, min, mid); //sort the left side
this.drunkMergeSort(array, mid + 1, max); //sort the right side
this.drunkMerge(array, min, max, mid); //combines them
}
}

@Override
public void runSort(int[] array, int currentLength, int bucketCount) {
this.drunkMergeSort(array, 0, currentLength - 1);
}
}
90 changes: 90 additions & 0 deletions src/sorts/STBSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package sorts;

import templates.Sort;
import utils.Delays;
import utils.Highlights;
import utils.Reads;
import utils.Writes;

/*
* This version of Bitonic Sort was taken from here, written by H.W. Lang:
* http://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/bitonic/oddn.htm
*/

final public class STBSort extends Sort {
private boolean direction = true;

public STBSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
super(delayOps, markOps, readOps, writeOps);

this.setSortPromptID("STB");
this.setRunAllID("STB Sort");
this.setReportSortID("STB Sort");
this.setCategory("Concurrent Sorts");
this.isComparisonBased(true);
this.isBucketSort(false);
this.isRadixSort(false);
this.isUnreasonablySlow(false);
this.setUnreasonableLimit(0);
this.isBogoSort(false);
}

private static int greatestPowerOfTwoLessThan(int n){
int k = 1;
while (k < n) {
k = k << 1;
}
return k >> 1;
}

private void compare(int[] A, int i, int j, boolean dir)
{
Highlights.markArray(1, i);
Highlights.markArray(2, j);

Delays.sleep(0.5);

int cmp = Reads.compare(A[i], A[j]);

if (dir == (cmp == 1)) Writes.swap(A, i, j, 1, true, false);
}

private void bitonicMerge(int[] A, int lo, int n, boolean dir)
{
if (n > 1)
{
int m = STBSort.greatestPowerOfTwoLessThan(n);

for (int i = lo; i < lo + n - m; i++) {
this.compare(A, i, i+m, dir);
}

this.bitonicSort(A, lo, m, dir);
this.bitonicMerge(A, lo + m, n - m, dir);
}
}

private void bitonicSort(int[] A, int lo, int n, boolean dir)
{
if (n > 1)
{
int m = n / 2;
this.bitonicSort(A, lo, m, !dir);
this.bitonicSort(A, lo + m, n - m, dir);
this.bitonicMerge(A, lo, n, dir);
this.bitonicSort(A, lo, m, dir);
}
}

public void changeDirection(String choice) throws Exception {
if(choice.equals("forward")) this.direction = true;
else if(choice.equals("backward")) this.direction = false;
else throw new Exception("Invalid direction for Bitonic Sort!");
}


@Override
public void runSort(int[] array, int currentLength, int bucketCount) {
this.bitonicSort(array, 0, currentLength, this.direction);
}
}