PHP Eight sort algorithm--insert sort (-) Direct Insert Sort

Source: Internet
Author: User

Direct Insert Sort:

Insert sort is one of the simplest sorting algorithms for the n sequence of elements, insert sort by n-1 The sequencing composition. It works by constructing an ordered sequence that, for unsorted data, is scanned from the back forward in the sorted sequence to find the appropriate location to insert.

insert sort algorithm step:

  1. The first element of the first sequence to be sorted is treated as an ordered sequence, and the second element to the last element as an unordered sequence.

  2. Scan an unordered sequence from beginning to end, inserting each element scanned into the appropriate position of an ordered sequence (here you need to note that if there is one element in an ordered sequence that is equal to what is to be inserted, the element to be inserted is traced back to the element, so that the insertion order of the method is stable.) If inserted in front of this element, then the insertion sort of this method is not stable

    For the above steps, you can summarize the algorithm for inserting sorting into:

    in the L -order, move the element on position L to the left to its correct position in the first l+1 element, where the first l element is ordered .


insert sort algorithm analysis:

Because each of the nested loops spends n iterations, so the time complexity for inserting a sort is O (n^2);

The insertion sort explained above is a direct insert sort in the insertion sorting algorithm, where the insertion position is found in an ordered sequence, and is then searched for directly, so it is called a direct insert sort

Please continue to follow our sorting algorithm. Individual projects are: Co-financing Company (www.ya-jing.cn)

PHP Eight sort algorithm--insert sort (-) Direct Insert Sort

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.