PHP implements four basic sorting algorithms and four php algorithms.

Source: Internet
Author: User

PHP implements four basic sorting algorithms and four php algorithms.

Many people say that algorithms are the core of a program. The quality of algorithms determines the quality of the program. As a beginner phper, although seldom touched on algorithms. However, the basic sorting algorithm should still be mastered. It is a necessary tool for program development. Here we will introduce four basic algorithms: Bubble sorting, insert sorting, select sorting, and fast sorting, and analyze the algorithm ideas.

Premise: Use the Bubble sorting method and the quick sorting method respectively. Select the sorting method and insert the sorting method to sort the values in the following array in the ascending order.
$ Arr (, 78, 39 );
1. Bubble Sorting 

Train of Thought Analysis: in a set of numbers to be sorted, compare and adjust the adjacent two numbers from the past to the next, so that the larger numbers will sink, A relatively small amount of data goes up. That is, when the numbers of two adjacent parties are compared and their sorting and sorting requirements are opposite, they are exchanged.

Code implementation:

Java code
  1. $ Arr = array (, 78, 39 );
  2. Function bubbleSort ($ arr)
  3. {
  4. $ Len = count ($ arr );
  5. // This layer cyclically controls the number of rounds to be bubbling
  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 for each 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 

Train of Thought Analysis: In the number of a group to be sorted, select the minimum number and the number at the first position. Then find the smallest number in the remaining number and exchange it with the number in the second position, so that the loop ends until the last number is compared with the last number.

Code implementation:

Java code
  1. Function selectSort ($ arr ){
  2. // Dual Loop completion, number of outer control wheels, number of inner control comparisons
  3. $ Len = count ($ arr );
  4. For ($ I = 0; $ I <$ len-1; $ I ++ ){
  5. // First assume the location of the smallest value
  6. $ P = $ I;
  7. For ($ j = $ I + 1; $ j <$ len; $ j ++ ){
  8. // $ Arr [$ p] is the minimum value currently known.
  9. If ($ arr [$ p]> $ arr [$ j]) {
  10. // Compare, find a smaller value, record the location of the minimum value; and use the known minimum value for the next comparison.
  11. $ P = $ j;
  12. }
  13. }
  14. // The location of the current minimum value has been determined and saved to $ p. If you find that the location of the minimum value is different from the current position $ I, you can change the location.
  15. If ($ p! = $ I ){
  16. $ Tmp = $ arr [$ p];
  17. $ Arr [$ p] = $ arr [$ I];
  18. $ Arr [$ I] = $ tmp;
  19. }
  20. }
  21. // Return the final result
  22. Return $ arr;
  23. }


3. Insert sorting 

Train of Thought Analysis: In the number of a group to be sorted, assuming that the previous number is already sorted, we need to insert the nth number into the previous order number, so that the n numbers are sorted in order. This repeats until all the rows are sorted.
Code implementation:

Java code
  1. Function insertSort ($ arr ){
  2. $ Len = count ($ arr );
  3. For ($ I = 1, $ I <$ len; $ I ++ ){
  4. $ Tmp = $ arr [$ I];
  5. // Loop control in the inner layer, compare and insert
  6. For ($ j = $ I-1; $ j> = 0; $ j --){
  7. If ($ tmp <$ arr [$ j]) {
  8. // It is found that the inserted element is small, and the position of the element is switched.
  9. $ Arr [$ j + 1] = $ arr [$ j];
  10. $ Arr [$ j] = $ tmp;
  11. } Else {
  12. // If you encounter elements that do not need to be moved, because they are sorted as arrays, you do not need to compare them again.
  13. Break;
  14. }
  15. }
  16. }
  17. Return $ arr;
  18. }


4. Quick sorting 

Train of Thought Analysis: select a benchmark element, usually the first or last element. After a scan, the columns to be sorted are divided into two parts. One part is smaller than the benchmark element, and the other part is greater than or equal to the benchmark element. At this time, the reference element is in the correct position after sorting, and then uses the same method to recursively sort the divided two parts.

Code implementation:

Java code
    1. Function quickSort ($ arr ){
    2. // Determine whether or not to continue
    3. $ Length = count ($ arr );
    4. If ($ length <= 1 ){
    5. Return $ arr;
    6. }
    7. // Select the first element as the benchmark
    8. $ Base_num = $ arr [0];
    9. // Traverse all elements except the ruler and put them in two arrays according to the size relationship
    10. // Initialize two Arrays
    11. $ Left_array = array (); // smaller 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 the array on the left
    16. $ Left_array [] = $ arr [$ I];
    17. } Else {
    18. // Put it on the right
    19. $ Right_array [] = $ arr [$ I];
    20. }
    21. }
    22. // Recursively call this function to sort the array on the left and right respectively in the same way.
    23. $ Left_array = quick_sort ($ left_array );
    24. $ Right_array = quick_sort ($ right_array );
    25. // Merge
    26. Return array_merge ($ left_array, array ($ base_num), $ right_array );
    27. }

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.