The simple algorithm php

Selection Sort

 Way: let the first bit comparison with other sizes to find the smallest number, then the second place with, among others, to find out the size of the first bit comparison of the second, and so on

$arr = [2,45,12,67,33,5,23,132,46];
for ($i=0; $i < count($arr); $i++) {
    for ($j=$i+1; $j <count($arr) ; $j++) { 
        if($arr[$i] > $arr[$j]){
            $tmp = $arr[$i];
            $arr[$i] = $arr[$j];
            $arr[$j] = $tmp;
        }
    }
}

print_r($arr);

Bubble Sort

Method: Comparison of two adjacent data sorting positions and
optimization: determining whether the added fields if_replace wheel sorting is completed, if not continue to complete the back of the sorting

$arr = [2,45,12,67,33,5,23,132,46];
$if_replace = false;
for ($i=0; $i < count($arr); $i++) {
    for ($j=0; $j < count($arr)-1; $j++) {
        if($arr[$j] > $arr[$j+1]){
            $tmp = $arr[$j];
            $arr[$j] = $arr[$j+1];
            $arr[$j+1] = $tmp;
            $if_replace = true;
        }
    }
    if(!$if_replace){
        break;
    }
}
print_r($arr);

Insertion Sort

Methods: Construction of an ordered sequence, for unsorted data, scan in sorted sequence from back to front, and to find the corresponding position of the insert

$arr = [2,45,12,67,33,5,23,132,46];
for ($i=0; $i < count($arr)-1; $i++) { 
    for ($j=$i+1; $j > 0; $j--) {
        if($arr[$j] < $arr[$j-1]){
            $tmp = $arr[$j];
            $arr[$j] = $arr[$j-1];
            $arr[$j-1] = $tmp;
        }else{
            break;
        }
    }
}

print_r($arr);

Hill sorting
method: set to be sorted elements of a sequence has n elements, first takes an integer increment (less than n) as a spacer element into the entire increment subsequences,
   all of the elements from the increment in the same sub-sequences, each a subsequence direct insertion sort are implemented.
   Then narrow interval increment, repeating the dividing and ranking work sequences. Taken until the last increment = 1, all the elements in the same sub-sequence ordered so far.

 

$arr = [2,45,12,67,33,5,23,132,46];
$increment = count($arr);
do{

    $increment = floor($increment/3)+1;

    for ($i=0; $i < $increment; $i++) { 
        $k = 0;
        $j = $i;
        do{

            for ($m=$j+$increment; $m > 0; $m-=$increment) { 
                if($arr[$m-$increment] > $arr[$m] && $arr[$m]){
                    $tmp = $arr[$m];
                    $arr[$m] = $arr[$m-$increment];
                    $arr[$m-$increment] = $tmp;
                }else{
                    break;
                }
            }

            $k++;
            $j = $i+($k*$increment);
        }while($j<count($arr));

    }

}while($increment > 1);
print_r($arr);

 

  Quick Sort

: Remove the start number of columns in a number as a reference number, greater than the full count into the right side of it, it is less than or equal to the number of whole into its left, and so the operation of each section until only a few

$arr = [33, 24, 8, 21, 2, 23, 3, 32, 16];
function quickSort($arr)
{
    $count = count($arr);

    if ($count < 2) {
        return $arr;
    }

    $leftArray = $rightArray = array();
    $middle = $arr[0];// 基准值

    for ($i = 1; $i < $count; $i++) {
        //Than the reference value, stored in the left; greater than the reference value, stored in the right side 
        IF ( $ ARR [ $ I ] < $ Middle ) {
             $ leftArray [] = $ ARR [ $ I ]; 
        } the else {
             $ rightArray [] = $ ARR [ $ I ]; 
        } 
    } 

    $ leftArray = QUICKSORT ( $ leftArray );
     $ rightArray = QUICKSORT ( $ rightArray ); 

    return  The array_merge ( $ leftArray , Array ( $ Middle ),$rightArray);
}

print_r(quickSort($arr));

 

Guess you like

Origin www.cnblogs.com/yx520zhao/p/11511562.html