PHP Implementation Insert Sort

Source: Internet
Author: User
1. The concept of insertion sequencing

It is through the construction of ordered sequences, for unsorted data, in the sorted sequence from the back forward scan, find the appropriate location and insert.

In fact, we are not unfamiliar, give a small example of reality, we have played poker, get the card, you need to wipe the poker, the general is based on the order of the small to large, a new card, we are not a piece of the comparison, find it suitable position, this is the insertion sort.


2. Code implementation

The function Insertsort (& $arr) {//is the address of the array, so that the value of the array can also change for
    ($i =1 $i <count ($arr); $i + +) {    //is starting from 1, Because the No. 0 is an ordered
        $target = $arr [$i];//Save the target number in a variable
        $j = $i;
        while ($j >0 && $target < $arr [$j-1]) {  //loop forward from the previous start of the target number, the current number is smaller than the target number, move the current number forward one, freeing up a position
            $arr [$j] = $ arr[$j-1];
            $j-;
        }
        $arr [$j] = $target; The loop is over, the target number is found and the target number is placed on the
    position
}
$arr = [12,56,98,32,16,34,2,9,1];
Insertsort ($arr);
Dump ($arr);

Above is the corresponding code, the key step is marked. 3. Complexity Analysis

① first is considered ordered, the second element is compared to the first element, and if it is smaller than the first element, it is inserted before the first element. The third element is sequentially compared to the second element and the first element (the top three elements are ordered); the fourth element, in turn, is compared to the third, second, and first element and is inserted into the appropriate position to form an ordered table (i.e. the first four elements are ordered at this time) so the direct insertion sort algorithm gradually forms an ordered sequence. A local ordered sequence is formed at the front of the table.
② regardless of the initial sequence, the total need for N-1, the first trip is the second element compared to the first element, the second is the third element compared to the first two elements, the third is the fourth element compared to the first three elements ...
③ when the initial sequence is ordered, the first trip is only one time, the second is only one time, and the third is only a comparison ... All you need to do is compare N-1 times to complete the sort. When the initial sequence is in reverse order, the first trip is compared, the second trip is two times, ... The first N-1 trip compares N-1 times. Total N (n-1)/2 times.
The ④ direct insertion arrangement is based on a comparison of two elements of an explicit adjacent position, so the algorithm is stable. The number of comparisons to the sort process is related to the initial state of the queue to be sorted. Each trip arrangement does not uniquely determine the final position of the next element.

So the best case is O (n), and the worst case is O (n2) n squared.

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.