From bfa00341d0eb560b6f184a55a22558bf401f6e9a Mon Sep 17 00:00:00 2001 From: TheiaDraizer <57834094+TheiaDraizer@users.noreply.github.com> Date: Thu, 19 Dec 2024 22:36:53 +0100 Subject: [PATCH 1/4] Create Code.java --- Brute Force/Bogo Sort/Code.java | 79 +++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Brute Force/Bogo Sort/Code.java diff --git a/Brute Force/Bogo Sort/Code.java b/Brute Force/Bogo Sort/Code.java new file mode 100644 index 00000000..1a5deeb2 --- /dev/null +++ b/Brute Force/Bogo Sort/Code.java @@ -0,0 +1,79 @@ +// 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(); + } + // } + } + + // Creating a unique array with random integers + Integer[] uniqueArray = (Integer[]) new Randomize.Array1D(4, new Randomize.Integer(1, 9)).create(); + + // Constructor + 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(); + // } + + // Call the bogosort function + bogosort(new int[]{a, b, c, d}); + } + + // Main method to run the program + public static void main(String[] args) { + new Main(); + } +} From 70121070814f0d1f1a41095454ad19909223ec49 Mon Sep 17 00:00:00 2001 From: TheiaDraizer <57834094+TheiaDraizer@users.noreply.github.com> Date: Thu, 19 Dec 2024 22:40:36 +0100 Subject: [PATCH 2/4] Create README.md --- Brute Force/Bogo Sort/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Brute Force/Bogo Sort/README.md diff --git a/Brute Force/Bogo Sort/README.md b/Brute Force/Bogo Sort/README.md new file mode 100644 index 00000000..236b812e --- /dev/null +++ b/Brute Force/Bogo Sort/README.md @@ -0,0 +1,17 @@ +# BogoSort +BogoSort is an inefficient sorting algorithm that randomly shuffles an array until it is sorted. + +### How it works: +1. Check if the array is sorted. +2. If not, shuffle the array randomly. +3. Repeat until the array is sorted. + +### Time Complexity: +- **Worst-case**: O((n+1)!) due to the random permutations. +- **Average-case**: O((n+1)!) as it has no systematic approach. + +### Practical use: +Rarely used in practice, typically for educational or humorous purposes. + +## References +- [Wikipedia](https://en.wikipedia.org/wiki/Bogosort) From c08ab4db4446f28dc4daa4f1adea9665536527ae Mon Sep 17 00:00:00 2001 From: TheiaDraizer <57834094+TheiaDraizer@users.noreply.github.com> Date: Thu, 19 Dec 2024 22:42:24 +0100 Subject: [PATCH 3/4] Update Code.java --- Brute Force/Bogo Sort/Code.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Brute Force/Bogo Sort/Code.java b/Brute Force/Bogo Sort/Code.java index 1a5deeb2..d36612c1 100644 --- a/Brute Force/Bogo Sort/Code.java +++ b/Brute Force/Bogo Sort/Code.java @@ -52,10 +52,9 @@ void bogosort(int[] array) { // } } - // Creating a unique array with random integers + Integer[] uniqueArray = (Integer[]) new Randomize.Array1D(4, new Randomize.Integer(1, 9)).create(); - // Constructor Main() { int a = uniqueArray[0]; int b = uniqueArray[1]; @@ -68,11 +67,9 @@ void bogosort(int[] array) { Tracer.delay(); // } - // Call the bogosort function bogosort(new int[]{a, b, c, d}); } - // Main method to run the program public static void main(String[] args) { new Main(); } From bd7ba0e164a80005f9f08c11803ba25a751bfd7a Mon Sep 17 00:00:00 2001 From: TheiaDraizer <57834094+TheiaDraizer@users.noreply.github.com> Date: Thu, 19 Dec 2024 22:47:06 +0100 Subject: [PATCH 4/4] Update README.md --- Brute Force/Bogo Sort/README.md | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/Brute Force/Bogo Sort/README.md b/Brute Force/Bogo Sort/README.md index 236b812e..d186fc90 100644 --- a/Brute Force/Bogo Sort/README.md +++ b/Brute Force/Bogo Sort/README.md @@ -1,17 +1,11 @@ -# BogoSort -BogoSort is an inefficient sorting algorithm that randomly shuffles an array until it is sorted. +# Bogo Sort -### How it works: -1. Check if the array is sorted. -2. If not, shuffle the array randomly. -3. Repeat until the array is sorted. +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. -### Time Complexity: -- **Worst-case**: O((n+1)!) due to the random permutations. -- **Average-case**: O((n+1)!) as it has no systematic approach. - -### Practical use: -Rarely used in practice, typically for educational or humorous purposes. +## 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)