This commit is contained in:
ngb
2021-06-07 09:32:52 +02:00
commit 8c99dbafd8
38 changed files with 781 additions and 0 deletions

BIN
lib/ACE_v0.3.0.jar Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,19 @@
#BlueJ class context
comment0.target=Algorithm
comment1.params=arr
comment1.target=Algorithm(int[])
comment2.params=
comment2.target=void\ run()
comment3.params=
comment3.target=void\ delay()
comment4.params=
comment4.target=void\ start()
comment5.params=
comment5.target=int[]\ getArray()
comment6.params=
comment6.target=int\ getCursor()
comment7.params=i1\ i2
comment7.target=void\ swap(int,\ int)
comment8.params=
comment8.target=void\ algorithm()
numComments=9

View File

@@ -0,0 +1,57 @@
package algorithms;
public abstract class Algorithm implements Runnable {
private int delay = 100;
volatile int[] arr;
public volatile int accesses, comparisons;
public boolean done = false;
int cursor = 0;
public Algorithm(int[] arr) {
this.arr = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
this.arr[i] = arr[i];
}
}
@Override
public void run() {
algorithm();
done = true;
}
public void delay() {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void start() {
new Thread(this).start();
}
public int[] getArray() {
// int[] newArr = new int[arr.length];
// for (int i = 0; i < arr.length; i++) {
// newArr[i] = arr[i];
// }
// return newArr;
return arr;
}
public int getCursor() {
return cursor;
}
public void swap(int i1, int i2) {
int temp = arr[i1];
arr[i1] = arr[i2];
arr[i2] = temp;
}
abstract void algorithm();
}

Binary file not shown.

View File

@@ -0,0 +1,7 @@
#BlueJ class context
comment0.target=BetterBubbleSort
comment1.params=arr
comment1.target=BetterBubbleSort(int[])
comment2.params=
comment2.target=void\ algorithm()
numComments=3

View File

@@ -0,0 +1,32 @@
package algorithms;
public class BetterBubbleSort extends Algorithm {
int border;
public BetterBubbleSort(int[] arr) {
super(arr);
}
@Override
void algorithm() {
border = arr.length;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - 1; j++) {
cursor = j;
delay();
if (arr[j] > arr[j + 1]) {
swap(j, j + 1);
accesses += 4;
}
accesses += 2;
comparisons++;
if (j == border - 1) {
break;
}
}
border--;
}
}
}

Binary file not shown.

View File

@@ -0,0 +1,7 @@
#BlueJ class context
comment0.target=BubbleSort
comment1.params=arr
comment1.target=BubbleSort(int[])
comment2.params=
comment2.target=void\ algorithm()
numComments=3

View File

@@ -0,0 +1,24 @@
package algorithms;
public class BubbleSort extends Algorithm {
public BubbleSort(int[] arr) {
super(arr);
}
@Override
public void algorithm() {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - 1; j++) {
cursor = j;
delay();
if (arr[j] > arr[j + 1]) {
swap(j, j + 1);
accesses += 4;
}
accesses += 2;
comparisons++;
}
}
}
}

Binary file not shown.

View File

@@ -0,0 +1,7 @@
#BlueJ class context
comment0.target=InsertionSort
comment1.params=arr
comment1.target=InsertionSort(int[])
comment2.params=
comment2.target=void\ algorithm()
numComments=3

View File

@@ -0,0 +1,30 @@
package algorithms;
public class InsertionSort extends Algorithm {
int index;
public InsertionSort(int[] arr) {
super(arr);
}
@Override
void algorithm() {
while (index < arr.length) {
for (int i = index; i > 0; i--) {
cursor = i;
delay();
if (arr[i] < arr[i - 1]) {
swap(i, i - 1);
accesses += 4;
}else {
break;
}
accesses += 2;
comparisons++;
}
index++;
}
}
}

Binary file not shown.

View File

@@ -0,0 +1,11 @@
#BlueJ class context
comment0.target=MergeSort
comment1.params=arr
comment1.target=MergeSort(int[])
comment2.params=
comment2.target=void\ algorithm()
comment3.params=arr\ l\ m\ r
comment3.target=void\ merge(int[],\ int,\ int,\ int)
comment4.params=arr\ l\ r
comment4.target=void\ sort(int[],\ int,\ int)
numComments=5

View File

@@ -0,0 +1,78 @@
package algorithms;
public class MergeSort extends Algorithm {
public MergeSort(int[] arr) {
super(arr);
}
@Override
void algorithm() {
sort(arr, 0, arr.length - 1);
}
private void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int[n1];
int R[] = new int[n2];
for (int i = 0; i < n1; ++i) {
cursor = l + i;
delay();
L[i] = arr[l + i];
accesses++;
}
for (int j = 0; j < n2; ++j) {
cursor = m + 1 + j;
delay();
R[j] = arr[m + 1 + j];
accesses++;
}
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
cursor = k;
delay();
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
comparisons++;
accesses++;
k++;
}
while (i < n1) {
arr[k] = L[i];
accesses++;
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
accesses++;
j++;
k++;
}
}
private void sort(int arr[], int l, int r) {
if (l < r) {
int m = (l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
}

Binary file not shown.

View File

@@ -0,0 +1,11 @@
#BlueJ class context
comment0.target=QuickSort
comment1.params=arr
comment1.target=QuickSort(int[])
comment2.params=
comment2.target=void\ algorithm()
comment3.params=arr\ low\ high
comment3.target=int\ partition(int[],\ int,\ int)
comment4.params=arr\ low\ high
comment4.target=void\ sort(int[],\ int,\ int)
numComments=5

View File

@@ -0,0 +1,49 @@
package algorithms;
public class QuickSort extends Algorithm {
public QuickSort(int[] arr) {
super(arr);
}
@Override
void algorithm() {
sort(arr, 0, arr.length - 1);
}
private int partition(int arr[], int low, int high) {
int pivot = arr[high];
accesses++;
int i = (low - 1);
for (int j = low; j < high; j++) {
cursor = j;
delay();
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
accesses += 4;
}
comparisons++;
accesses++;
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
accesses += 4;
return i + 1;
}
private void sort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
sort(arr, low, pi - 1);
sort(arr, pi + 1, high);
}
}
}

Binary file not shown.

View File

@@ -0,0 +1,13 @@
#BlueJ class context
comment0.target=RadixSort
comment1.params=arr
comment1.target=RadixSort(int[])
comment2.params=
comment2.target=void\ algorithm()
comment3.params=arr\ n
comment3.target=int\ getMax(int[],\ int)
comment4.params=arr\ n\ exp
comment4.target=void\ countSort(int[],\ int,\ int)
comment5.params=arr\ n
comment5.target=void\ radixsort(int[],\ int)
numComments=6

View File

@@ -0,0 +1,70 @@
package algorithms;
import java.util.Arrays;
public class RadixSort extends Algorithm {
int exponent = 2;
public RadixSort(int[] arr) {
super(arr);
}
@Override
void algorithm() {
radixsort(arr, arr.length);
}
private int getMax(int arr[], int n) {
int mx = arr[0];
accesses++;
for (int i = 1; i < n; i++) {
if (arr[i] > mx) {
mx = arr[i];
accesses++;
}
accesses++;
comparisons++;
}
return mx;
}
private void countSort(int arr[], int n, int exp) {
int output[] = new int[n];
int i;
int count[] = new int[exponent];
Arrays.fill(count, 0);
for (i = 0; i < n; i++) {
cursor = i;
delay();
count[(arr[i] / exp) % exponent]++;
accesses++;
}
for (i = 1; i < exponent; i++) {
count[i] += count[i - 1];
}
for (i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % exponent] - 1] = arr[i];
count[(arr[i] / exp) % exponent]--;
accesses += 3;
}
for (i = 0; i < n; i++) {
cursor = i;
delay();
arr[i] = output[i];
accesses++;
}
}
private void radixsort(int arr[], int n) {
int m = getMax(arr, n);
for (int exp = 1; m / exp > 0; exp *= exponent)
countSort(arr, n, exp);
}
}

Binary file not shown.

View File

@@ -0,0 +1,7 @@
#BlueJ class context
comment0.target=SelectionSort
comment1.params=arr
comment1.target=SelectionSort(int[])
comment2.params=
comment2.target=void\ algorithm()
numComments=3

View File

@@ -0,0 +1,31 @@
package algorithms;
public class SelectionSort extends Algorithm {
int start;
public SelectionSort(int[] arr) {
super(arr);
}
@Override
void algorithm() {
while (start < arr.length) {
int min = start;
for (int i = start; i < arr.length; i++) {
cursor = i;
delay();
if (arr[i] < arr[min]) {
min = i;
}
accesses += 2;
comparisons++;
}
swap(start, min);
accesses += 4;
start++;
}
}
}

View File

@@ -0,0 +1,76 @@
#BlueJ package file
objectbench.height=128
objectbench.width=1256
package.divider.horizontal=0.6
package.divider.vertical=0.8005908419497785
package.editor.height=535
package.editor.width=1145
package.editor.x=0
package.editor.y=351
package.frame.height=776
package.frame.width=1296
package.numDependencies=0
package.numTargets=8
package.showExtends=true
package.showUses=true
readme.height=58
readme.name=@README
readme.width=47
readme.x=10
readme.y=10
target1.height=50
target1.name=QuickSort
target1.showInterface=false
target1.type=ClassTarget
target1.width=90
target1.x=10
target1.y=80
target2.height=50
target2.name=BubbleSort
target2.showInterface=false
target2.type=ClassTarget
target2.width=90
target2.x=100
target2.y=90
target3.height=50
target3.name=RadixSort
target3.showInterface=false
target3.type=ClassTarget
target3.width=90
target3.x=190
target3.y=90
target4.height=50
target4.name=SelectionSort
target4.showInterface=false
target4.type=ClassTarget
target4.width=110
target4.x=10
target4.y=150
target5.height=50
target5.name=InsertionSort
target5.showInterface=false
target5.type=ClassTarget
target5.width=110
target5.x=110
target5.y=150
target6.height=50
target6.name=MergeSort
target6.showInterface=false
target6.type=ClassTarget
target6.width=90
target6.x=10
target6.y=210
target7.height=50
target7.name=BetterBubbleSort
target7.showInterface=false
target7.type=ClassTarget
target7.width=130
target7.x=100
target7.y=210
target8.height=50
target8.name=Algorithm
target8.showInterface=false
target8.type=AbstractTarget
target8.width=90
target8.x=10
target8.y=270

Binary file not shown.

View File

@@ -0,0 +1,5 @@
#BlueJ class context
comment0.target=SortingAlgorithms2Launcher
comment1.params=args
comment1.target=void\ main(java.lang.String[])
numComments=2

View File

@@ -0,0 +1,8 @@
package main;
public class SortingAlgorithms2Launcher {
public static void main(String[] args) {
new Window();
}
}

BIN
src/main/Tiles.class Normal file

Binary file not shown.

9
src/main/Tiles.ctxt Normal file
View File

@@ -0,0 +1,9 @@
#BlueJ class context
comment0.target=Tiles
comment1.params=algorithm
comment1.target=Tiles(algorithms.Algorithm)
comment2.params=g2d\ offsetX\ offsetY\ width\ height
comment2.target=void\ draw(java.awt.Graphics2D,\ int,\ int,\ int,\ int)
comment3.params=algo
comment3.target=java.awt.image.BufferedImage\ makeImageFromAlgorithm(algorithms.Algorithm)
numComments=4

52
src/main/Tiles.java Normal file
View File

@@ -0,0 +1,52 @@
package main;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import algorithms.Algorithm;
public class Tiles {
private Algorithm algorithm;
public Tiles(Algorithm algorithm) {
this.algorithm = algorithm;
this.algorithm.start();
}
public void draw(Graphics2D g2d, int offsetX, int offsetY, int width, int height) {
g2d.drawImage(makeImageFromAlgorithm(algorithm), offsetX, offsetY, width, height, null);
g2d.setColor(Color.WHITE);
g2d.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
g2d.drawString(" Algorithm:", 5 + offsetX, 15 + offsetY);
g2d.drawString(" Accesses:", 5 + offsetX, 30 + offsetY);
g2d.drawString("Comparisons:", 5 + offsetX, 45 + offsetY);
g2d.drawString(algorithm.getClass().getSimpleName(), 110 + offsetX, 15 + offsetY);
g2d.drawString(algorithm.accesses + "", 110 + offsetX, 30 + offsetY);
g2d.drawString(algorithm.comparisons + "", 110 + offsetX, 45 + offsetY);
}
private BufferedImage makeImageFromAlgorithm(Algorithm algo) {
int[] arr = algo.getArray();
BufferedImage img = new BufferedImage(arr.length, arr.length,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < arr.length; x++) {
for (int y = 0; y < arr[x]; y++) {
int c;
if (x == algo.getCursor()) {
c = Color.RED.getRGB();
}else {
c = Color.WHITE.getRGB();
}
if (algo.done) {
c = Color.GREEN.getRGB();
}
img.setRGB(x, arr.length - y - 1, c);
}
}
return img;
}
}

BIN
src/main/Window$1.class Normal file

Binary file not shown.

BIN
src/main/Window.class Normal file

Binary file not shown.

9
src/main/Window.ctxt Normal file
View File

@@ -0,0 +1,9 @@
#BlueJ class context
comment0.target=Window
comment1.params=
comment1.target=Window()
comment2.params=g2d
comment2.target=void\ frameLoop(java.awt.Graphics2D)
comment3.params=g2d
comment3.target=void\ loop(java.awt.Graphics2D)
numComments=4

85
src/main/Window.java Normal file
View File

@@ -0,0 +1,85 @@
package main;
import java.awt.Graphics2D;
import java.util.ArrayList;
import algorithms.BetterBubbleSort;
import algorithms.BubbleSort;
import algorithms.InsertionSort;
import algorithms.MergeSort;
import algorithms.QuickSort;
import algorithms.RadixSort;
import algorithms.SelectionSort;
import graphics.Frame;
public class Window {
private Frame frame;
private ArrayList<Tiles> tiles;
public Window() {
frame = new Frame() {
@Override
public void frameLoop(Graphics2D g2d) {
loop(g2d);
}
};
frame.setTitle("Sorting Algorithms");
int[] arr = new int[50];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int temp = arr[i];
arr[i] = arr[rand];
arr[rand] = temp;
}
tiles = new ArrayList<Tiles>();
tiles.add(new Tiles(new BubbleSort(arr)));
tiles.add(new Tiles(new BetterBubbleSort(arr)));
tiles.add(new Tiles(new SelectionSort(arr)));
tiles.add(new Tiles(new InsertionSort(arr)));
tiles.add(new Tiles(new MergeSort(arr)));
tiles.add(new Tiles(new QuickSort(arr)));
tiles.add(new Tiles(new RadixSort(arr)));
frame.startLoop();
}
private void loop(Graphics2D g2d) {
int countX = 1;
int countY = 1;
switch (tiles.size()) {
case 1:
case 2:
case 3:
countX = tiles.size();
break;
case 4:
countX = 2;
countY = 2;
break;
case 5:
case 6:
case 7:
case 8:
case 9:
countX = 3;
countY = 3;
}
int width = frame.getWidth() / countX;
int height = frame.getHeight() / countY;
for (int i = 0; i < tiles.size(); i++) {
tiles.get(i).draw(g2d, width * (i % countX), height * ((i - (i % countX)) / countY), width, height);
}
}
}

47
src/main/package.bluej Normal file
View File

@@ -0,0 +1,47 @@
#BlueJ package file
dependency1.from=SortingAlgorithms2Launcher
dependency1.to=Window
dependency1.type=UsesDependency
dependency2.from=Window
dependency2.to=Tiles
dependency2.type=UsesDependency
objectbench.height=93
objectbench.width=760
package.divider.horizontal=0.6
package.divider.vertical=0.8003992015968064
package.editor.height=394
package.editor.width=649
package.editor.x=190
package.editor.y=171
package.frame.height=600
package.frame.width=800
package.numDependencies=2
package.numTargets=3
package.showExtends=true
package.showUses=true
readme.height=58
readme.name=@README
readme.width=47
readme.x=10
readme.y=10
target1.height=50
target1.name=Window
target1.showInterface=false
target1.type=ClassTarget
target1.width=80
target1.x=70
target1.y=60
target2.height=50
target2.name=Tiles
target2.showInterface=false
target2.type=ClassTarget
target2.width=80
target2.x=310
target2.y=80
target3.height=50
target3.name=SortingAlgorithms2Launcher
target3.showInterface=false
target3.type=ClassTarget
target3.width=200
target3.x=180
target3.y=240

37
src/package.bluej Normal file
View File

@@ -0,0 +1,37 @@
#BlueJ package file
editor.fx.0.height=776
editor.fx.0.width=1296
editor.fx.0.x=-53
editor.fx.0.y=99
objectbench.height=93
objectbench.width=760
package.divider.horizontal=0.6
package.divider.vertical=0.8003992015968064
package.editor.height=394
package.editor.width=649
package.editor.x=258
package.editor.y=154
package.frame.height=600
package.frame.width=800
package.numDependencies=0
package.numTargets=2
package.showExtends=true
package.showUses=true
project.charset=windows-1252
readme.height=58
readme.name=@README
readme.width=47
readme.x=10
readme.y=10
target1.height=62
target1.name=algorithms
target1.type=PackageTarget
target1.width=80
target1.x=90
target1.y=10
target2.height=62
target2.name=main
target2.type=PackageTarget
target2.width=80
target2.x=180
target2.y=10