Learn the Bogo Sort Algorithm

Last updated by Dipen Dadhaniya on Dec 18, 2025 at 11:08 AM
| Reading Time: 3 minutes

Sorting algorithms are a must-know for any software engineer preparing for a technical interview. They will help you crack many questions during your coding round.

Bogo sort, while not necessarily a useful or effective sorting algorithm, is an interesting algorithm to learn in theory. In this article, we’ll give you a refresher on the bogo sort algorithm:

  • What Is Bogo Sort?
  • How Does Bogo Sort Work?
  • Bogo Sort Algorithm
  • Bogo Sort Pseudocode
  • Bogo Sort Code
  • Bogo Sort Complexities
  • Advantages of Bogo Sort
  • Disadvantages of Bogo Sort
  • FAQs on Bogo Sort

What Is Bogo Sort?

Bogo sort is an algorithm used to sort the elements of an array by randomly generating different permutations of an array and then checking whether it is sorted or not. It’s based on the generate-and-test paradigm. Other names for bogo sort include permutation sort, stupid sort, slow sort, shotgun sort, or monkey sort.

Although bogo sort is a highly inefficient sorting algorithm, it finds some use in quantum computing — check out “Quantum Bogo Sort” if you’re intrigued.

How Does Bogo Sort Work?

Let’s assume that the array Arr[] = {2, 3, 1, 2, 4}  is to be sorted using bogo sort.

First, we check if the array is already sorted (which it isn’t, in the above case).

We then generate a random permutation of the array and check again if the array is sorted or not. This is repeated until we get a sorted array.

For example:

<figure “>

In the above example, we got the sorted array in the third permutation, However, this might not be the case always. The number of permutations or steps taken to get the sorted array may be anywhere between 1 and infinity.

There’s no guarantee that we will get the sorted array after shuffling the array a certain number of times. However, the probability of it running infinitely is very low. Given enough time, bogo sort will eventually return the sorted array.

Also note, permutations 1 and 2 seen in the above example will differ every time the program is run, even for the same array, as they are generated randomly.

Bogo Sort Algorithm

Repeat the below two steps until the array is sorted:

Step 1: Check if the array is already sorted or not. If yes, then print the array, else
Step 2: Generate a random permutation (not necessarily different from previously generated) and go to Step 1.

Bogo Sort  Pseudocode

Bogo sort being a simple method, the pseudocode for it is just three lines!

while  is_sorted(Arr) is false do

random_shuffle(Arr)

return Arr

Bogo Sort Code

We’ve used C++ to demonstrate how bogo sort works. Use this as a reference code to code in C, Java, Python, or any programming language of your choice.

#include<bits/stdc++.h>

using namespace std;

bool isSorted(int Arr[], int N)

{

for(int i=1; i<N; i++)

{

if(Arr[i] < Arr[i-1])

return false;

}

return true;

}

 

void randomly_shuffle(int Arr[], int N)

{

for(int i = 0; i < N; i++)

{

swap(Arr[i], Arr[rand() % N]);

}

}

 

int main()

{

int N = 4;

int Arr[N] = {2, 13, 7, 1};

 

// isSorted() function return true if array

// is sorted else it returns false

while(!isSorted(Arr, N))

{

// randomly_shuffle() function generates a

// random permutation of array

randomly_shuffle(Arr, N);

}

 

for(int i = 0; i < N; i++)

cout << Arr[i] << ” “;

 

return 0;

}

Output:

1 2 7 13

Bogo Sort Complexities

Time Complexity

  • Worst-case time complexity: O(infinite)

It is not guaranteed that we get the sorted permutation at some point after shuffling the array a certain number of times. Although its probability is infinitesimally low, there’s a chance that we do not get the sorted order even after gazillions of shuffling.

  • Average-case time complexity: O(n! * n)

There are n! permutations, only one of which is sorted. So, we would expect to get the correct answer after about O(n!) iterations. And each shuffle/check operation is itself O(n).

  • Best-case time complexity: O(n)

When the given array is already sorted, the program terminates just after checking if the array is sorted once, which takes O(n) time to execute.

Space Complexity

Bogo sort algorithm does not require any extra space for sorting the input array.

Thus, its auxiliary space is O(1).

FAQs on Bogo Sort

Question 1: Is bogo sort stable?

Answer: No, bogo sort is not an example of a stable sorting algorithm, as there is no guarantee that randomly generated sorted permutations have the same relative order of elements of the same value as in the input or not.

Question 2: Is bogo sort an in-place sorting algorithm?

Answer: Yes, bogo sort is an in-place sorting algorithm as it takes only O(1) auxiliary space.

Question3: Why is using bogo sort not practical?

Answer: Since the worst-case time complexity of bogo sort is O(infinite), there is no practical use of bogo sort.

Are You Ready to Nail Your Next Coding Interview?

Sorting algorithms interview questions feature in almost every coding interview for software developers. If you’re looking for guidance and help to nail these questions and more, sign up for our free webinar. 

As pioneers in the field of technical interview prep, we have trained thousands of software engineers to crack the toughest coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!

Sign up now!

———-

Article contributed by Abhinav Tiwari

Last updated on: December 18, 2025
Register for our webinar

Uplevel your career with AI/ML/GenAI

Loading_icon
Loading...
1 Enter details
2 Select webinar slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

Strange Tier-1 Neural “Power Patterns” Used By 20,013 FAANG Engineers To Ace Big Tech Interviews

100% Free — No credit card needed.

Register for our webinar

Uplevel your career with AI/ML/GenAI

Loading_icon
Loading...
1 Enter details
2 Select webinar slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

Ready to Enroll?

Get your enrollment process started by registering for a Pre-enrollment Webinar with one of our Founders.

Next webinar starts in

00
DAYS
:
00
HR
:
00
MINS
:
00
SEC

Register for our webinar

How to Nail your next Technical Interview

Loading_icon
Loading...
1 Enter details
2 Select slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

Almost there...
Share your details for a personalised FAANG career consultation!
Your preferred slot for consultation * Required
Get your Resume reviewed * Max size: 4MB
Only the top 2% make it—get your resume FAANG-ready!

Registration completed!

🗓️ Friday, 18th April, 6 PM

Your Webinar slot

Mornings, 8-10 AM

Our Program Advisor will call you at this time

Register for our webinar

Transform Your Tech Career with AI Excellence

Transform Your Tech Career with AI Excellence

Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills

25,000+ Professionals Trained

₹23 LPA Average Hike 60% Average Hike

600+ MAANG+ Instructors

Webinar Slot Blocked

Register for our webinar

Transform your tech career

Transform your tech career

Learn about hiring processes, interview strategies. Find the best course for you.

Loading_icon
Loading...
*Invalid Phone Number

Used to send reminder for webinar

By sharing your contact details, you agree to our privacy policy.
Choose a slot

Time Zone: Asia/Kolkata

Choose a slot

Time Zone: Asia/Kolkata

Build AI/ML Skills & Interview Readiness to Become a Top 1% Tech Pro

Hands-on AI/ML learning + interview prep to help you win

Switch to ML: Become an ML-powered Tech Pro

Explore your personalized path to AI/ML/Gen AI success

Your preferred slot for consultation * Required
Get your Resume reviewed * Max size: 4MB
Only the top 2% make it—get your resume FAANG-ready!
Registration completed!
🗓️ Friday, 18th April, 6 PM
Your Webinar slot
Mornings, 8-10 AM
Our Program Advisor will call you at this time

Get tech interview-ready to navigate a tough job market

Best suitable for: Software Professionals with 5+ years of exprerience
Register for our FREE Webinar

Next webinar starts in

00
DAYS
:
00
HR
:
00
MINS
:
00
SEC

Your PDF Is One Step Away!

The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants

The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer

The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary