7 basic php sorting methods and 7 php sorting methods

Source: Internet
Author: User

7 basic php sorting methods and 7 php sorting methods

This article summarizes seven common sorting methods, which are implemented in php.

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.

Articles you may be interested in:
  • PHP array sorting method Summary recommended favorites
  • The sorting of PHP multi-dimensional arrays is based on an item in a two-dimensional array.
  • PHP Bubble Sorting binary search sequence query two-dimensional array Sorting Algorithm function details
  • Php two-dimensional array sorting
  • Php two-dimensional array sorting method (array_multisort usort)
  • Three methods for fast sorting in php
  • PHP two-dimensional array sorting methods and user-defined functions
  • Sorting method of php array containing Chinese Characters
  • Differences between sorting functions sort, asort, rsort, krsort, and ksort in PHP
  • Code for sorting multi-dimensional arrays by specified values in php

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.