BozoSort Definitive (C++, C#, VB.NET, Java, PHP)

WARNING: PLEASE USE WITH EXTREME CAUTION! CONTENTS MAY BE FUNNY BUT NOT FOR PRODUCTION USE. Programmer Discretion is Advised!

Hey you bozos out there, now there is a sort even for you! For those who don’t know, a Bozo Sort is a joke sorting algorithm that actually sorts but at horrible timing and is very inefficient. It is good to see it at work from a theoretical stand point but it should never be used in production systems (only bozos would do that). As you may, or may not know, I like to throw up the occasional joke entry once in awhile to give newbies and experts alike a glimpse into the wacky side of programming. This entry was inspired by a guy recently asking about Bozo Sorting on the boards, so I thought I would bring it to you for a chuckle…. right here on the Programming Underground!

As part of my on going series called “The Definitive Series” I construct the highly INEFFICIENT bozo sort and port it across five major programming languages. This is a true sorting algorithm and will work… eventually. It is closely related to the BogoSort which is also an extremely crappy algorithm as well. With Bogo, imagine having a deck of cards and throwing them up in the air and picking them up off the floor in random order…testing to see if they are sorted. If they are not, throw them back in the air and do it all over again. Eventually you will pick up the cards in sorted order…. even if it may take you days, months or years!

The BozoSort modifies this idea a little. It picks two random numbers from our set of values and simply swaps them. Then tests if the array is now sorted. If not, it swaps two more random values and retests.

Use this joke gag of an algorithm on your colleague at work or your boss who is desk checking your code. See if they recognize it or even catch it. Just make sure it never gets to production or you may get in trouble!

For our examples we use a small array of five numbers, so most of the time it will take the computer only a few seconds to actually find the correct sequence to have it sorted. But as you increase the size of the array you can exponentially increase the time it takes to find the sort.

Lets take a look at the C++ shall we?

#include <iostream>
#include <ctime>
using namespace std;

// Prototype declarations
void BozoSort(int [], int);
bool isSorted(int [], int);

int main()
{
	int arMyValues[5] = { 3, 2, 1, 5, 4 };
	BozoSort(arMyValues,5);

	cout << "Array sorted... you bozo!" << endl;

	// Loop through the array and show it sorted.
	for (int i = 0; i < 5; i++) {
		cout << "Element: " << i << " - " << arMyValues[i] << endl;
	}

	return 0;
}

// BozoSort Algorithm
// Warning: Highly inefficient and not recommended for large arrays.
// Use with caution, it is for bozos after all!

void BozoSort(int arValues[], int size) {
	int slot1 = 0;
	int slot2 = 0;
	int temp;

	// Seed our random number generator
	srand(time(NULL));

	// Continue until sorted
	while (!isSorted(arValues,size)) {
		// Pick two values at random.
		slot1 = rand() % size;
		slot2 = rand() % size;

		// Swap the values and go for a retest.
		temp = arValues[slot1];
		arValues[slot1] = arValues[slot2];
		arValues[slot2] = temp;
	}
}


// Loop through the array and determine if one value is larger
// than the one after it. If it is, then it isn't sorted.
// Returns true if the array is sorted.

bool isSorted(int arValues[], int size) {
	for (int i = 0; i < size - 1; i++) {
		if (arValues[i] > arValues[i + 1]) {
			return false;
		}
	}
	return true;
}

The basic idea is to have one function which does the looping, number picking, and swapping. The other function called “isSorted” simply checks if the array is sorted or not by comparing the values in the array. As you can see, it is not extremely complicated, but can be a pain in the neck to whoever seriously uses this code.

Here is a glance at it in C#.

public partial class Form1 : Form {
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			// Create our example array and sort it.
			int[] myValues = { 3, 2, 1, 5, 4 };
			BozoSort(myValues);

			MessageBox.Show("Array sorted you bozo!");

			// Loop through and add our sorted values to listbox.
			foreach (int val in myValues) {
				listBox1.Items.Add(val);
			}
		}

		private void BozoSort(int[] arValues) {
			int slot1 = 0;
			int slot2 = 0;
			int temp;

			// Create our random number generator.
			Random rand = new Random();

			// Continue until sorted
			while (!isSorted(arValues)) {
						// Pick two values at random.
						slot1 = rand.Next(0,arValues.Length);
						slot2 = rand.Next(0,arValues.Length);

						// Swap the values and go for a retest.
						temp = arValues[slot1];
						arValues[slot1] = arValues[slot2];
						arValues[slot2] = temp;
			}
		}

		// Loop through the array and determine if one value is larger
		// than the one after it. If it is, then it isn't sorted.
		// Returns true if the array is sorted.

		private bool isSorted(int[] arValues) {
			for (int i = 0; i < arValues.Length - 1; i++) {
						if (arValues[i] > arValues[i + 1]) {
									return false;
						}
			}
			return true;
		}
}

Again, not too different from its C++ counterpart. The main changes are handling the length of the array and the use of a randomize object for random number generating. We also dump the values to a listbox for display and a great messagebox to tell the user they are simply a bozo. Classic!

Onwards to VB.NET!

Public Class Form1

	Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
		Dim myValues() As Integer = {3, 2, 1, 5, 4}
		BozoSort(myValues)

		MessageBox.Show("Array sorted you bozo!")

		' Loop through and add our sorted values to listbox.
		For Each val As Integer In myValues
			listBox1.Items.Add(val)
		Next
	End Sub

	Private Sub BozoSort(ByRef arValues() As Integer)
		Dim slot1 As Integer = 0
		Dim slot2 As Integer = 0
		Dim temp As Integer = 0

		' Continue until sorted
		Do While Not isSorted(arValues)
			' Pick two values at random.
			slot1 = New System.Random().Next(0, arValues.Length)
			slot2 = New System.Random().Next(0, arValues.Length)

			' Swap the values and go for a retest.
			temp = arValues(slot1)
			arValues(slot1) = arValues(slot2)
			arValues(slot2) = temp
		Loop
	End Sub

	' Loop through the array and determine if one value is larger
	' than the one after it. If it is, then it isn't sorted.
	' Returns true if the array is sorted.

	Private Function isSorted(ByRef arValues() As Integer) As Boolean
		Dim i As Integer

		' Do to for looping being inclusive of the end value, we subtract 2.
		For i = 0 To arValues.Length - 2
			If arValues(i) > arValues(i + 1) Then
				Return False
			End If
		Next

		Return True
	End Function
End Class

Here is a mirror image of its C# cousin. As with any VB.NET syntax, it is a bit different but the principles are still there. One thing to watch out for is handling the end value of the array. In VB it is inclusive when you use for loops so you have to subtract 2 to make sure it stays in step with the C language variants.

Attack of the Java!

import java.util.Random;

public class GoBozo {
	public static void main(String args[]) {
		int[] arMyValues = { 3, 2, 1, 5, 4 };
		BozoSort(arMyValues);

		System.out.println("Array sorted... you bozo!");

		// Loop through the array and show it sorted.
		for (int i = 0; i < 5; i++) {
			System.out.println("Element: " + i + " - " + arMyValues[i]);
		} 
	}

	// BozoSort Algorithm
	// Warning: Highly inefficient and not recommended for large arrays.
	// Use with caution, it is for bozos after all!

	private static void BozoSort(int[] arValues) {
		int slot1 = 0;
		int slot2 = 0;
		int temp;

		Random rand = new Random();

		// Continue until sorted
		while (!isSorted(arValues)) {
			// Pick two values at random.
			slot1 = rand.nextInt(arValues.length);
			slot2 = rand.nextInt(arValues.length);

			// Swap the values and go for a retest.
			temp = arValues[slot1];
			arValues[slot1] = arValues[slot2];
			arValues[slot2] = temp;
		}
	}


	// Loop through the array and determine if one value is larger
	// than the one after it. If it is, then it isn't sorted.
	// Returns true if the array is sorted.

	private static boolean isSorted(int[] arValues) {
		for (int i = 0; i < arValues.length - 1; i++) {
			if (arValues[i] > arValues[i + 1]) {
				return false;
			}
		}

		return true;
	}
}

Java came straight out of C++ so as you can imagine the syntax is very similar. It has evolved though to include a Random object here which you must import using the java.util package. Call its nextInt() function to get the random numbers we need.

Lastly, PHP for all you web gurus!

<?php

// Setup our test example and call our sort
$myValues = array(3, 2, 1, 5, 4);
BozoSort($myValues);

// Print the results
echo "Array is sorted you bozo!";

for ($i = 0; $i < count($myValues); $i++) {
	echo "Element: $i - {$myValues[$i]} <br/>";
}

function BozoSort(&$arValues) {
	$slot1 = 0;
	$slot2 = 0;

	// Continue until sorted
	while (!isSorted($arValues)) {
		// Pick two values at random.
		$slot1 = rand(0,count($arValues) - 1);
		$slot2 = rand(0,count($arValues) - 1);

		// Swap the values and go for a retest.
		$temp = $arValues[$slot1];
		$arValues[$slot1] = $arValues[$slot2];
		$arValues[$slot2] = $temp;
	}
}

// Loop through the array and determine if one value is larger
// than the one after it. If it is, then it isn't sorted.
// Returns true if the array is sorted.

function isSorted($arValues) {
	for ($i = 0; $i < count($arValues) - 1; $i++) {
		if ($arValues[$i] > $arValues[$i + 1]) {
			return false;
		}
	}
	return true;
}

?>

Ok, there we go. PHP models itself after the C++ variant here but with a slight twist with variables and function signatures. So look out for that and if you find your web page taking a little long at times, know it is the inefficiency of the algorithm at work.

So there is our definitive series for the ever so crappy, yet funny, bozoSort algorithm! Wasn’t it funny? Yeah I know, a bit nerdy to be tooooo funny. But enjoy and I hope you keep reading. We do carry not so funny, but interesting, content on occasion. So keep checking back!

Thanks! 🙂

About The Author

Martyr2 is the founder of the Coders Lexicon and author of the new ebooks "The Programmers Idea Book" and "Diagnosing the Problem" . He has been a programmer for over 20 years. He works for a hot application development company in Vancouver Canada which service some of the biggest tech companies in the world. He has won numerous awards for his mentoring in software development and contributes regularly to several communities around the web. He is an expert in numerous languages including .NET, PHP, C/C++, Java and more.