diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..2a713cd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.jar +*.class + diff --git a/README.md b/README.md index bde68c9c..2abcf067 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,34 @@ # ArrayVisualizer -Sorting Visualizer with 6 different views and 14 included sorting algorithms +Sorting Visualizer/Audiolizer -Outdated videos: +## Videos +[Vertical Pyramid](https://www.youtube.com/watch?v=QOYcpGnHH0g) -Classic: -https://www.youtube.com/watch?v=QmOtL6pPcI0 +[Dynamic Hoops](https://www.youtube.com/watch?v=S0RtR2Yllzk) -Color Circle: -https://www.youtube.com/watch?v=IjIViETya5k +[TriMesh](https://www.youtube.com/watch?v=Zc__8qaLfJk) -Dots: -https://www.youtube.com/watch?v=zcO8uxg_Spw +[Horizontal Pyramid](https://www.youtube.com/watch?v=vmT3XUBoxiQ) -Spiral Dots: -https://www.youtube.com/watch?v=jrHLeKwMzfI +[Static Hoops](https://www.youtube.com/watch?v=jXs1y3tCKQg) + +[Christmas Tree](https://www.youtube.com/watch?v=xY1tiHzo8mE) + +[Variable Width TriMesh](https://www.youtube.com/watch?v=0tr6AtLu4pg) + +[Color Circle](https://www.youtube.com/watch?v=sVYtGyPiGik) + +# How to use + +### Build +``` +mkdir -p dist target +javac src/array/visualizer/ArrayVisualizer.java -sourcepath src -d target/ +jar -cvfm dist/ArrayVisualizer.jar manifest.mf -C target/ ./ +``` + +### Run +``` +java -jar dist/ArrayVisualizer.jar +``` +======= \ No newline at end of file diff --git a/dist/ArrayVisualizer.jar b/dist/ArrayVisualizer.jar deleted file mode 100644 index 95178f7a..00000000 Binary files a/dist/ArrayVisualizer.jar and /dev/null differ diff --git a/manifest.mf b/manifest.mf new file mode 100644 index 00000000..83bd0727 --- /dev/null +++ b/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Created-By: 1.8.0_181 (Oracle Corporation) +Main-Class: array.visualizer.ArrayVisualizer diff --git a/src/array/visualizer/ArrayVisualizer.java b/src/array/visualizer/ArrayVisualizer.java index 63258d27..7e56aa62 100644 --- a/src/array/visualizer/ArrayVisualizer.java +++ b/src/array/visualizer/ArrayVisualizer.java @@ -66,7 +66,7 @@ public class ArrayVisualizer { static ArrayList COLORSTRAT2cols = new ArrayList(); static String[] ComparativeSorts = "Selection!Bubble!Insertion!Double Selection!Cocktail Shaker!Quick!Merge!Merge OOP!Weave Merge!Max Heap!Shell".split("!"); - static String[] DistributiveSorts = "Radix LSD!Radix MSD!Radix LSD In-Place!Gravity!Shatter!Counting!Time!Bogo".split("!"); + static String[] DistributiveSorts = "Radix LSD!Radix MSD!Radix LSD In-Place!Binary Quicksort!Gravity!Shatter!Counting!Time!Bogo".split("!"); static int cx = 0; static int cy = 0; @@ -657,6 +657,7 @@ public void run(){ new GravitySort(), new RadixLSD(4), new RadixMSD(4), + new BinaryQuickSort(), new RadixLSDInPlace(2), new RadixLSDInPlace(10)} ) @@ -766,14 +767,16 @@ public void run(){ case 2: sort = new RadixLSDInPlace(base);break; case 3: - sort = new GravitySort();break; + sort = new BinaryQuickSort();break; case 4: - sort = new ShatterSorts(base);break; + sort = new GravitySort();break; case 5: - sort = new CountingSort();break; + sort = new ShatterSorts(base);break; case 6: - sort = new TimeSort(base);break; + sort = new CountingSort();break; case 7: + sort = new TimeSort(base);break; + case 8: sort = new BogoSort();break; default: sort = null; break; diff --git a/src/array/visualizer/sort/BinaryQuickSort.java b/src/array/visualizer/sort/BinaryQuickSort.java new file mode 100644 index 00000000..a06f7522 --- /dev/null +++ b/src/array/visualizer/sort/BinaryQuickSort.java @@ -0,0 +1,135 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package array.visualizer.sort; + +import array.visualizer.ArrayController; + +import static array.visualizer.ArrayVisualizer.*; +import static array.visualizer.utils.Analysis.*; +import static array.visualizer.utils.Swaps.*; +import java.util.Queue; +import java.util.LinkedList; + +/** + * Binary MSD Radix Sort / Binary Quicksort. + * + * Implemented as recursive decent, and via task queue, see: + * * binaryQuickSortRecursive, and + * * binaryQuickSort respectively. + * + * Both of which are in-place sorting algorithms, with the recursive utilizing + * the stack for divide-and-conquer, while the non-recursive utilizes a queue. + * + * Can be extended to support unsigned integers, by sorting the first bit rin + * reverse. Can be made stable at the cost of O(n) memory. Can be parallalized + * to O(log2(n)) subtasks / threads. + * + * @author Skeen + */ +public class BinaryQuickSort implements Sort { + + public static boolean getBit(int n, int k) { + // Find boolean value of bit k in n + return ((n >> k) & 1) == 1; + } + + public static int analyzeBit(final ArrayController ac) { + // Find highest bit of highest value + int max = 0; + for(int i = 0; i < ac.length; i++){ + ac.marked.set(1, i); + ac.aa++; + sleep(1); + max = Math.max(max, ac.array[i]); + } + return 31 - Integer.numberOfLeadingZeros(max); + } + + public static class Task { + public int p; + public int r; + public int bit; + + public Task(int p, int r, int bit) + { + this.p = p; + this.r = r; + this.bit = bit; + } + } + + public static void binaryQuickSortRecursive(final ArrayController ac, int p, int r, int bit) + { + if (p < r && bit >= 0) + { + int q=partition(ac, p, r, bit); + sleep(1); + binaryQuickSortRecursive(ac, p, q, bit-1); + binaryQuickSortRecursive(ac, q+1, r, bit-1); + } + } + + public static void binaryQuickSort(final ArrayController ac, int p, int r, int bit) + { + Queue tasks = new LinkedList(); + tasks.add(new Task(p, r, bit)); + + while (tasks.isEmpty() == false) + { + Task task = tasks.remove(); + if (task.p < task.r && task.bit >= 0) + { + int q=partition(ac, task.p, task.r, task.bit); + sleep(1); + tasks.add(new Task(task.p, q, task.bit-1)); + tasks.add(new Task(q+1, task.r, task.bit-1)); + } + } + } + + public static int partition(final ArrayController ac, int p, int r, int bit) + { + int i = p - 1; + int j = r + 1; + + while (true) { + // Left is not set + i++; + while(i < r && !getBit(ac.array[i], bit)) { + i++; + ac.marked.set(1, i); + sleep(0.45); + ac.comps+=2; + } + // Right is set + j--; + while(j > p && getBit(ac.array[j], bit)) { + j--; + ac.marked.set(2, j); + sleep(0.45); + ac.comps+=2; + } + // If i is less than j, we swap, otherwise we are done + if (i < j) + swap(ac, i, j); + else + return j; + } + } + + @Override + public String name() + { + return "Binary Quicksort"; + } + + @Override + public void sort(ArrayController ac) + { + int msb = analyzeBit(ac); + binaryQuickSort(ac, 0, ac.length-1, msb); + // binaryQuickSortRecursive(ac, 0, ac.length-1, msb); + } +}