Php implements four basic sorting algorithms _ PHP Tutorial

Source: Internet
Author: User
Php implements four basic sorting algorithms. Php implements four basic sorting algorithms: $ arr (, 39). it uses four sort algorithms for sorting bubble sorting: (train of thought: for the number of unsorted orders, four basic sorting algorithms are implemented from php.
Sort array: $ arr (, 78, 39 );

Sort by four sort algorithms

Bubble sorting: (train of thought: compare and adjust the number of unsorted data from the past to the next, with a large sinking and a small increase)

$ 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 ;}

Select sorting: (Find the smallest number in a set of numbers and exchange the position with the first number, and then find the minimum number to exchange with the second number in the remaining number,

Continue once until the second to last number is compared with the last number)

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 ;}

Insert sorting: assume that the previous number is already sorted. now we need to insert the nth number into the previous number so that the n number is sorted.

This repeats until all the rows are sorted)

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 ;}

Quick sorting: Select a benchmark element, usually the first or last element. After a scan, the columns to be sorted are divided into two parts,

A part is smaller than the reference element, and a part is greater than or equal to the reference element. at this time, the reference element is in the correct position after sorting, and then recursive using the same method.

The two parts of sorting. )

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 );}


Sort sorting array: $ arr (, 78, 39); sort by four sort algorithms bubble sorting :( train of thought: Number of unsorted orders, from...

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.