PHP algorithm face question

Source: Internet
Author: User

1. Using PHP to describe the bubble sort and quick sort algorithm, an object can be an array

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;

}

Quick sort (Array sort)

function Quick_sort ($array) {

if (count ($array) <= 1) return $array;

$key = $array [0];

$left _arr = Array ();

$right _arr = Array ();

for ($i =1; $i; $i + +) {

if ($array [$i] <= $key) {

$left _arr[] = $array [$i];

}else{

$right _arr[] = $array [$i];

}

}

$left _arr = Quick_sort ($left _arr);

$right _arr = Quick_sort ($right _arr);

Return Array_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

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

}

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

}

}

3. Write a two-dimensional array sorting algorithm function, which can be universal, call PHP built-in functions

Two-dimensional array sorting, $arr is the data, $keys is the sort of health value, $order is the 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;

}

PHP algorithm face question

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.