commit cb7ddd09dfec1298edae1d3852b8814447c17776 Author: Baipyrus Date: Sun May 15 17:10:16 2022 +0200 initial commit diff --git a/CombSort/CombSort.pde b/CombSort/CombSort.pde new file mode 100644 index 0000000..cc53db4 --- /dev/null +++ b/CombSort/CombSort.pde @@ -0,0 +1,81 @@ +final int amount = 100; +final float timeout = 2.5; + +ArrayList bars; +float barWidth; + +int current = 0; +int distance = -1; +boolean complete = false; + +int sortedFrameCountCapture = -1; +float tenPercentHeight; +float ninetyPercentHeight; + +void setup() { + // fullScreen(); + size(1280, 720, P3D); + + distance = amount-1; + tenPercentHeight = height * 0.1; + ninetyPercentHeight = height * 0.9; + bars = new ArrayList(); + for (int i = 0; i < amount; i++) { + bars.add(floor(random(tenPercentHeight, ninetyPercentHeight))); + } + barWidth = (float)width / amount; + + frameRate(120); +} + +void draw() { + background(0); + + stroke(0); + for (int i = 0; i < bars.size(); i++) { + if (i == current && !complete) { + fill(255,0,0); + } else if (i == current + distance && !complete) { + fill(0,255,0); + } else { + fill(255); + } + rect(i * barWidth, height-bars.get(i), barWidth, bars.get(i)); + } + + fill(255); + stroke(255); + textAlign(LEFT, TOP); + textSize(width*0.022); + text("Bar Amount: "+amount+"; FPS: "+round(frameRate)+";", 0, 0); + + if (!complete && distance > 0) { + final int tempValue = bars.get(current); + if (tempValue > bars.get(current+distance)) { + bars.set(current, bars.get(current+distance)); + bars.set(current+distance, tempValue); + } + current++; + if (current+distance == amount) { + current = 0; + distance--; + if (distance == 0) { + complete = true; + sortedFrameCountCapture = frameCount; + } + } + } else if (sortedFrameCountCapture != -1 && frameCount-sortedFrameCountCapture >= amount*timeout) { + sortedFrameCountCapture = -1; + current = 0; + complete = false; + distance = amount-1; + + bars = new ArrayList(); + for (int i = 0; i < amount; i++) { + bars.add(floor(random(tenPercentHeight, ninetyPercentHeight))); + } + barWidth = (float)width / amount; + + // frameRate(amount); + } +} \ No newline at end of file diff --git a/InsertionSort/InsertionSort.pde b/InsertionSort/InsertionSort.pde new file mode 100644 index 0000000..75c5e64 --- /dev/null +++ b/InsertionSort/InsertionSort.pde @@ -0,0 +1,86 @@ +final int amount = 250; +final float timeout = 2.5; + +ArrayList bars; +float barWidth; + +int current = 1; +int compare = 1; +boolean complete = false, hasSwitched = false; + +int sortedFrameCountCapture = -1; +float tenPercentHeight; +float ninetyPercentHeight; + +void setup() { + // fullScreen(); + size(1280, 720, P3D); + + tenPercentHeight = height * 0.1; + ninetyPercentHeight = height * 0.9; + bars = new ArrayList(); + for (int i = 0; i < amount; i++) { + bars.add(floor(random(tenPercentHeight, ninetyPercentHeight))); + } + barWidth = (float)width / amount; + + frameRate(1000); +} + +void draw() { + background(0); + + stroke(0); + for (int i = 0; i < bars.size(); i++) { + if (i == current && !complete) { + fill(255,0,0); + } else if (i == compare && !complete) { + fill(0,255,0); + } else { + fill(255); + } + rect(i * barWidth, height-bars.get(i), barWidth, bars.get(i)); + } + + fill(255); + stroke(255); + textAlign(LEFT, TOP); + textSize(width*0.022); + text("Bar Amount: "+amount+"; FPS: "+round(frameRate)+";", 0, 0); + + if (!complete) { + boolean noSwap = false; + if (bars.get(compare) < bars.get(compare - 1)) { + hasSwitched = true; + final int temporaryValue = bars.get(compare); + bars.set(compare, bars.get(compare - 1)); + bars.set(compare - 1, temporaryValue); + } else { + noSwap = true; + } + + if (compare <= 1 || noSwap) { + current++; + compare = current; + if (current == bars.size()) { + complete = true; + sortedFrameCountCapture = frameCount; + } + } else { + compare--; + } + } else if (sortedFrameCountCapture != -1 && frameCount-sortedFrameCountCapture >= amount*timeout) { + current = 1; + compare = 1; + complete = false; + hasSwitched = false; + + bars = new ArrayList(); + for (int i = 0; i < amount; i++) { + bars.add(floor(random(tenPercentHeight, ninetyPercentHeight))); + } + barWidth = (float)width / amount; + + // frameRate(amount); + } +} \ No newline at end of file diff --git a/QuickSort/QuickSort.pde b/QuickSort/QuickSort.pde new file mode 100644 index 0000000..f688a88 --- /dev/null +++ b/QuickSort/QuickSort.pde @@ -0,0 +1,142 @@ +final float timeout = 2.5; + +ArrayList bars = new ArrayList(); +ArrayList queue = new ArrayList(); +int mQL = 1, aQL = 0, amount = 1000; +float barWidth; + +int sortedFrameCountCapture = -1; + +void setup() { + fullScreen(P3D); + // size(1280, 720); + + // amount = floor(random(50, 500)); + barWidth = (float)width / amount; + for (int i = 0; i < amount; i++) { + bars.add(round(random(height * 0.1, height * 0.9))); + } + queue.add(new Job(0, amount-1)); + + frameRate(1000); +} + +void draw() { + background(0); + stroke(0); + + for (int i = 0; i < bars.size(); i++) { + // stroke(255); + fill(255); + for (int j = 0; j < queue.size(); j++) { + Job job = queue.get(j); + if (i == job.rightIndex || i == job.leftIndex) { + // stroke(255, 0, 0); + fill(255, 0, 0); + } else if (i == job.i) { + // stroke(0, 255, 0); + fill(0, 255, 0); + } else if (i == job.j) { + // stroke(0, 0, 255); + fill(0, 0, 255); + } + } + // line(i, height, i, height-bars.get(i)); + rect(i * barWidth, height, barWidth, -bars.get(i)); + } + + /*if (queue.size() > 0) { + queue.get(0).sortStep(); + }*/ + for (int i = 0; i < queue.size(); i++) { + queue.get(i).sortStep(); + } + + if (queue.size() == 0) { + if (sortedFrameCountCapture == -1) { + sortedFrameCountCapture = frameCount; + } else if (frameCount-sortedFrameCountCapture>=round((amount/2)*timeout)) { + sortedFrameCountCapture = -1; + + // amount = floor(random(50, 500)); + barWidth = (float)width / amount; + bars = new ArrayList(); + for (int i = 0; i < amount; i++) { + bars.add(round(random(height * 0.1, height * 0.9))); + } + queue.add(new Job(0, amount-1)); + + mQL = 1; + aQL = 0; + + frameRate(1000); + } + } + + mQL = max(queue.size(), mQL); + + fill(255); + stroke(255); + textAlign(LEFT, TOP); + textSize(width*0.022); + text("Queue Length: "+queue.size()+"; Max. Length: "+mQL+"; Absolute Length: "+aQL+"; Bar Amount: "+bars.size()+"; FPS: "+round(frameRate)+";", 0, 0); +} + +class Job { + int leftIndex, rightIndex; + int i, j, pivotValue; + boolean iIndex, jIndex; + + Job(int start, int end) { + i = start; + j = end - 1; + rightIndex = end; + leftIndex = start; + pivotValue = bars.get(rightIndex); + } + + void sortStep() { + checkIndecies(); + if (i < j || iIndex || jIndex) { + if (iIndex) { + i++; + } else if (jIndex) { + j--; + } else if (i < j) { + final int tV1 = bars.get(i); + bars.set(i, bars.get(j)); + bars.set(j, tV1); + } + } else { + // Reverse here for reverse sorting-order + if (bars.get(i) > pivotValue) { + final int tV2 = bars.get(i); + bars.set(i, bars.get(rightIndex)); + bars.set(rightIndex, tV2); + } + + if (isInBounds(i-1) && (leftIndex < i-1)) { + queue.add(new Job(leftIndex, i-1)); + } + if (isInBounds(i+1) && (i+1 < rightIndex)) { + queue.add(new Job(i+1, rightIndex)); + } + aQL++; + queue.remove(this); + } + } + + void checkIndecies() { + // Reverse here for reverse sorting-order + iIndex = (i < rightIndex) && (bars.get(i) < pivotValue); + jIndex = (j > leftIndex) && (bars.get(j) >= pivotValue); + } + + boolean isEqual(Job other) { + return ((other.leftIndex == leftIndex) && (other.rightIndex == rightIndex)); + } +} + +boolean isInBounds(int index) { + return ((index >= 0) && (index < bars.size())); +} \ No newline at end of file diff --git a/SelectionSort/SelectionSort.pde b/SelectionSort/SelectionSort.pde new file mode 100644 index 0000000..47615b1 --- /dev/null +++ b/SelectionSort/SelectionSort.pde @@ -0,0 +1,93 @@ +// final int FPS = 1000; +final float timeout = 2.5; + +ArrayList bars; +float barWidth; +int amount; + +int current = 0, compare = 1; +boolean complete = false; +int recordIndex = 0; + +float tenPercentHeight; +float ninetyPercentHeight; +int sortedFrameCountCapture = -1; + +void setup() { + fullScreen(P3D); + // size(1280, 720, P3D); + + tenPercentHeight = height * 0.1; + ninetyPercentHeight = height * 0.9; + amount = floor(random(100, 500)); + bars = new ArrayList(); + for (int i = 0; i < amount; i++) { + bars.add(floor(random(tenPercentHeight, ninetyPercentHeight))); + } + barWidth = (float)width / amount; + + // frameRate(amount); + frameRate(1000); +} + +void draw() { + background(0); + + stroke(0); + for (int i = 0; i < bars.size(); i++) { + if (i == current && !complete) { + fill(255,0,0); + } else if (i == compare && !complete) { + fill(0,255,0); + } else if (i == recordIndex && !complete) { + fill(0,0,255); + } else { + fill(255); + } + rect(i * barWidth, height-bars.get(i), barWidth, bars.get(i)); + } + + fill(255); + stroke(255); + textAlign(LEFT, TOP); + textSize(width*0.022); + text("Bar Amount: "+amount+"; FPS: "+round(frameRate)+";", 0, 0); + + if (!complete) { + if (bars.get(compare) < bars.get(recordIndex)) { + recordIndex = compare; + } + + compare++; + if (compare == bars.size()) { + final int tempValue = bars.get(current); + if (bars.get(recordIndex) < tempValue) { + bars.set(current, bars.get(recordIndex)); + bars.set(recordIndex, tempValue); + } + + current++; + compare = current+1; + recordIndex = current; + if (current == bars.size()-1) { + complete = true; + sortedFrameCountCapture = frameCount; + } + } + } else if (sortedFrameCountCapture != -1 && frameCount-sortedFrameCountCapture >= amount*timeout) { + sortedFrameCountCapture = -1; + current = 0; + compare = 1; + complete = false; + + amount = floor(random(100, 500)); + bars = new ArrayList(); + for (int i = 0; i < amount; i++) { + bars.add(floor(random(tenPercentHeight, ninetyPercentHeight))); + } + barWidth = (float)width / amount; + + // frameRate(amount); + frameRate(1000); + } +} diff --git a/ShakerSort/ShakerSort.pde b/ShakerSort/ShakerSort.pde new file mode 100644 index 0000000..20790ae --- /dev/null +++ b/ShakerSort/ShakerSort.pde @@ -0,0 +1,117 @@ +final float timeout = 2.5; +final int aMin = 100; +final int aMax = 500; +final int repeats = 4; + +ArrayList bars; +float barWidth; +int amount; + +int sortOnLeft = 0, sortOnRight = 0; +int current = 1; +boolean complete = false; +boolean direction = true; +boolean hasSwitched = false; + +float tenPercentHeight; +float ninetyPercentHeight; +int sortedFrameCountCapture = -1; + +void setup() { + fullScreen(P3D); + // size(1280, 720, P3D); + + tenPercentHeight = height * 0.1; + ninetyPercentHeight = height * 0.9; + amount = floor(random(aMin, aMax)); + bars = new ArrayList(); + for (int i = 0; i < amount; i++) { + bars.add(floor(random(tenPercentHeight, ninetyPercentHeight))); + } + barWidth = (float)width / amount; + + // frameRate(amount); + frameRate(1000); +} + +void draw() { + background(0); + + stroke(0); + for (int i = 0; i < bars.size(); i++) { + if (i == sortOnLeft && !complete) { + fill(255, 0, 0); + } else if (i == amount-1-sortOnRight && !complete) { + fill(255, 0, 0); + } else if (i == current && !complete) { + fill(0, 255, 0); + } else { + fill(255); + } + rect(i * barWidth, height-bars.get(i), barWidth, bars.get(i)); + } + + fill(255); + stroke(255); + textAlign(LEFT, TOP); + textSize(width*0.022); + text("Bar Amount: "+amount+"; FPS: "+round(frameRate)+";", 0, 0); + + for (int j = 0; j < repeats; j++) { + if (!complete) { + final int tempValue = bars.get(current); + if (tempValue < bars.get(current-1)) { + bars.set(current, bars.get(current-1)); + bars.set(current-1, tempValue); + hasSwitched = true; + } + + if (current == amount-1-sortOnRight && direction) { + direction = !direction; + sortOnRight++; + if (hasSwitched == false) { + complete = true; + } + hasSwitched = false; + } else if (current == sortOnLeft+1 && !direction) { + direction = !direction; + sortOnLeft++; + if (hasSwitched == false) { + complete = true; + } + hasSwitched = false; + } else { + if (direction) { + current++; + } else { + current--; + } + } + + if (sortOnLeft+1 == amount-1-sortOnRight) { + complete = true; + } + + if (complete) { + sortedFrameCountCapture = frameCount; + } + } else if (sortedFrameCountCapture != -1 && frameCount-sortedFrameCountCapture >= amount*timeout) { + sortedFrameCountCapture = -1; + current = 1; + complete = false; + sortOnLeft = 0; + sortOnRight = 0; + direction = true; + hasSwitched = false; + + amount = floor(random(aMin, aMax)); + bars = new ArrayList(); + for (int i = 0; i < amount; i++) { + bars.add(floor(random(tenPercentHeight, ninetyPercentHeight))); + } + barWidth = (float)width / amount; + + // frameRate(amount); + } + } +}