"Chaos" algorithm -- directly insert and sort

Source: Internet
Author: User

Disclaimer: copyright. You are welcome to reprint it. Contact mailbox: yiluohuanghun@gmail.com]

Direct sorting is a common algorithm. Not to mention, directly cut into the text.

1. Basic Ideas

Assume that the records to be sorted are stored in the array R [1. n. In the initial phase, R [1] is a self-contained ordered zone, and the unordered zone is R [2. n]. From I = 2 until I = n, insert R [I] into the current ordered zone R [1 .. I-1] sequentially to generate an ordered zone containing n records.

2, The I-1 directly inserted sort:

Generally, a record R [I] (I = 2, 3 ,..., N-1) insert to the current ordered area, so that after insertion, the records in the range are still guaranteed to be sorted by the keyword ordered operation called the I-1 directly insert sort.

During a certain period of time in the sorting process, R is divided into two subintervals R [1 .. i-1] ordered area with sorted order) and R [I .. n] unordered parts, which can be called unordered areas ).

The basic operation of directly inserting sorting records is to insert the 1st records of the current unordered partition into the ordered partition R [1 .. i-1] in the appropriate position to make R [1 .. i] into a new ordered area. This method adds one record to the ordered area at a time, which is usually called the increment method.

The insertion sorting is very similar to the cards in poker. You do not need to sort out the 1st cards you have touched. After that, you can touch the top 1 card from the card (unordered area) on the table and insert it to the correct position in the left-hand card (ordered area. In order to find the correct position, you must compare the selected cards from left to right (or from right to left) with the ones in the left hand.

650) this. width = 650; "src =" http://img1.51cto.com/attachment/201308/190359736.png "title =" quick picture .png "/>

Insert sorting method directly

1. Simple Method

First in the current ordered zone R [1 .. find the correct Insertion Location k for R [I] in I-1] (1 ≤ k ≤ i-1); then place R [k .. all records in I-1] Move one location behind, freeing up space on k locations to insert R [I].

Note::

If the keyword of R [I] is greater than or equal to the keyword of all records in R [1 .. I-1], R [I] is inserted to the original location.

2. Improvement Methods

A method that allows you to search for comparison operations and record moving operations in turn.

Specific Practices:

Record the keywords of the record R [I] from right to left and record R [j] (j = I-1, I-2 ,..., 1) keyword comparison:

① If the keyword of R [j] is greater than the keyword of R [I], the R [j] is moved back to a position;

② If the keyword of R [j] is less than or equal to the keyword of R [I], the search process ends, and j + 1 is the insert position of R [I.

Records with larger keywords than those of R [I] have been moved backward, so the position of j + 1 has been vacated, you only need to insert R [I] directly to this position to complete the sort of insertion directly.

Int insertSortArray (int array [], int len) {int index = 1; int bottom, tmp; for (; index <len; index ++) {if (array [index] <array [index-1]) {tmp = array [index]; array [index] = array [index-1]; for (bottom = index-1; tmp <array [bottom]; bottom --) // shift {array [bottom + 1] = array [bottom];} array [bottom + 1] = tmp;} return TRUE ;}

The role of the sentry guard:

1. Store copies of R [I] as temporary variables.

2. It is used in the search loop to monitor whether the subscript variable j exceeded.

Algorithm efficiency:

The best case of time complexity is that the order has been sorted, the number of comparisons is n-1, and the number of moves is 0. The worst case is insertion sorting in reverse order, the average moving times and comparison times are O (n * n ). The space complexity is O (1 ).

Sorting features:

1. It is a stable sorting method.

2. It is suitable for close sorting.

3. Suitable for n smaller cases.

4. The final position of an element cannot be determined until the last sorting process.

From the perspective of space, it only needs the secondary space R [0] of a record; from the perspective of time, n records need to be inserted in n-1 troughs, comparison with keywords and moving of records are required for each trip, but the number of comparisons is not fixed. The best case is that records are already arranged in an orderly manner. You only need to compare each record to find the position of the inserted record. You do not need to move the record, but the complexity is On ); the worst case is that records are stored in reverse order. Each record must be compared with the previous keyword and moved to the record. The complexity is On * n ). Therefore, the average performance complexity is On * n ).

Therefore, direct insertion of sorting algorithms is very suitable for situations where records are basically ordered and the number of records is not many.


This article from the "Yi fall Dusk" blog, please be sure to keep this source http://yiluohuanghun.blog.51cto.com/3407300/1266966

Related Article

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.