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

New Sorting Algorithms #61

Open
wants to merge 8 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
80 changes: 80 additions & 0 deletions src/sorts/BingoSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package sorts;

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

/*
* Bingo Sort is a variant of Selection Sort which looks through all elements, using the
* element with the maximum VALUE instead of the item to be swapped instead of the item.
*
* This is best suited to use when there are duplicate values in the array because the
* sort will run quicker (similar to Counting Sort) - running on O(n+m^2) best case scenario,
* otherwise it will run at O(n*m) time complexity,
* where "m" is the amount of unique values in the array.
*
* >> Original source(s):
* >> https://en.wikipedia.org/wiki/Selection_sort#Variants
* >> https://xlinux.nist.gov/dads/HTML/bingosort.html
*
* >> Imported and Translated (from Pascal) by Joel "McDude73" Zaleschuk
*/

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

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

@Override
public void runSort(int[] array, int length, int bucketCount) {
int maximum = length - 1;
int next = array[maximum];

for(int i=maximum-1;i>=0;i--) {
if(array[i] > next) {
next = array[i];
}
}
while(maximum > 0 && array[maximum] == next) {
maximum--;
}
while(maximum > 0) {
int val = next;
next = array[maximum];

for(int j=maximum-1;j>=0;j--) {

Highlights.markArray(1, array[j]);
Highlights.markArray(2, val);

if(Reads.compare(array[j], val) == 0) {
Writes.swap(array, j, maximum, 0.02, true, false);
maximum--;

}
else {
if(array[j] > next) {
next = array[j];
}
}
Delays.sleep(0.01);
}
while(maximum > 0 && array[maximum] == next) {
maximum--;
}
}
}
}
48 changes: 48 additions & 0 deletions src/sorts/BozoSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package sorts;

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

/*
* Bozosort will check to see if all the elements are sorted in their correct
* positions, just like Bogosort.
*
* If there is an element out of place, Bozosort will swap two elements at random
* instead of swapping every single one of them.
*
* >> Imported by Joel "McDude_73" Zaleschuk
*/

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

this.setSortPromptID("Bozo");
this.setRunAllID("Bozo Sort");
this.setReportSortID("Bozosort");
this.setCategory("Distributive Sorts");
this.isComparisonBased(false); //Comparisons are not used to swap elements
this.isBucketSort(false);
this.isRadixSort(false);
this.isUnreasonablySlow(true);
this.setUnreasonableLimit(16);
this.isBogoSort(true);
}

@Override
public void runSort(int[] array, int currentLen, int bucketCount) {
while(!this.bogoIsSorted(array, currentLen)) {
int a = (int) (Math.random() * currentLen);
int b = (int) (Math.random() * currentLen);

if(a == b) {
if(b + 1 >= currentLen) b = 0;
else ++b;
}
Writes.swap(array, a, b, 0, true, false);
}
}
}
59 changes: 59 additions & 0 deletions src/sorts/GrateSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package sorts;

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

/*
* Grate Sort will commit a pass every time the array is not considered sorted.
* What it will do is it will start by comparing the element selected from the pass to the
* right-most element.
*
* It will swap the elements if the right-most element is smaller than the one on the pass.
* It will check the element one to the left from where the last element was checked until
* the right marker reaches the element being checked during the pass.
*
* If at any point a swap was made, another pass must take place after the current one.
*
* >> Idea from EilrahcF#1021
* >> Scripted by Joel "McDude_73" Zaleschuk
*/

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

this.setSortPromptID("Grate");
this.setRunAllID("Grate Sort");
this.setReportSortID("Grate Sort");
this.setCategory("Exchange Sorts");
this.isComparisonBased(true);
this.isBucketSort(false);
this.isRadixSort(false);
this.isUnreasonablySlow(true);
this.setUnreasonableLimit(512);
this.isBogoSort(false);
}

@Override
public void runSort(int[] array, int currentLength, int bucketCount) {
boolean sorted = false;
while(!sorted) {
sorted = true;
for(int i=0;i<currentLength;i++) {
for(int j=currentLength;j>i;j--) {
Highlights.markArray(1, i);
Highlights.markArray(2, j);
Delays.sleep(0.25);
if(Reads.compare(array[i], array[j]) > 0) {
sorted = false;
Writes.swap(array, i, j, 0.1, true, false);
break;
}
}
}
}
}
}
104 changes: 104 additions & 0 deletions src/sorts/IterativeMergeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package sorts;

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

/*
* Iterative Merge Sort goes through multiple iterations of creating a sub-array on two
* different sides of what will become a merged array. The arrays are created multiple
* times, however each pass through the array will double the sub-array size.
*
* Once each value is written to the sub-arrays accordingly, then the elements
* are compared to each other based on what's in each sub-array
* via it's value, and is then written to the main array.
*
* Any leftover pieces from the sub-arrays are written into the main array last.
*
* >> Original source(s):
* >> https://www.geeksforgeeks.org/iterative-merge-sort/
*
* >> Imported by Joel "McDude73" Zaleschuk
*/

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

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

private void merge(int[] array, int left, int middle, int right) {
int i, j, k;
int n1 = middle - left + 1;
int n2 = right - middle;

int[] leftArr = new int[n1];
int[] rightArr = new int[n2];

for(i=0;i<n1;i++) {
Highlights.markArray(1, left+i);
Writes.write(leftArr, i, array[left+i], 0.75, false, true);
}
for(j=0;j<n2;j++) {
Highlights.markArray(1, j+middle+1);
Writes.write(rightArr, j, array[middle+1+j], 0.75, false, true);
}

i = 0;
j = 0;
k = left;
Highlights.clearMark(2);

while(i < n1 && j < n2) {
if(Reads.compare(leftArr[i], rightArr[j]) <= 0) {
Writes.write(array, k, leftArr[i], 0.75, true, false);
i++;
}
else {
Writes.write(array, k, rightArr[j], 0.75, true, false);
j++;
}
k++;
}

while(i < n1) {
Writes.write(array, k, leftArr[i], 0.75, true, false);
i++;
k++;
}

while(j < n2) {
Writes.write(array, k, rightArr[j], 0.75, true, false);
j++;
k++;
}
}

@Override
public void runSort(int[] array, int length, int bucketCount) {
int currentSize, leftStart;

for(currentSize=1;currentSize<=length-1;currentSize=2*currentSize) {

for(leftStart=0;leftStart<length-1;leftStart+=2*currentSize) {

int middle = Math.min(leftStart+currentSize-1, length-1);
int rightEnd = Math.min(leftStart+2*currentSize-1, length-1);

merge(array, leftStart, middle, rightEnd);
}
}
}
}
95 changes: 95 additions & 0 deletions src/sorts/IterativeQuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package sorts;

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

/*
* Iterative Quick Sort runs a few routines:
*
* Initially, a stack is created which will initially have a starting and ending index
* written to it. These values are then traded back into the starting and ending index
* values when the main loop is executed (see below).
*
* As long as the value can be written from the index position of "top", it will
* run a loop which will do the following:
*
* The ending index and starting index are overwritten with pushed elements in the stack
* where the index of the "top" variable currently is, then subtracting "top" by one.
*
* The partition will help with determining which elements should be swapped based on
* the given starting and ending index values.
*
* The stack may continue to be written to after every partition is run based on
* comparing the partition with the starting and ending index values running two different
* comparisons, then the stack will push up to four elements to the top of this stack.
*
* >> Original source(s):
* >> https://www.geeksforgeeks.org/iterative-quick-sort/
*
* >> Imported by Joel "McDude73" Zaleschuk
*/

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

this.setSortPromptID("Iterative Quick");
this.setRunAllID("Iterative Quick Sort");
this.setReportSortID("Iterative Quicksort");
this.setCategory("Exchange Sorts");
this.isComparisonBased(true);
this.isBucketSort(false);
this.isRadixSort(false);
this.isUnreasonablySlow(false);
this.setUnreasonableLimit(0);
this.isBogoSort(false);
}

private int partition(int[] array, int lowValue, int highValue) {
int pivot = array[highValue];

int i = lowValue - 1;
for(int j=lowValue;j<=highValue-1;j++) {
if(Reads.compare(array[j], pivot) <= 0) {
i++;
Writes.swap(array, i, j, 1, true, false);
}
}
Writes.swap(array, i+1, highValue, 1, true, false);
return i + 1;
}

private void quickSort(int[] array, int startIndex, int endIndex) {
int[] stack = new int[endIndex - startIndex + 1];

int top = -1;

Writes.write(stack, ++top, startIndex, 0, true, true);
Writes.write(stack, ++top, endIndex, 0, true, true);

while(top >= 0) {
endIndex = stack[top--];
startIndex = stack[top--];

int p = partition(array, startIndex, endIndex);

if(Reads.compare(p-1, startIndex) == 1) {
Writes.write(stack, ++top, startIndex, 0, false, true);
Writes.write(stack, ++top, p-1, 0, false, true);
}

if(Reads.compare(p+1, endIndex) == -1) {
Writes.write(stack, ++top, p+1, 0, false, true);
Writes.write(stack, ++top, endIndex, 0, false, true);
}
}
}

@Override
public void runSort(int[] array, int length, int bucketCount) {
quickSort(array, 0, length-1);
}
}
Loading