Common sorting algorithms

Source: Internet
Author: User

    • Bubble sort

The larger number sinks, the smaller the number rises, the more the two numbers are compared.

function Bubblesort (array $arr) {    for ($i =0, $len =count ($arr); $i < $len; $i + +) {         $flag = false;        for ($j =0; $j < $len-1; $j + +) {             if ($arr [$j] > $arr [$j +1]) {                $tmp = $arr [$j];                $arr [$j] = $arr [$j +1];                $arr [$j +1] = $tmp;                $flag = true;            }        }        If no exchange occurs, early termination;        if ($flag = = false) break;    }    return $arr;}
    • Select sort

In an array of length n, find the minimum value in the number of N-1 followed by the 1th digit interchange, the lowest value in the N-2 with the 2nd interchange, and so on until the last number.

function Selectsort (array $arr) {    for ($i =0, $len =count ($arr); $i < $len-1; $i + +) {         $min _key = $i +1;        for ($j = $i +1; $j < $len; $j + +) {             if ($arr [$j] < $arr [$min _key]) {                $min _key = $j;            }        }        if ($arr [$i] > $arr [$min _key]) {            $tmp = $arr [$i];            $arr [$i] = $arr [$min _key];            $arr [$min _key] = $tmp;        }    }    return $arr;}

    • Insert Sort

Suppose that the first n-1 in the array is ordered, and the nth number is inserted in front so that the number of n is also ordered, so that the first number begins to loop until all is ordered.

function Insertsort (array $arr) {    for ($i =0, $len =count ($arr); $i < $len-1; $i + +) {for        ($j = $i +1; $j > 0; $j- -) {             //If the subsequent number is smaller than the previous, the swap occurs            if ($arr [$j] < $arr [$j-1]) {                $tmp = $arr [$j];                $arr [$j] = $arr [$j-1];                $arr [$j-1] = $tmp;            }        }    }    return $arr;}

    • Hill sort

Can be seen as a grouping insert sort, sort the array by one increment, and then gradually decrease the increment until the increment is 1

function Shellsort (array $arr) {    $len = count ($arr);    $inc = Round ($len/2);    while (true) {for         ($i =0, $i < $inc; $i + +) {for             ($j = $i + $inc; $j < $len, $j + = $inc) {for                 ($k = $j-$inc; $k >= $i; $k-= $inc) {                     if ($arr [$k] > $arr [$k + $inc]) {                        $tmp = $arr [$k];                        $arr [$k] = $arr [$k + $inc];                        $arr [$k + $inc] = $tmp;                    }        }} if ($inc = = 1) break;        $inc = Round ($inc/2);    }    return $arr;}

    • Quick Sort

First select 1 numbers from the array as the key value, will be smaller than the key on the left, large on the right. Use recursion to do the same for the sequence at both ends of the key.

function QuickSort (array $arr, $left = null, $right = null) {    $left = $left ===null? 0: $left;    $right = $right ===null? Count ($arr)-1: $right;    if ($left >= $right) {        return $arr;    }    $key = $arr [$left]; $i = $left; $j = $right;    while ($i < $j) {        while ($i < $j && $arr [$j] >= $key) {            $j--;        }        if ($i < $j) {            $arr [$i] = $arr [$j];            $i + +;        }        while ($i < $j && $arr [$i] < $key) {            $i + +;        }        if ($i < $j) {            $arr [$j] = $arr [$i];            $j--;        }    }    $arr [$i] = $key;    $arr = QuickSort ($arr, $left, $i-1);    $arr = QuickSort ($arr, $i +1, $right);    return $arr;}

    • Merge sort

Merge the ordered array, decompose the sequence into the smallest unit, then merge sequentially.

function mergesort (array $arr) {$merge = $arr;         while (true) {$tmp = [];        $count = count ($merge);        if ($count = = 1) {return $merge [0]; } for ($i =0; $i < $count; $i +=2) {if (Isset ($merge [$i +1]) {$tmp [] = Arraymerge ($me            rge[$i], $merge [$i +1]);            } else {$tmp [] = $merge [$i];    }} $merge = $tmp; }}function arraymerge ($a, $b) {if (!is_array ($a) &&!is_array ($b)) {if ($a < $b) return        [$a, $b];    else return [$b, $a];        } else {$c = [];        $i = $j = $k = 0;        $m = count ($a);        $n = count ($b);            while ($i < $m && $j < $n) {if ($a [$i] < $b [$j]) $c [$k + +] = $a [$i + +];        else $c [$k + +] = $b [$j + +];        } while ($i < $m) {$c [$k + +] = $a [$i + +]; } while ($j < $n) {$c [$k + +] = $b [$j + +];    } return $c; }}

    • Heap Sort

Heap is a complete binary tree, divided into Dagen and small Gan, Dagen requires that the value of each node is not greater than the value of the parent node, small Gan conversely. Heap sorting is a sort of algorithm using this data structure design, it is a sort of selection, can use the characteristics of the array to quickly locate the element in which the index resides.

function Heapsort (array $arr) {    $len = count ($arr);    for ($i = $len-1; $i >= 0; $i-) {         $arr = buildheap ($arr, $len, $i);        $arr = Swap ($arr, 0, $i);    }    return $arr;} Build the heap, order reverse sorting to establish Gans function buildheap ($arr, $len, $LMT) {for    ($i =intval ($len/2)-1; $i >= 0; $i-) {         $left = 2 * $i +1;        $right = $i +2;        if ($left > $LMT) continue;        if ($right > $LMT) {            $k = $left;        } else {            $k = $arr [$left]> $arr [$right]? $left: $right;        }        if ($arr [$i] < $arr [$k]) {            $arr = swap ($arr, $i, $k);        }    }    return $arr;} Swap data function Swap ($arr, $k 1, $k 2) {    $tmp = $arr [$k 1];    $arr [$k 1] = $arr [$k 2];    $arr [$k 2] = $tmp;    return $arr;}

    • Base sort

Binsort the value into its corresponding position as a key value, and then iterates through the array to get the result. When there is a large value in the sequence, Binsort's sorting method wastes a lot of space overhead. Base sorting is based on Binsort, which reduces the overhead of space through the limitation of cardinality. The process of cardinal sorting is to unify all the values to be compared (positive integers) to the same digit length, and the number of shorter digits is preceded by 0. Then, start with the lowest bit and order one at a time. Thus, from the lowest bit to the highest order, the sequence becomes an ordered series. The cardinality is sorted by LSD (Least significant digital) or MSD (most significant digital), and LSD is sorted by the rightmost start of the key value, while the MSD is the opposite, starting with the leftmost side of the key value.

function Radixsort (array $arr, $level =1) {    //using LSD    $limit = POW ($level);    $div = Pow (Ten, $level-1);    $flag = false;    $bucket = [];    $ret = [];    foreach ($arr as $v) {        $bucket [Intval ($v/$div)%10][] = $v;        if ($v >= $limit) $flag = true;    }    for ($i =0; $i <, $i + +) {         if (isset ($bucket [$i])) {            $ret = Array_merge ($ret, $bucket [$i]);        }    }    if ($flag) {        $ret = Radixsort ($ret, $level + 1);    }    return $ret;}

Common sorting algorithms

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.