Example: Sort by bubble, quick sort, select sort, insert sort order the values in the array from small to large
$array = (9,5,1,3,6,4,8,7,2);
1. Bubble Sorting algorithm
Idea: 22 Compare the size of the data element to be sorted, and find that the two data elements are exchanged in reverse order until there are no reversed data elements Function bubblesort ($array) { $lg = count ($array); if ($LG <=1) { return $array; } //this layer cycle control Number of rounds to bubble for ($i =0; $i < $LG; $i + +) { //This layer loop is used to control the number of times each round pops up The number of comparisons that need to be compared for ($j =1; $j < $LG-$i; $j + +) { if ($array [$j -1]> $array [$j]) { $_ tmp = $array [$j -1]; $array [$j -1] = $array [$j]; $array [$j] = $_tmp; } } } return $array; }
2, choosing a sorting algorithm
Idea: In the set of numbers to sort, select the smallest number to exchange with the number of the first position. Then in the remaining number, find the smallest and second position of the number of exchanges, so loop to the penultimate number and the last number of the comparison so far Function selectsort ($array) { $lg = count ($array) if ($LG <=1) { return $array; } //double loop complete, outer control wheel number, Internal control comparison for ($i =0; $i < $LG; $i + +) { $min = $i;//First assume the position of the smallest value for ($j = $i +1; $j < $LG; $j + +) { //$array [$min] is the currently known minimum value if ($array [$j] < $array[$min]) { $min = $j;//Compare, find smaller, record the location of the minimum value, and compare it with a known minimum value at the next comparison } } / /The position of the current minimum value has been determined and saved to $p. If the location of the minimum value is found to be different from the current assumed position $i, the position can be if ($i != $min) { $_tmp = $array [$i]; $array [$i] = $array [$min]; $array [$min] = $_tmp; } } return $array; }
3, insert sort algorithm
Idea: In order to sort a group of numbers, assuming that the previous number is already in the sequence, now you want to insert the nth number into the ordinal number in front, so that the n number is also in order. This cycle is repeated until all the rows are in order. Function insertsort ($array) { $lg = count ($array) ; if ($lg <=1 ) { return $array; } for ($i =1; $i < $LG; $i + +) { $x = $array [$i]; $j = $i -1; while ($j >=0) && ($array [$j]> $x) { $array [$j +1] = $array [$j]; $j--; } if ($array [$j +1] != $x) { $array [$j +1] = $x; } } return $array; }
4, Quick sort algorithm
Function quicksort ($array) { //first to determine if the need to continue $length = count ($array); if ($length <= 1) { return $array; } // Select the first element as the Datum $base _num = $array [0]; //traverses all elements except the ruler, Put two arrays in the size relationship //initialize two arrays $left _array = array (); //less than benchmark $right _array = array (); //is greater than the benchmark for ($i =1; $i < $length; $i +) { if ($base _ num > $array [$i]) { / /Put in the left array $left _array[] = $ array[$i];&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; } else { // Put on the right $right _array[] = $array [ $i]; } } // This function is then recursively called on the left and right arrays with the same sorting method $left _array = quicksort ($left _array); $right _array = quicksort ($right _array) //merger return array_merge ($left _array, array ($base _num), $right _array);}
5, two-part lookup algorithm
Function binarysearch ($arr, $key) { $low = 0; $high = 9; while ($low <= $high) { $mid = intval ($ low+ $high)/2); if ($key == $ arr[$mid]) { return $mid +1; }elseif ($key <$ arr[$mid]) { $ high = $mid -1; }elseif ($key > $arr [$mid]) { $low = $mid +1;&Nbsp; } } return -1; }
6. Sequential Lookup algorithm
function Sqsearch ($arr, $value) {$length = count ($arr); for ($i =0; $i < $length; $i + +) {if ($value = = $arr [$i]) {return $i +1; }} return-1;}
Sorting Algorithms and Lookup algorithms