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