Data structure is important, algorithm + data structure + document = Program
using PHP to describe the bubbling sorting algorithm, an object can be an array
Copy CodeThe code is as follows:
//bubble sort (array sort)
function Bubble_sort ($array) {
$count = count ($array);
if ($count <= 0)
return false;
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; }
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
Copy CodeThe code is as follows:
//Two points lookup (find an element in an 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]) {
Return Bin_sch ($array, $low, $mid-1, $k);
}else{
Return Bin_sch ($array, $mid +1, $high, $k);
}
}
return-1;
}
//Sequential lookup (find an element in an array)
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;
}
}
Write a two-dimensional array sorting algorithm function, which can be universal, call PHP built-in functions
Copy CodeThe code is as follows:
//Two-D array sorting, $arr is data, $keys is the sort of health value, $order is collation, 1 is ascending, 0 is descending
function Array_sort ($arr, $keys, $order =0) {
if (!is_array ($arr)) {
return false;
}
$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;
}
http://www.bkjia.com/PHPjc/327838.html www.bkjia.com true http://www.bkjia.com/PHPjc/327838.html techarticle data structure is very important, algorithm + data structure + document = program using PHP to describe the bubble sorting algorithm, the object can be an array copy code code as follows://bubble sort (array sort) ...