Using PHP to describe the bubbling sort and quick sort algorithm, an object can be an array.
Using PHP to describe order lookups and binary lookups (also called binary lookup) algorithms, sequential lookups must consider efficiency, and an object can be an ordered array.
Write a two-dimensional array sorting algorithm function, which can be universal, call PHP built-in functions
1. Using PHP to describe the bubbling sort and quick sort algorithm, an object can be an array
function Bubble_sort ($array)
{
$count = count ($array);
If($count <=0)returnfalse;
for($i =0; $i < $count; $i + +) {
for($j = $count-1; $j > $i; $j –) {
If($array [$j] < $array [$j-1]){
$tmp = $array [$j];
$array [$j] = $array [$j-1];
$array [$j-1] = $tmp;
}
}
}
return$array;
}
function Quick_sort ($array) {
If(Count ($array) <=1)return$array;
$key = $array [0];
$left _arr = $right _arr = Array ();
foreach($array as$val) {
If($val < = $key) {
$left _arr[] = $val;
}else {
$right _arr[] = $val;
}
}
$left _arr = Quick_sort ($left _arr);
$right _arr = Quick_sort ($right _arr);
returnArray_merge ($left _arr, Array ($key), $right _arr);
}
2. Using PHP to describe order lookups and binary lookups (also called binary lookup) algorithms, sequential lookups must consider efficiency, an object can be an ordered array
function Bin_sch ($array, $low, $high, $k) {
If($low <= $high) {
$mid = Intval (($low + $high)/2);
If($array [$mid] = = $k) {
return$mid;
}elseif ($k < $array [$mid]) {
returnBin_sch ($array, $low, $mid-1, $k);
}Else{
returnBin_sch ($array, $mid +1, $high, $k);
}
}
return-1;
}
function Seq_sch ($array, $n, $k) {
$array [$n] = $k;
for($i =0; $i < $n; $i + +) {
If($array [$i]== $k) {
break;
}
}
If($i < $n) {
return$i;
}Else{
return-1;
}
}
3. Write a two-dimensional array sorting algorithm function, which can be universal, call PHP built-in functions
function Array_sort ($arr, $keys, $order =0) {
If(!is_array ($arr)) {
returnfalse;
}
$keysvalue = Array ();
foreach($arr as$key = $val) {
$keysvalue [$key] = $val [$keys];
}
If($order = =0){
Asort ($keysvalue);
}Else{
Arsort ($keysvalue);
}
Reset ($keysvalue);
foreach($keysvalue as$key = $vals) {
$keysort [$key] = $key;
}
$new _array = Array ();
foreach($keysort as$key = $val) {
$new _array[$key] = $arr [$val];
}
return$new _array;
}
PHP Description Bubble Sort and quick sort algorithm