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;
}

Comments

Post new comment

  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <span> <fn>
  • Lines and paragraphs break automatically.
  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].
  • Mathematical equations and graphs can be added between [tex] and [/tex], [graph] and [/graph] tags.
  • Textual smileys will be replaced with graphical ones.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Honey Pot that kill bots