Four basic sorting algorithm examples in PHP and four Algorithm examples in PHP

Source: Internet
Author: User
PHP provides four basic sorting algorithm examples and four Algorithm examples. PHP provides four basic sorting algorithm examples. many people say that algorithms are the core of the program. the quality of algorithms determines the quality of the program. As a beginner phper, although there is little access to four basic sorting algorithm examples of PHP, four Algorithm examples

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(1,43,54,62,21,66,32,78,36,76,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:

$ Arr = array (, 54,62, 66, 39); function bubbleSort ($ arr) {$ len = count ($ arr ); // This layer controls the number of rounds to be bubbling for ($ I = 1; $ I <$ len; $ I ++) {// This layer loop is used to control the number of times a number needs to be compared in each round ($ 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 ;}

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:

Function selectSort ($ arr) {// double loop completion, number of outer control wheels, number of inner control comparisons $ len = count ($ arr); for ($ I = 0; $ I <$ len-1; $ I ++) {// first assume the location 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]) {// comparison, it is found that, record the location of the minimum value, and compare it with the known minimum value in the next comparison. $ P = $ j ;}/// 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. If ($ p! = $ I) {$ tmp = $ arr [$ p]; $ arr [$ p] = $ arr [$ I]; $ arr [$ I] = $ tmp;} // return the final result return $ arr ;}

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:

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]) {// It is found that the inserted element is small and the switch position is, swap the following elements with the preceding elements $ arr [$ j + 1] = $ arr [$ j]; $ arr [$ j] = $ tmp ;} else {// 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. Break ;}}return $ arr ;}

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:

Function quickSort ($ arr) {// first judge whether to continue $ length = count ($ arr); if ($ length <= 1) {return $ arr ;} // select the first element as the reference $ base_num = $ arr [0]; // traverses all elements except the ruler, put the two arrays according to the size relationship // initialize the two arrays $ left_array = array (); // less than the benchmark $ right_array = array (); // for ($ I = 1; $ I <$ length; $ I ++) {if ($ base_num> $ arr [$ I]) {// Put $ left_array [] = $ arr [$ I];} else {// Put $ right_array [] = $ arr [$ I] on the right;} // recursively call this function $ left_array = quick_sort ($ left_array); $ right_array = quick_sort ($ right_array ); // merge return array_merge ($ left_array, array ($ base_num), $ right_array );}

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 used...

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.