PHP Implementation Bubble Sort, select sort, insert sort and quick sort quick Sort Method Fast sorting C language fast sorting algorithm C

Source: Internet
Author: User
before self-study data structure of the time to see the C language version of the four basic sorting method, C language is almost forgotten, recently available in PHP four sort method to re-write again, review the increasingly unfamiliar algorithm. Paste the code directly.

 
  ";    Print_r ($var);    echo "
";} $arr =array (33,11,22,66,55,44,88,99,77);p rintf ("* * Original array * *");p($arr);/*** Bubble sort * @param $arr sorted array * Idea: compared to adjacent numbers, If the left is larger than the right, swap the position. * Two nodes, One Direction: two cycles, bubbling direction (i.e. $j initial and termination conditions) *num's role is to do the optimization, once the loop has no exchange then the bubble has completed **/function Bubblesort ($arr) {print ("* * bubble Sort Method * *"); $ Len=count ($arr); $temp =0; $num =1;for ($i = 0; ($i < $len-1) && ($num >0); $i + +) {$num =0;for ($j = $len-1; $j > $i; $j-) {if ($arr [$j]> $arr [$j-1]) {continue;} else{//bubbling Exchange $temp= $arr [$j-1]; $arr [$j -1]= $arr [$j];; $arr [$j]= $temp; $num + +;}}} return $arr;} P (Bubblesort ($arr));/*** Select sorting method * @param $arr sorted array * Idea: each cycle, find the smallest number, the subscript to save to the left number * and find the smallest number method is: and the right number comparison, if the right side is relatively small, then save its subscript, The value corresponding to this subscript is compared to the next right digit until all the right digits are compared again, and the minimum value is stored in the left-most number **/function Selectsort ($arr) {print ("* * * * * * * * * * * * *"); $len =count ($arr) ; $buff =0;for ($i =0; $i < $len 1; $i + +) {$temp = $i; for ($j = $i; $j < $len-1; $j + +) {if ($arr [$temp]> $arr [$j +1]) {$te mp= $j +1;//If the right number is smaller, save its subscript, to compare with the subsequent data}}//get the smallest number subscript if ($temp! = $i) {//If the subscript is not $i then exchange, otherwise there is no need to $buff= $arr [$temp]; $arr [$temp ]= $arr [$i]; $arr [$i]= $buff;}} return $arr;} P (Selectsort ($arr));/*** Insert Sort * @param $arr sorted array * Idea: Assuming that the previous number is already in order, now you want to insert the nth number into the ordinal number in front. That is, insert the second data into the first data, make it an ordered array, and then the third number is inserted into an ordered array of the previous two numbers, forming an ordered array, so that the final sorting is done **/function insertsort ($arr) {print ("* * Insert Sort Method * * "), $len =count ($arr), for ($i =1; $i < $len; $i + +) {$temp = $arr [$i];//the data to be inserted is cached for ($j = $i-1; $j >=0; $j-) {if ( $temp < $arr [$j]) {$arr [$j +1]= $arr [$j];//if the data to be inserted is smaller than the last one in the original array, the $arr[$j]= $temp; else break;}} return $arr;} P (Insertsort ($arr));/*** Fast Sorting method * @param $arr sorted array * Idea: Select a datum element, pass a scan, divide the waiting sequence into two parts, and then call your own method recursively for each part * Finally, we return to the sorted array on the left and the sorted array on the right and the base element merge, which is an ordered array. **/function QuickSort ($arr) {$len =count ($arr), if ($len <=1) return $arr; $baseNum = $arr [0]; $leftArr = $rightArr =array ();//divided into two parts for ($i =1; $i < $len; $i + +) {if ($arr [$i]<= $arr [0]) {$LEFTARR []= $arr [$i];} else{$RIGHTARR []= $arr [$i];}} $LEFTARR =quicksort ($LEFTARR);//Recursive call $rightarr=quicksort ($RIGHTARR); return Array_merge ($LEFTARR, Array ($baseNum), $ Rightarr);} Print ("* * Fast Sorting method * *");p(QuickSort ($arr));? >

Operation Result:



The above describes the PHP implementation bubble sort, choose sort, insert sort and quick sort, including quick sort, insert sort aspect of content, want to be interested in PHP tutorial friends 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.