Seven php basic sorting methods

Source: Internet
Author: User
This article mainly introduces seven php sorting methods in detail. if you are interested, you can refer to this article to summarize the seven commonly used sorting methods and use php for implementation.

1. Insert directly to sort

/** Insert sorting directly. The principle of insert sorting is: the elements before the current insertion position are ordered. * If the element inserted at the current position is larger than the last element of the ordered element, then do nothing. * otherwise, locate the inserted position in the ordered sequence and insert */function insertSort ($ arr) {$ len = count ($ arr ); for ($ I = 1; $ I <$ len; $ I ++) {if ($ arr [$ i-1]> $ arr [I]) {for ($ j = $ I-1; $ j >=0; $ j --) {$ tmp = $ arr [$ j + 1]; if ($ tmp <$ arr [$ j]) {$ arr [$ j + 1] = $ arr [$ j]; $ arr [$ j] = $ tmp ;} else {break ;}}}return $ arr ;}

2. Bubble sorting

/* Bubble sorting, bubble sorting thought: perform n-1 batch bubble sorting, and compare and adjust the maximum value to the end of the array (sub-array) */function bubbleSort ($ arr) {$ len = count ($ arr); for ($ I = 1; $ I <$ len; $ I ++) {for ($ j = 0; $ j <$ len-$ I; $ j ++) {if ($ arr [$ j]> $ arr [$ j + 1]) {$ tmp = $ arr [$ j + 1]; $ arr [$ j + 1] = $ arr [$ j]; $ arr [$ j] = $ tmp ;}} return $ arr ;}

3. simple selection and sorting

/* Simple selection and sorting. The thought is to determine the elements from small to large from the first element of the array */function selectSort ($ arr) {$ len = count ($ arr); for ($ I = 0; $ I <$ len; $ I ++) {$ k = $ I; for ($ j = $ I + 1; $ j <$ len; $ j ++) {if ($ arr [$ k]> $ arr [$ j]) {$ k = $ j ;}} if ($ k! = $ I) {$ tmp = $ arr [$ I]; $ arr [$ I] = $ arr [$ k]; $ arr [$ k] = $ tmp;} return $ arr ;}

4. Hill Sorting

/* Hill sorting. Hill sorting principle: separates the array into several sub-sequences according to the specified step size, and sorts the sub-sequences respectively (in this case, it is direct) */function shellSort ($ arr) {$ len = count ($ arr); $ k = floor ($ len/2); while ($ k> 0) {for ($ I = 0; $ I <$ k; $ I ++) {for ($ j = $ I; $ j <$ len, ($ j + $ k) <$ len; $ j = $ j + $ k) {if ($ arr [$ j]> $ arr [$ j + $ k]) {$ tmp = $ arr [$ j + $ k]; $ arr [$ j + $ k] = $ arr [$ j]; $ arr [$ j] = $ tmp; }}$ k = floor ($ k/2);} return $ arr ;}

5. fast sorting

/** Fast sorting and fast sorting idea: sort the records to be sorted into two separate parts, the keywords of one part of the record are not greater than * the keywords of the other part of the record, and then the two parts of the record are sorted in a fast manner to achieve the entire sequence order, for details, you need to set a standard keyword for each sort and a pointer to the keyword pointing to the first record and the keyword of the last record respectively. * QuickSort ($ arr, 0, count ($ arr)-1); */function quickSort (& $ arr, $ low, $ high) {if ($ low <$ high) {$ I = $ low; $ j = $ high; $ primary = $ arr [$ low]; while ($ I <$ j) {while ($ I <$ j & $ arr [$ j] >=$ primary) {$ j --;} if ($ I <$ j) {$ arr [$ I ++] = $ arr [$ j];} while ($ I <$ j & $ arr [$ I] <= $ primary) {$ I ++;} if ($ I <$ j) {$ arr [$ j --] = $ arr [$ I] ;}} $ arr [$ I] = $ primary; quickSort ($ arr, $ low, $ i-1); quickSort ($ arr, $ I + 1, $ high );}}

6. heap sorting

/* Heap sorting * // adjust the process in which the sub-heap is a large root heap and $ s is the root position of the sub-heap, $ m is the location of the last element in the heap. function heapAdjust (& $ arr, $ s, $ m) {$ tmp = $ arr [$ s]; // the change to a large root heap may affect the left or right sub-heap. // the for loop is used to ensure that the sub-heap is also a large root heap for ($ j = 2 * $ s + 1; $ j <= $ m; $ j = 2 * $ j + 1) {// locate the publisher in the left and right children of the root node, and then compare it with the root node, // adjust the value if it is large, otherwise, the loop is exceeded according to the features of the big root heap. if ($ j <$ m & $ arr [$ j] <$ arr [$ j + 1]) {$ j ++ ;} if ($ tmp >=$ arr [$ j]) {break;} $ arr [$ s] = $ arr [$ j]; $ s = $ j ;} $ arr [$ s] = $ tmp;} // heapSort ($ arr) {$ len = count ($ arr ); // Adjust the heap to a large root heap from the sub-heap in sequence ($ I = floor ($ len/2-1); $ I >=0; $ I --) {heapAdjust ($ arr, $ I, $ len-1);} // change the root node to the last position in sequence, change the heap to the root heap again, and find the maximum number of times, // Obtain an ordered array by analogy ($ n = $ len-1; $ n> 0; $ n --) {$ tmp = $ arr [$ n]; $ arr [$ n] = $ arr [0]; $ arr [0] = $ tmp; heapAdjust ($ arr, 0, $ n-1);} return $ arr ;}

7. Merge and sort

/* Merge and sort. here we implement two-way merge * // separate the ordered $ arr1 [s .. m], $ arr2 [m + 1 .. n] merged into ordered $ arr2 [s .. n] function Merge (& $ arr1, & $ arr2, $ s, $ m, $ n) {for ($ k = $ s, $ I = $ s, $ j = $ m + 1; $ I <= $ m & $ j <= $ n; $ k ++) {if ($ arr1 [$ I] <$ arr1 [$ j]) {$ arr2 [$ k] = $ arr1 [$ I ++];} else {$ arr2 [$ k] = $ arr1 [$ j ++] ;}} if ($ I <=$ m) {for (; $ I <=$ m; $ I ++) {$ arr2 [$ k ++] = $ arr1 [$ I] ;}} else if ($ j <=$ n) {(; $ j <= $ n; $ j ++) {$ arr2 [$ k ++] = $ arr1 [$ j] ;}} // recursive function MSort (& $ arr1, & $ arr2, $ s, $ t) {if ($ s = $ t) {$ arr2 [$ s] = $ arr1 [$ s];} else {$ m = floor ($ s + $ t)/2 ); $ tmp_arr = array (); MSort ($ arr1, $ tmp_arr, $ s, $ m); MSort ($ arr1, $ tmp_arr, $ m + 1, $ t ); merge ($ tmp_arr, $ arr2, $ s, $ m, $ t) ;}// pair an array $ arr [0 .. elements in n-1] perform two-way merge function mergeSort ($ arr) {$ len = count ($ arr); MSort ($ arr, $ arr, 0, $ len-1 ); return $ arr ;}

Experience
If the number of records to be sorted is n hours, direct insertion sorting and simple selection sorting can be used. when the amount of information in the records is large, the simple selection sorting method is better.
If the records to be sorted are ordered by keywords, direct insertion sorting and bubble sorting are applicable.
If the n value is large, fast sorting, heap sorting, and merge sorting can be used. In addition, quick sorting is considered as the best internal sorting method.

The above is all the content of this article, hoping to help you learn.

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.