`

选择排序

 
阅读更多
选择排序
<?php
/**
 * selectSort
 */
function selectionSort($data){

  $len = count($data);
  
  for($i=0; $i<$len; $i++){
	
	for($j=$i+1; $j<$len; $j++){
	  
	  if($data[$i] > $data[$j]){
		$t = $data[$i];
		$data[$i]=$data[$j];
		$data[$j]=$t;
	  }
	}
  }
  return $data;
}
/**
 * Super select sort
 */
function superSelectionSort($data){

  $len = count($data); 

  for($i=0; $i<$len; $i++){

	$t = $i;

	for($j=$i+1;$j<$len;$j++){
	  if($data[$t] > $data[$j]){
		$t = $j;
	  }
	}
	
	$tmp = $data[$t];
	$data[$t] = $data[$i];
	$data[$i] = $tmp;
  }
  return $data;
}
$data = array(9,8,5,32,234,65,1,0);
print_r(superSelectionSort($data));
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics