PHP implements a common sorting algorithm

Source: Internet
Author: User
Insert sort (Insertion sort): Inserts a record to be sorted each time, pressing its keyword size into the appropriate location in the previously sorted sub-file until all records are inserted.

Insert sort (one-dimensional array) function Insert_sort ($arr) {$count = count ($arr), for ($i =1; $i < $count; $i + +) {$tmp = $arr [$i]; $j = $i -1;whi Le ($arr [$j] > $tmp) {$arr [$i] = $arr [$j]; $arr [$j] = $tmp; $j--;}} return $arr;}

Select sort (Selection sort): Each time a record of the smallest keyword is selected from the record to be sorted, the order is placed at the end of the ordered sub-file until all records have been sorted.

Select sort (one-dimensional array) function Selection_sort ($arr) {$count = count ($arr), for ($i =0; $i < $count; $i + +) {$k = $i; for ($j = $i +1; $j < $count; $j + +) {if ($arr [$k] > $arr [$j]) {$k = $j;} if ($k! = $i) {$tmp = $arr [$i]; $arr [$i] = $arr [$k]; $arr [$k] = $tmp;}}} return $arr;}

Bubble sort (Bubble sort): 22 Compare the keywords of the records to be sorted, and find that the two records are exchanged in reverse order until there is no reverse record position.

Bubble sort (one-dimensional array)//The actual effect is that each loop places the smallest value in the array into the first segment of the array, and then the next loop no longer loops the key where the correct array value is placed, and so on function Selection_sort ($arr) {$count = Count ($arr); if ($count <= 0) return false;for ($i =0; $i < $count; $i + +) {for ($j = $count-1; $j > $i; $j-) {if ($arr [$j] < $ array[$j-1]) {$tmp = $arr [$j]; $arr [$j] = $arr [$j-1]; $arr [$j-1] = $tmp;}}} return $arr;}

Quick sort: Essentially the same as bubbling sort, is an application that belongs to an interchange sort.

Quick sort (one-dimensional array) function Quick_sort ($arr) {$count = count ($arr), if ($count <= 1) return $arr; $key = $arr [0]; $left _arr = Array (); $right _arr = Array (); for ($i =1; $i < $count; $i + +) {if ($arr [$i] <= $key) $left _arr[] = $arr [$i];else$right_ arr[] = $arr [$i];} $left _arr = Quick_sort ($left _arr); $right _arr = Quick_sort ($right _arr); return Array_merge ($left _arr,array ($arr), $ Right_arr);}

The above describes the PHP implementation of the common sorting algorithm, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.