/// Bogo排序法(遞增)。
pub fn bogo_sort(array: &mut [i32]) {
let length_dec = array.len() - 1;
let mut rng = rand::thread_rng();
loop {
let mut sorted = true;
for (i, n) in array.iter().copied().enumerate().take(length_dec) {
if n > array[i + 1] {
sorted = false;
break;
}
}
if sorted {
break;
}
<[i32] as rand::seq::SliceRandom>::shuffle(array, &mut rng);
}
}
/// Bogo排序法(遞減)。
pub fn bogo_sort_desc(array: &mut [i32]) {
let length_dec = array.len() - 1;
let mut rng = rand::thread_rng();
loop {
let mut sorted = true;
for (i, n) in array.iter().copied().enumerate().take(length_dec) {
if n < array[i + 1] {
sorted = false;
break;
}
}
if sorted {
break;
}
<[i32] as rand::seq::SliceRandom>::shuffle(array, &mut rng);
}
}
使用了rand套件來完成打亂切片元素順序的功能。
/**
* Bogo排序法(遞增)。
*/
public static void bogoSort(final int[] array) {
final int lengthDec = array.length - 1;
final Random random = new Random();
while (true) {
boolean sorted = true;
for (int i = 0; i < lengthDec; ++i) {
if (array[i] > array[i + 1]) {
sorted = false;
break;
}
}
if (sorted) {
break;
}
for (int i = lengthDec; i > 0; --i) {
final int index = random.nextInt(i + 1);
int temp = array[index];
array[index] = array[i];
array[i] = temp;
}
}
}
/**
* Bogo排序法(遞減)。
*/
public static void bogoSortDesc(final int[] array) {
final int lengthDec = array.length - 1;
final Random random = new Random();
while (true) {
boolean sorted = true;
for (int i = 0; i < lengthDec; ++i) {
if (array[i] < array[i + 1]) {
sorted = false;
break;
}
}
if (sorted) {
break;
}
for (int i = lengthDec; i > 0; --i) {
final int index = random.nextInt(i + 1);
int temp = array[index];
array[index] = array[i];
array[i] = temp;
}
}
}
/**
* Bogo排序法(遞增)。
*/
function bogoSort(array: number[]) {
const lengthDec = array.length - 1;
for (; ;) {
let sorted = true;
for (let i = 0;i < lengthDec;i++) {
if (array[i] > array[i + 1]) {
sorted = false;
break;
}
}
if (sorted) {
break;
}
for (let i = lengthDec;i > 0;i--) {
const index = Math.floor(Math.random() * (i + 1));
const temp = array[index];
array[index] = array[i];
array[i] = temp;
}
}
}
/**
* Bogo排序法(遞減)。
*/
function bogoSortDesc(array: number[]) {
const lengthDec = array.length - 1;
for (; ;) {
let sorted = true;
for (let i = 0;i < lengthDec;i++) {
if (array[i] < array[i + 1]) {
sorted = false;
break;
}
}
if (sorted) {
break;
}
for (let i = lengthDec;i > 0;i--) {
const index = Math.floor(Math.random() * (i + 1));
const temp = array[index];
array[index] = array[i];
array[i] = temp;
}
}
}
// Bogo排序法(遞增)。
func BogoSort(array []int) {
length := len(array)
lengthDec := length - 1
rand.Seed(time.Now().UnixNano())
for {
sorted := true
for i := 0; i < lengthDec; i += 1 {
if array[i] > array[i+1] {
sorted = false
break
}
}
if sorted {
break
}
rand.Shuffle(length, func(i, j int) {
array[i], array[j] = array[j], array[i]
})
}
}
// Bogo排序法(遞減)。
func BogoSortDesc(array []int) {
length := len(array)
lengthDec := length - 1
rand.Seed(time.Now().UnixNano())
for {
sorted := true
for i := 0; i < lengthDec; i += 1 {
if array[i] < array[i+1] {
sorted = false
break
}
}
if sorted {
break
}
rand.Shuffle(length, func(i, j int) {
array[i], array[j] = array[j], array[i]
})
}
}