PHP Common sorting algorithm

Source: Internet
Author: User

Insert sort (insertion sort), choose sort (Selection sort), bubble sort and quick sort are the sort algorithms we often use. Here are the basic ideas of these algorithms and the corresponding PHP implementation code.

The basic idea of inserting a sort (insertion sort) is to insert a record to be sorted each time, by its keyword size, into the appropriate location in the previously sorted sub-file until all records are inserted.

//Insert Sort (one-dimensional array)functionInsert_sort ($arr){    $count=Count($arr);  for($i= 1;$i<$count;$i++){        $tmp=$arr[$i]; $j=$i-1;  while($arr[$j] >$tmp){            $arr[$j+1] =$arr[$j]; $arr[$j] =$tmp; $j--; }     }    return $arr;}$arr=Array(1,2,3,5,100,3,2,4,5,6,7,55,78,46);$arr=insert_sort ($arr);Print_r($arr);

The basic idea of choosing a sort (Selection sort) is that each trip selects the smallest record of the keyword from the record to be sorted, placing the order at the end of the ordered sub-file until all records have been sorted.

//Select sort (one-dimensional array)functionSelect_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;}$arr=Array(1,2,3,5,100,3,2,4,5,6,7,55,78,46);$arr=select_sort ($arr);Print_r($arr);

The basic idea of bubble sorting is: 22 Compare the keywords of the records to be sorted, and find that the two records are exchanged in reverse order until there are no reversed records.

//Bubble sort (one-dimensional array)functionBubble_sort ($array){    $count=Count($array); if($count<= 0)return false;  for($i= 0;$i<$count;$i++){         for($j=$count-1;$j>$i;$j--){            if($array[$j] <$array[$j-1]){                $tmp=$array[$j]; $array[$j] =$array[$j-1]; $array[$j-1] =$tmp; }         }     }    return $array;} $arr=Array(1,2,3,5,100,3,2,4,5,6,7,55,78,46);$arr=bubble_sort ($arr);Print_r($arr);

The fast sort is essentially the same as bubbling sort, which is an application that belongs to the interchange sort. So the basic idea is the same as the bubble sort above.

//Quick Sort (one-dimensional array)functionQuick_sort ($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= Quick_sort ($left _arr); $right _arr= Quick_sort ($right _arr); return Array_merge($left _arr,Array($key),$right _arr);}$arr=Array(1,2,3,5,100,3,2,4,5,6,7,55,78,46);$arr=quick_sort ($arr);Print_r($arr);

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.