Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
TheiaDraizer wants to merge4 commits intoalgorithm-visualizer:master
base:master
Choose a base branch
Loading
fromTheiaDraizer:Bogo-sort
Open
Show file tree
Hide file tree
Changes fromall 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 deletionsBrute Force/Bogo Sort/Code.java
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsBrute Force/Bogo Sort/README.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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)

[8]ページ先頭

©2009-2025 Movatter.jp