Bogo Sort / Permutation Sort Program with Explanation and Code
2025-11-19
bogo sort
permutation sort
stupid sort
random sorting
inefficient sorting algorithms
python bogo sort
c bogo sort
javascript bogo sort
php bogo sort
csharp bogo sort
worst sorting methods
Bogo Sort (Permutation Sort) is a highly inefficient sorting algorithm that repeatedly shuffles the array until it becomes sorted. This page explains the concept and provides implementations in Python, C, JavaScript, PHP, and C#.
Program Explanation
What is BogoSort / Permutation Sort?
Bogo Sort (also known as Permutation Sort or Stupid Sort) is a highly inefficient sorting algorithm based on the idea of generating random permutations of the array until it accidentally becomes sorted. It continuously shuffles the elements and checks whether they are sorted. If not, it shuffles again. Although simple to understand, Bogo Sort has terrible performance and is mainly used for educational or humorous purposes.
Steps to Solve
- Check if the array is sorted.
- If the array is sorted, stop.
- If not sorted, generate a random permutation (shuffle the array).
- Repeat the process until the array becomes sorted.
- Output the sorted array.
Key Points
- Time complexity: O((n+1)!) — extremely slow.
- Not suitable for real-world use.
- Demonstrates algorithmic inefficiency and randomness concepts.
- Works only for small arrays due to factorial growth.
- Simple implementation using random shuffling.
Program Code
import random
def is_sorted(arr):
return all(arr[i] <= arr[i+1] for i in range(len(arr)-1))
def bogo_sort(arr):
while not is_sorted(arr):
random.shuffle(arr)
arr = list(map(int, input("Enter numbers: ").split()))
bogo_sort(arr)
print("Sorted array:", arr)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int isSorted(int arr[], int n) {
for (int i = 0; i < n - 1; i++)
if (arr[i] > arr[i + 1])
return 0;
return 1;
}
void shuffle(int arr[], int n) {
for (int i = 0; i < n; i++) {
int j = rand() % n;
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() {
srand(time(0));
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
bogoSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
function isSorted(arr) {
for (let i = 0; i < arr.length - 1; i++)
if (arr[i] > arr[i + 1]) return false;
return true;
}
function shuffle(arr) {
for (let i = 0; i < arr.length; i++) {
let j = Math.floor(Math.random() * arr.length);
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
function bogoSort(arr) {
while (!isSorted(arr)) shuffle(arr);
}
let arr = prompt("Enter numbers:").split(" ").map(Number);
bogoSort(arr);
console.log("Sorted array:", arr);
using System;
class Program {
static bool IsSorted(int[] arr) {
for (int i = 0; i < arr.Length - 1; i++)
if (arr[i] > arr[i + 1])
return false;
return true;
}
static void Shuffle(int[] arr) {
Random rand = new Random();
for (int i = 0; i < arr.Length; i++) {
int j = rand.Next(arr.Length);
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
static void BogoSort(int[] arr) {
while (!IsSorted(arr))
Shuffle(arr);
}
static void Main() {
Console.Write("Enter numbers: ");
int[] arr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
BogoSort(arr);
Console.Write("Sorted array: ");
foreach (int x in arr)
Console.Write(x + " ");
}
}
<?php
function isSorted($arr) {
for ($i = 0; $i < count($arr) - 1; $i++)
if ($arr[$i] > $arr[$i + 1]) return false;
return true;
}
function shuffleArray(&$arr) {
for ($i = 0; $i < count($arr); $i++) {
$j = rand(0, count($arr) - 1);
$temp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $temp;
}
}
function bogoSort(&$arr) {
while (!isSorted($arr))
shuffleArray($arr);
}
$arr = array_map('intval', explode(" ", readline("Enter numbers: ")));
bogoSort($arr);
echo "Sorted array: " . implode(" ", $arr);
?>
