Code implementation of PHP four basic sorting Algorithms (1)

Source: Internet
Author: User

Many people have said that the algorithm is the core of the program, the quality of the algorithm determines the process. As a primary phper, although little contact with algorithmic aspects. But for the basic sorting algorithm or should be mastered, it is the necessary tool for program development. This article introduces bubble sort, insert sort, select Sort, fast sort four basic algorithms, analyze the idea of the algorithm.

Premise: Bubble sort method, fast sorting method, select sorting method, insert Sort method to sort the values in the following arrays in order from small to large.

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

1. Bubble sort

Train of thought analysis: In order to sort of a group of numbers, the current has not yet lined up the sequence, from the next two consecutive number of adjacent to the comparison and adjustment, so that the larger number to sink, smaller upward. That is, when the number of adjacent numbers in two is found to be the opposite of their sorting requirements, they are interchanged.

 
 
  1. $arr =array (1,43,54,62,21,66,32,78,36,76,39);
  2. function Bubblesort ($arr)
  3. {
  4. $len =count ($arr);
  5. This layer loops the number of wheels that need to bubble
  6. for ($i =1; $i < $len; $i + +)
  7. {//This layer loop is used to control the number of times a number needs to be compared per round
  8. for ($k =0; $k < $len-$i; $k + +)
  9. {
  10. if ($arr [$k]> $arr [$k +1])
  11. {
  12. $tmp = $arr [$k +1];
  13. $arr [$k +1]= $arr [$k];
  14. $arr [$k]= $tmp;
  15. }
  16. }
  17. }
  18. return $arr;
  19. }

2. Select sort

Analysis: In the number of groups to be sorted, select the smallest number and the first position of the number exchange. Then, in the remaining number, find the smallest interchange with the second position, so that it loops to the penultimate number and the last number comparison.

 
 
  1. function Selectsort ($arr) {
  2. Double loop complete, outer control wheel number, inner layer control comparison times
  3. $len =count ($arr);
  4. For ($i =0 $i < $len-1; $i + +) {
  5. Assume the position of the smallest value first
  6. $p = $i;
  7. for ($j = $i +1; $j < $len; $j + +) {
  8. $arr [$p] is the currently known minimum value
  9. if ($arr [$p] > $arr [$j]) {
  10. Compare, find a smaller, recorded position of the minimum value, and compare it with a known minimum value for the next comparison.
  11. $p = $j;
  12. }
  13. }
  14. The position of the current minimum value has been determined and saved to the $p. If the location of the lowest value is found to be different from the current assumed position $i, the position is interchangeable.
  15. if ($p!= $i) {
  16. $tmp = $arr [$p];
  17. $arr [$p] = $arr [$i];
  18. $arr [$i] = $tmp;
  19. }
  20. }
  21. Return final Result
  22. return $arr;
  23. }



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.