Algorithm analysis of PHP surface test questions

Source: Internet
Author: User

The interview is often asked what algorithm, where the integration of a number of common algorithms and their implementation principles. The following examples are tested and available, if you have any questions please let us know!!

I small white, if there is a better way to achieve, please enlighten me, thank you!!!!

/** * Bubble Sort * Adjacent 2 number comparison, small in front, big in the after * array has several elements, it is necessary to compare several rounds $i * number of times each round needs to be compared, array elements-Number of comparisons $j * @param array$array* @return Array$array*/function Bubblesort ($array){$cnt =Count$array);For$i = 0;$i <$cnt;$i + +){For$j = 0;$j < ($CNT-$i-1);$j + +) { $array [ $j] >  $array [ $j +1 $temp =  $array [ $j Span style= "color: #000000;" >];  $array [ $j] =  $array [ Span style= "color: #800080;" > $j +1 $array [ $j +1] =  $temp Span style= "color: #000000;" >; }}} return  $array 
/span>

/** * Quick Sort * Recursive implementation * Gets the first number of data, the loop makes a comparison of the following number, * than its small placed in an array, larger than its size in an array * 2 arrays are called recursively until the final array element is less than or equal to 1 o'clock, there is no comparable element * by Array_ The merge function merges the array of comparisons in order of size and then a layer of return, resulting in a sort from small to large * @param array $array * @return Array $array*/functionQuickSort ($array){        if(Count($array) <= 1)return $array; $key=$array[0]; $left _arr=Array(); $right _arr=Array();  for($i= 1;$i<Count($array);$i++){                if($array[$i] <=$key){                        $left _arr[] =$array[$i]; }Else{                        $right _arr[] =$array[$i]; }        }        $left _arr= QuickSort ($left _arr); $right _arr= QuickSort ($right _arr); return Array_merge($left _arr,Array($key),$right _arr);}

Algorithm analysis of PHP surface test questions

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.