Skip to content

Bogo sort added #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions Brute Force/Bogo Sort/Code.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
11 changes: 11 additions & 0 deletions Brute Force/Bogo Sort/README.md
Original file line number Diff line number Diff line change
@@ -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)