PHP implementation of four basic sorting algorithms

Source: Internet
Author: User

Sorted array: $arr (1,43,54,62,21,66,32,78,36,76,39);


Sort by four sorting algorithms


Bubble sort:(idea: A number that is not ordered, compared and adjusted from the last two digits, a large sinking, a small ascent)

    $arr =array (1,43,54,62,21,66,32,78,36,76,39);       function Bubblesort ($arr)      {       $len =count ($arr);      The layer loop controls the number of rounds that need to bubble for      ($i =1; $i < $len; $i + +)      {//This layer loop is used to control the number of times a number needs to be compared per round for      ($k =0; $k < $len-$i; $k + +)      {      if ($arr [$k]> $arr [$k +1])      {      $tmp = $arr [$k +1];      $arr [$k +1]= $arr [$k];      $arr [$k]= $tmp;      }      }} return $arr;      }  

Select Sort:(Find the smallest number in a group of numbers with the first number exchange position, in the remaining number to find the smallest with the second position of the number exchange,

To continue at once until the second and last number is compared

    function Selectsort ($arr) {      //double loop complete, outer control wheel count, inner control comparison count      $len =count ($arr);      for ($i =0; $i < $len-1; $i + +) {      //First assume the position of the smallest value      $p = $i;            for ($j = $i +1; $j < $len; $j + +) {      //$arr [$p] is the currently known minimum value      if ($arr [$p] > $arr [$j]) {      //compare, find smaller, Records the position of the minimum value, and compares it with a known minimum value at the next comparison.      $p = $j;      }      }      The location 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 is interchangeable.      if ($p! = $i) {      $tmp = $arr [$p];      $arr [$p] = $arr [$i];      $arr [$i] = $tmp;      }      }      Return the final result return      $arr;      }  

Insert Sort:(assuming that the previous number is already in the order, now you want to insert the nth number into the ordinal number in front, so that the n number is also in the order of the row.)

So cycle until all the rows are in order.

    function Insertsort ($arr) {      $len =count ($arr);       For ($i =1, $i < $len; $i + +) {      $tmp = $arr [$i];      Inner loop control, compare and insert      for ($j = $i-1; $j >=0; $j-) {      if ($tmp < $arr [$j]) {      //find the inserted element smaller, swap position, swap the element behind with the previous element      $arr [$j +1] = $arr [$j];      $arr [$j] = $tmp;      } else {      //If you encounter an element that does not need to be moved, because the array is already sorted, the preceding one does not need to be compared again. Break      ;      }}} return $arr;      }  

Quick Sort:(Select a datum element, usually select the first element or the last element.) Through a scan, the waiting sequence is divided into two parts,

Part is smaller than the base element, part is greater than or equal to the datum element, and the datum element is in the correct position after it is sorted, and then recursively using the same method

Two parts of the sorting division. )

function QuickSort ($arr) {  //first determine if the need to continue  $length = count ($arr);  if ($length <= 1) {  return $arr;  }  Select the first element as a datum  $base _num = $arr [0];  Iterate through all elements except the ruler, place two arrays in the size relationship  //Initialize two arrays  $left _array = Array ();//$right less than the baseline  _array = Array (); For  ($i =1; $i < $length; $i + +) {  if ($base _num > $arr [$i]) {  //put on left array  $left _array[] = $arr [$i];  } else {  //Put right  $right _array[] = $arr [$i];  }  }  This function is then recursively called on the left and right arrays with the same sorting method  $left _array = Quick_sort ($left _array);  $right _array = Quick_sort ($right _array);  Merge  return Array_merge ($left _array, Array ($base _num), $right _array);  }  




PHP implementation of four basic 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.