Bogo Sort

Bogo Sort :

Author(s) : Unknown Date : Unknown

Bogo sort is a humorous (and inefficient) sorting algorithm that relies on randomly shuffling the elements and checking if the list is sorted after each shuffle. If it's not sorted, the shuffling continues. While theoretically, there's a chance the list might become sorted by random luck, bogo sort is not intended for practical use due to its unpredictable and potentially very slow execution time.

Time Complexity O(n * n!)
Best Case O(n)
Worst Case Infinite
Space Complexity O(1)
Stable No

Code Integration:

* the code might contain some bugs or may not work correctly

Python JavaScript Java Go C
def bogosort(arr):
	def is_sorted(arr):
		for i in range(len(arr) - 1):
			if arr[i] > arr[i + 1]:
				return False
		return True

	while not is_sorted(arr):
		random.shuffle(arr)
	return arr

# Test array
arr = [12, 11, 13, 5, 6, 7]
print('Sorted array is:', bogosort(arr.copy()))
function bogosort(arr) {
	function isSorted(arr) {
		for (let i = 0; i < arr.length - 1; i++) {
			if (arr[i] > arr[i + 1]) {
				return false;
			}
		}
		return true;
	}

	while (!isSorted(arr)) {
		arr.sort(() => Math.random() - 0.5);
	}
	return arr;
}

// Test array
const arr = [12, 11, 13, 5, 6, 7];
console.log('Sorted array is:', bogosort(arr.slice()));
public class BogoSort {
	public static boolean isSorted(int[] arr) {
		for (int i = 0; i < arr.length - 1; i++) {
			if (arr[i] > arr[i + 1]) {
				return false;
			}
		}
		return true;
	}

	public static void bogosort(int[] arr) {
		while (!isSorted(arr)) {
			shuffle(arr);
		}
	}

	private static void shuffle(int[] arr) {
		for (int i = 0; i < arr.length; i++) {
			int j = (int) (Math.random() * (arr.length - i)) + i;
			int temp = arr[i];
			arr[i] = arr[j];
			arr[j] = temp;
		}
	}

	public static void main(String[] args) {
		int[] arr = {12, 11, 13, 5, 6, 7};
		bogosort(arr);
		System.out.print("Sorted array is: ");
		for (int num : arr) {
			System.out.print(num + " ");
		}
	}
}
package main

import (
	"math/rand"
)

func isSorted(arr []int) bool {
	for i := 0; i < len(arr)-1; i++ {
		if arr[i] > arr[i+1] {
			return false
		}
	}
	return true
}

func bogosort(arr []int) []int {
	for !isSorted(arr) {
		rand.Shuffle(len(arr), func(i, j int) { arr[i], arr[j] = arr[j], arr[i] })
	}
	return arr
}

func main() {
	arr := []int{12, 11, 13, 5, 6, 7}
	sortedArr := bogosort(append([]int{}, arr...)) // copy the array
	println("Sorted array is:", sortedArr)
}
#include <stdio.h>
#include <stdlib.h>

int isSorted(int arr[], int n) {
	for (int i = 0; i < n - 1; i++) {
		if (arr[i] > arr[i + 1]) {
			return 0; // 0 indicates not sorted
		}
	}
	return 1; // 1 indicates sorted
}

void shuffle(int arr[], int n) {
	for (int i = 0; i < n - 1; i++) {
		int j = rand() % (n - i) + i;
		int temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}
}

void bogosort(int arr[], int n) {
	while (!isSorted(arr, n)) {
		shuffle(arr, n);
	}
}

int main() {
	int arr[] = {12, 11, 13, 5, 6, 7};
	int n = sizeof(arr) / sizeof(arr[0]);
	bogosort(arr, n);
	printf("Sorted array is: ");
	for (int i = 0; i < n; i++) {
		printf("%d ", arr[i]);
	}
	printf("\n");
	return 0;
}
light dark