Skip to content

Instantly share code, notes, and snippets.

@albertofem
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albertofem/f418817ff2944977b414 to your computer and use it in GitHub Desktop.
Save albertofem/f418817ff2944977b414 to your computer and use it in GitHub Desktop.
Bogo sort
<?php
// usage: php bogosort.php 1,2,3,4,5
$items = explode(",", $argv[1]);
shuffle($items);
$shuffled = 0;
$ordered = bogoSort($items);
echo "Ordering took: " . $shuffled . " shuffles\n";
function bogoSort(Array $items)
{
global $shuffled;
while(!isInOrder($items))
{
echo "Shuffling!\n";
$shuffled++;
shuffle($items);
}
return $items;
}
function isInOrder(Array $items)
{
for ($i=1; $i<count($items); $i++)
if ($items[$i] < $items[$i-1])
return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment