Article 1-Introduction to algorithms-insert sorting

Source: Internet
Author: User

Applicability of insert sorting: it is an effective algorithm for sorting a small number of elements.

Because the pseudocode in the book is too classic, you can't help copying it:

// Pseudocode Insertion-sort (a) for J = 2 to. lengthkey = A [J] // insert a [J] into a [1... (J-1)] I = J-1; while I> 0 and a [I]> Keya [I + 1] = A [I] I = i-1A [I + 1] = Key

C ++ implementation:

/* Input: A [1... (n-1)] * original address sorting * Version1 */template <typename T> void insertion_sort (T * a, int N) {int key = 0; // key initialization for (Int J = 1; j <n; ++ J) {// Insert the working key of a [J] = A [J]; int I = J-1; while (I> = 0 & A [I]> key) {A [I + 1] = A [I]; I = I-1 ;} A [I + 1] = key ;}}

Let's talk about something theoretical. This is the most important thing:

In this chapter, the author introduces an important algorithm analysis tool, which does not change cyclically (that is, the feature of every for or while loop). This is very important to prove the correctness of the algorithm.

If I want to prove the correctness of this algorithm, I must have a short sequence to test. I found that before each loop, a [1... J-1] (since the pseudo-code array is from 1, C ++ is from 0, here the analysis is based on the pseudo-code as the standard) are sequential ----- book cycle unchanged expression:

At the beginning of each iteration of the for loop, the Child array a [1 .. J-1] consists of elements originally in a [1 .. J-1], but has been sorted


The proof of the non-variant cycle mainly consists of three parts: initialization, persistence, and termination. (Here we will skip the proof)


After the algorithm is proved to be correct by means of a loop without variation, it is easy to analyze the complexity, and the complexity is θ (N ^ 2 ).


Perception: sorts a group of numbers. The idea in my mind is to select the smallest element and put it in the first place. Then, select the smallest element and put it in the second place... This brute force method is less efficient than insertion sorting. So where is the charm of insertion sorting? Program Design method. The Program Design Method Used for insert sorting is ---Addition Method: After sorting the sub-array a [1 .. J-1], insert a single element a [J] into the appropriate position of the sub-array to generate a sorted ArrayThis is an effective and very valuable idea. For example, the design of many dynamic plans is to use the increment method.






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.