Insert sorting in the PHP sorting algorithm series, and php Sorting Algorithm

Source: Internet
Author: User

Insert sorting in the PHP sorting algorithm series, and php Sorting Algorithm

Insert sort

There is an ordered data sequence that requires inserting a number in the sorted data sequence, but the data sequence is still ordered after insertion, in this case, we need to use a new sorting method-insert sorting method. The basic operation of insert sorting is to insert a data to the sorted data, in this way, a new ordered data with a number plus one is obtained. The algorithm is suitable for sorting a small amount of data, and the time complexity is O (n ^ 2 ). Is a stable sorting method. The insert algorithm divides the array to be sorted into two parts: the first part contains all the elements of the array, except the last element (so that the array can have more space to insert ), the second part only contains this element (that is, the element to be inserted ). After sorting the first part, insert the last element to the first part of the sorted part.

Principle

The basic idea of Insertion Sort is to insert a record to be sorted to an appropriate position in the Subsequence in the preceding sorted order according to its keyword size, until all records are inserted.
Set the array to a [0... N-1].

1. Initially, a [0] is a self-contained ordered zone, and the unordered zone is a [1 .. n-1]. Make I = 1
2. merge a [I] into the current ordered Zone a [0... A [0... I.
3. I ++ and repeat the second step until I = n-1. Sorting is completed.

PHP code implementation

Function insertSort ($ arr) {// obtain the length of the string to be sorted $ length = count ($ arr); // assume that the first string is ordered, therefore, compare for ($ I = 1; $ I <$ length; $ I ++) from $ I) {// store the value to be compared $ tmp = $ arr [$ I]; for ($ j = $ I-1; $ j> = 0; $ j --) {// if the inserted value is relatively small, the next element is moved one by one and the value is inserted into if ($ tmp <$ arr [$ j]). {$ arr [$ j + 1] = $ arr [$ j]; $ arr [$ j] = $ tmp;} else {break ;}} return $ arr ;}

Algorithm time complexity Calculation

In the best case (the element has already sorted the order): You only need to loop for n-1 times, and the time complexity O (n)
In the worst case (the element is in reverse order): number of times to be adjusted cyclically: [n * (n-1)]/2, time complexity is O (n ^ 2)
Average time complexity: O (n ^ 2)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.