mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
Formatted with Google Java Formatter
This commit is contained in:
@@ -2,53 +2,51 @@ package Sorts;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
/**
|
||||
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
||||
* @see SortAlgorithm
|
||||
*/
|
||||
public class BogoSort implements SortAlgorithm {
|
||||
|
||||
private static final Random random = new Random();
|
||||
private static final Random random = new Random();
|
||||
|
||||
|
||||
private static <T extends Comparable<T>> boolean isSorted(T[] array) {
|
||||
for (int i = 0; i < array.length - 1; i++) {
|
||||
if (SortUtils.less(array[i + 1], array[i])) return false;
|
||||
}
|
||||
return true;
|
||||
private static <T extends Comparable<T>> boolean isSorted(T[] array) {
|
||||
for (int i = 0; i < array.length - 1; i++) {
|
||||
if (SortUtils.less(array[i + 1], array[i])) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Randomly shuffles the array
|
||||
private static <T> void nextPermutation(T[] array) {
|
||||
int length = array.length;
|
||||
// Randomly shuffles the array
|
||||
private static <T> void nextPermutation(T[] array) {
|
||||
int length = array.length;
|
||||
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
int randomIndex = i + random.nextInt(length - i);
|
||||
SortUtils.swap(array, randomIndex, i);
|
||||
}
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
int randomIndex = i + random.nextInt(length - i);
|
||||
SortUtils.swap(array, randomIndex, i);
|
||||
}
|
||||
}
|
||||
|
||||
public <T extends Comparable<T>> T[] sort(T[] array) {
|
||||
while (!isSorted(array)) {
|
||||
nextPermutation(array);
|
||||
}
|
||||
return array;
|
||||
public <T extends Comparable<T>> T[] sort(T[] array) {
|
||||
while (!isSorted(array)) {
|
||||
nextPermutation(array);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
// Driver Program
|
||||
public static void main(String[] args) {
|
||||
// Integer Input
|
||||
Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
|
||||
// Driver Program
|
||||
public static void main(String[] args) {
|
||||
// Integer Input
|
||||
Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
|
||||
|
||||
BogoSort bogoSort = new BogoSort();
|
||||
BogoSort bogoSort = new BogoSort();
|
||||
|
||||
// print a sorted array
|
||||
SortUtils.print(bogoSort.sort(integers));
|
||||
// print a sorted array
|
||||
SortUtils.print(bogoSort.sort(integers));
|
||||
|
||||
// String Input
|
||||
String[] strings = {"c", "a", "e", "b", "d"};
|
||||
// String Input
|
||||
String[] strings = {"c", "a", "e", "b", "d"};
|
||||
|
||||
SortUtils.print(bogoSort.sort(strings));
|
||||
}
|
||||
SortUtils.print(bogoSort.sort(strings));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user