diff --git a/Brute Force/Bogo Sort/Code.java b/Brute Force/Bogo Sort/Code.java new file mode 100644 index 00000000..d36612c1 --- /dev/null +++ b/Brute Force/Bogo Sort/Code.java @@ -0,0 +1,76 @@ +// import libraries { +import org.algorithm_visualizer.*; +import java.util.*; +// } + +class Main { + // define tracer variables { + Array1DTracer arrayTracer = new Array1DTracer("Array"); + LogTracer logTracer = new LogTracer("Console"); + // } + + boolean isSorted(int[] array) { + for (int i = 0; i < array.length - 1; i++) { + if (array[i] > array[i + 1]) { + return false; + } + } + return true; + } + + int[] shuffle(int[] array) { + for (int i = 0; i < array.length; i++) { + int randomIndex = new Randomize.Integer(0, array.length - 1).create(); + int temp = array[i]; + array[i] = array[randomIndex]; + array[randomIndex] = temp; + } + return array; + } + + void bogosort(int[] array) { + while (!isSorted(array)) { + logTracer.println("Array not sorted: " + Arrays.toString(array)); + + // visualize { + arrayTracer.set(array); + Tracer.delay(); + // } + + logTracer.println("Shuffling array..."); + array = shuffle(array); + } + + logTracer.println("Array sorted: " + Arrays.toString(array)); + + // visualize { + arrayTracer.set(array); + for (int i = 0; i < array.length; i++) { + arrayTracer.select(i); + Tracer.delay(); + } + // } + } + + + Integer[] uniqueArray = (Integer[]) new Randomize.Array1D(4, new Randomize.Integer(1, 9)).create(); + + Main() { + int a = uniqueArray[0]; + int b = uniqueArray[1]; + int c = uniqueArray[2]; + int d = uniqueArray[3]; + + // visualize { + Layout.setRoot(new VerticalLayout(new Commander[]{arrayTracer, logTracer})); + arrayTracer.set(new int[]{a, b, c, d}); + Tracer.delay(); + // } + + bogosort(new int[]{a, b, c, d}); + } + + public static void main(String[] args) { + new Main(); + } +} diff --git a/Brute Force/Bogo Sort/README.md b/Brute Force/Bogo Sort/README.md new file mode 100644 index 00000000..d186fc90 --- /dev/null +++ b/Brute Force/Bogo Sort/README.md @@ -0,0 +1,11 @@ +# Bogo Sort + +Bogo Sort is a highly inefficient sorting algorithm that randomly shuffles the array until it is sorted. It works by checking if the array is sorted and, if not, randomly permuting the elements. This process repeats until the array is sorted, making it one of the worst sorting algorithms in terms of performance. + +## Complexity +| Name | Best | Average | Worst | Memory | Stable | Comments | +|-----------|--------|----------|---------|--------|--------|----------------------| +| BogoSort | n | (n+1)! | (n+1)! | 1 | No | Extremely inefficient | + +## References +- [Wikipedia](https://en.wikipedia.org/wiki/Bogosort)