PHP implements four fundamental sorting algorithms

Source: Internet
Author: User
PHP implementation of four basic sorting algorithms

Many people say that the algorithm is the core of the program, the quality of the algorithm determines the program. As a primary phper, although it is seldom exposed to algorithmic aspects of things. However, the basic sorting algorithm should be mastered, it is a necessary tool for program development. Here we introduce bubble sort, insert sort, choose Sort, quick sort four basic algorithms, analyze the idea of algorithm.

Premise: The bubble sorting method, the fast sorting method, the selection of sorting method, the insertion of the sorting method in the following array of values in order from small to large orders.
$arr (1,43,54,62,21,66,32,78,36,76,39);
1. Bubble sort

Thinking Analysis: In order to sort a group of numbers, the current is not well-arranged sequence, from the back to the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, smaller upward. That is, each time a comparison of two adjacent numbers finds that they are ordered in the opposite order as they are ordered, they are interchanged.

Code implementation:

Java code

  1. $arr =array (1,--------- ( a);
  2. function Bubblesort ($arr)
  3. {
  4. $len =count ($arr);
  5. The layer loop controls the number of rounds 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

Thinking analysis: In the group of numbers to be sorted, select the smallest number to exchange with the number of the first position. Then in the remaining number, find the smallest and second position of the number of exchanges, so loop to the penultimate number and the last number comparison.

Code implementation:

Java code

  1. function Selectsort ($arr) {
  2. Double loop complete, outer control wheel count, inner control comparison number
  3. $len =count ($arr);
  4. For($i =0; $i < $len-1; $i + +) {
  5. First assume the position of the smallest value
  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 smaller, record the position of the minimum value, and compare with the known minimum value at the next comparison.
  11. $p = $j;
  12. }
  13. }
  14. 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.
  15. If($p! = $i) {
  16. $tmp = $arr [$p];
  17. $arr [$p] = $arr [$i];
  18. $arr [$i] = $tmp;
  19. }
  20. }
  21. Return to final result
  22. return $arr;
  23. }


3. Insert Sort

Thinking Analysis: In order to sort a group of numbers, assuming that the previous number is already in the sequence, now the nth number is inserted into the ordinal number in front, so that the number of n is also in order. This cycle is repeated until all the rows are in order.
Code implementation:

Java code

  1. function Insertsort ($arr) {
  2. $len =count ($arr);
  3. for ($i =1, $i < $len; $i + +) {
  4. $tmp = $arr [$i];
  5. Inner loop control, compare and insert
  6. For($j = $i-1; $j >=0; $j-) {
  7. If($tmp < $arr [$j]) {
  8. Find the inserted element to be small, swap position, swap the element behind with the previous element
  9. $arr [$j +1] = $arr [$j];
  10. $arr [$j] = $tmp;
  11. } Else {
  12. 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.
  13. Break;
  14. }
  15. }
  16. }
  17. return $arr;
  18. }


4. Quick Sort

Thinking Analysis: Select a datum element, usually select the first element or the last element. With a scan, the pending sequence is divided into two parts, some smaller than the Datum element, and a portion greater than or equal to the datum element. At this point the datum element is in the correct position after it is ordered, and then the same method is used to sort the two parts recursively.

Code implementation:

Java code

    1. function QuickSort ($arr) {
    2. Determine if you need to proceed
    3. $length = count ($arr);
    4. If($length <= 1) {
    5. return $arr;
    6. }
    7. Select the first element as a datum
    8. $base _num = $arr [0];
    9. Iterates through all elements except the ruler, and fits into two arrays in relation to size
    10. Initialize two arrays
    11. $left _array = Array (); //Less than the baseline
    12. $right _array = Array (); //greater than the benchmark
    13. For($i =1; $i < $length; $i + +) {
    14. If($base _num > $arr [$i]) {
    15. Put in the left array
    16. $left _array[] = $arr [$i];
    17. } Else {
    18. Put it on the right
    19. $right _array[] = $arr [$i];
    20. }
    21. }
    22. This function is recursively called on the left and right arrays by the same sort method.
    23. $left _array = Quick_sort ($left _array);
    24. $right _array = Quick_sort ($right _array);
    25. Merging
    26. Return Array_merge ($left _array, Array ($base _num), $right _array);
    27. }

1/ F FEIMENGV
good sorting method, it is important to use rationally, improve the efficiency of the program!
  • 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.