Skip to content

Commit aee4fe6

Browse files
committed
Initial upload - Algorithm Visualizer
0 parents  commit aee4fe6

29 files changed

Lines changed: 1036 additions & 0 deletions

.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
.kotlin
6+
7+
### Eclipse ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
bin/
16+
!**/src/main/**/bin/
17+
!**/src/test/**/bin/
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/nbbuild/
22+
/dist/
23+
/nbdist/
24+
/.nb-gradle/
25+
26+
### VS Code ###
27+
.vscode/
28+
29+
### Mac OS ###
30+
.DS_Store

.idea/.gitignore

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/core_4_5_2.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AlgoVisualizer.iml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" name="core-4.5.2" level="project" />
11+
</component>
12+
</module>

src/algorithms/BubbleSort.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package algorithms;
2+
3+
public class BubbleSort implements SortAlgorithm {
4+
private int i = 0;
5+
private int j = 0;
6+
private int swapA;
7+
private int swapB;
8+
private boolean finished = false;
9+
private boolean didSwap = false;
10+
int[] arr;
11+
12+
public int getI() { return i; }
13+
public int getJ() { return j; }
14+
public boolean didSwap() { return didSwap; }
15+
16+
@Override
17+
public boolean isFinished() { return finished; }
18+
19+
@Override
20+
public int[] getActiveIndices() {
21+
//stay in bounds, don't compare what's already sorted
22+
// if (j >= arr.length - i - 1) return null;
23+
//Currently active pair being compared
24+
return new int[] { j, j+1 };
25+
}
26+
27+
@Override
28+
public int[] getSwapIndices() {
29+
//get swapping pair
30+
return didSwap ? new int[] {swapA, swapB} : null;
31+
}
32+
33+
public BubbleSort(int[] arr) {
34+
this.arr = arr;
35+
}
36+
37+
public int[] getArray() {
38+
return arr;
39+
}
40+
41+
// returns true when finished
42+
@Override
43+
public boolean step() {
44+
// System.out.println("Sorting array hash: " + System.identityHashCode(arr));
45+
didSwap = false;
46+
if (finished) return false;
47+
48+
if (i < arr.length - 1) {
49+
if (j < arr.length - i - 1) {
50+
51+
// Compare
52+
if (arr[j] > arr[j + 1]) {
53+
54+
swapA = j;
55+
swapB = j + 1;
56+
// Swap
57+
int temp = arr[j];
58+
arr[j] = arr[j + 1];
59+
arr[j + 1] = temp;
60+
61+
didSwap = true;
62+
63+
return true;
64+
}
65+
66+
j++;
67+
return false;
68+
} else {
69+
j = 0;
70+
i++;
71+
}
72+
} else {
73+
finished = true;
74+
}
75+
76+
77+
return false;
78+
}
79+
}

src/algorithms/InsertionSort.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package algorithms;
2+
3+
public class InsertionSort {
4+
}

src/algorithms/MergeSort.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package algorithms;
2+
3+
public class MergeSort {
4+
}

src/algorithms/SortAlgorithm.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package algorithms;
2+
3+
public interface SortAlgorithm {
4+
5+
/** Perform one step of the algorithm.
6+
* Return true if a swap occurred (so animations can trigger).
7+
*/
8+
boolean step();
9+
10+
/** Return true when the algorithm is finished. */
11+
boolean isFinished();
12+
13+
/** Return the currently active indices being compared. */
14+
int[] getActiveIndices();
15+
16+
/** Return the indices that were swapped on the last step. */
17+
int[] getSwapIndices();
18+
19+
int[] getArray();
20+
}

0 commit comments

Comments
 (0)