PHP implementation of Bozo sort

Bozo Sort is more efficient than than bogosort(in theory). It does sorting by randomly chose 2 items and compare them, then switch them if needed. Until everything is sorted.

function bozo_sort($array){
	$sorted = $array;
	sort($sorted);
	while($sorted !== $array){
		$i = array_rand($array);
		$j = array_rand($array);
		if($i < $j){
			if($array[$i] > $array[$j]){
				$z = $array[$j];
				$array[$j] = $array[$i];
				$array[$i] = $z;
			}
		}else{
			if($array[$j] > $array[$i]){
				$z = $array[$i];
				$array[$i] = $array[$j];
				$array[$j] = $z;
			}
		}
	}
	return $array;
}

Post new comment

The content of this field is kept private and will not be shown publicly.
If you have a Gravatar account, used to display your avatar.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <span> <fn>
  • Lines and paragraphs break automatically.
  • Textual smileys will be replaced with graphical ones.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.

More information about formatting options

What is 2 + 1?
To combat spam, please solve the math question above.
Honey Pot that kill bots