Eight sorting algorithms in PHP insert sorting (-) insert sorting directly

Source: Internet
Author: User
Insert sorting (-) directly insert sorting directly into the eight sorting algorithms of PHP:

Insert sorting is one of the simplest sorting algorithms. for sequences with N elements, insert sorting is composed of N-1 sort. It works by constructing an ordered sequence. for unordered data, it scans the sorted sequence from the back to the front and finds the corresponding position for insertion.

Steps for inserting a sorting algorithm:

  1. Consider the first element of the first sequence to be sorted as an ordered sequence, and treat the second element to the last element as an unordered sequence.

  2. Scan unordered sequences from start to end and insert each scanned element into an appropriate position of the ordered sequence (here you need to pay attention to a problem, if there is an element in the ordered sequence that is equal to the element to be inserted, the element to be inserted will be located after this element. in this way, the insertion sorting is stable. If it is inserted before this element, Insertion sorting in this way is unstable.

    For the above steps, we can summarize the Insertion sorting algorithm as follows:

    In the L-level sorting, move the element on position L to the right position of the element in front of the L + 1, where the first L element is ordered.


Insert sorting algorithm analysis:

Since each nested loop consumes N iterations, the time complexity of Insertion sorting is O (N ^ 2 );

The insertion sorting described above is the direct insertion sorting in the Insertion sorting algorithm, and the insert position in the ordered sequence is searched one by one. Therefore, the insertion sorting is called directly.

Welcome to our follow-up sorting algorithm. personal projects include: Capital companies (www.ya-jing.cn)

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.