Introduction to algorithms 2.1 Insert sorting

Source: Internet
Author: User
In section 2.1 of Introduction to algorithms, we use insertion sorting as an example to describe how to get started with algorithms. This article also writes this section according to the book. implemented in VB. the general idea of inserting sorting is as if we caught a card and inserted it into an existing card in our hand, and ensure that the inserted cards are sorted in order. that is to say, each time before inserting a new card, the cards in the hand are arranged in order, as long as you find the new

In section 2.1 of Introduction to algorithms, we use insertion sorting as an example to describe how to get started with algorithms. This article also writes this section according to the book. implemented in VB. the general idea of inserting sorting is as if we caught a card and inserted it into an existing card in our hand, and ensure that the inserted cards are sorted in order. that is to say, each time before inserting a new card, the cards in the hand are arranged in order, as long as you find the new

In section 2.1 of Introduction to algorithms, we will take insertion sorting as an example to describe how to get started with algorithms. This article also writes this section in accordance with the book. It is implemented in VB language.

The general idea of inserting sorting is as if we caught a card and inserted it into an existing card in our hand, and ensure that the inserted cards are sorted in order.

That is to say, each time before inserting a new card, the cards in the hand are arranged in order, as long as you find the position of the new card to be inserted, plug in, and then move all the cards after this position back to a position, note that only one location is moved. in this way, a card is inserted until all cards are inserted.

There are several steps in this process. One is to take out the card to be inserted, the other is to find the position to be inserted, the third is to insert the card, and move the card after the inserted position back.

Each step of these three steps must be the ultimate, that is, the minimum number of cards to be played, the minimum number of times to be inserted, and the minimum number of cards to be moved.

Below is the code and detailed code comments.

Private Sub InsertSort (Data () As Integer) Dim I As Long, j As Long, k As Integer If Data. length <= 1 Then Return 'in VB. in. NET, the array subscript starts from 0 and is inserted into the sorting. You only need to insert 2nd numbers into the original array, that is, you have a card in your hand, and then insert it. for I = 1 To Data. length-1 'Save the number k = Data (I) 'to be inserted. in sorted numbers, compare from the end to the front, instead of from the back to the back. 'This does not need to traverse the entire sorted array, just move all those greater than the number to be inserted' and note that it is compared from the I-1, not from the I, in this way, the j = I-1 'can be compared less. Note that> k is used below, instead of> = k, which can reduce the number of moves. in addition, short match AndAlso should be used to prevent Data (j) from crossing the border when j is finally-1. while j> = 0 AndAlso Data (j)> K' all move Data (j + 1) = Data (j) j = j-1 End While 'is greater than the number to be inserted. Next, insert the number to be inserted, here, you can directly use the preceding j to get the location to be inserted. 'in the preceding while, j has been reduced by 1 and must be added back. data (j + 1) = k Next End Sub
Amazed at the introduction to algorithms, there are not many operations at a time, which is quite subtle.

In particular, we have already sorted the order of the cards in our hands and compared them from the back to the front, so that we can find the location and move the data one time.


Next, let's talk about semi-insert sorting. the statement about semi-insertion sorting is displayed on the Internet. It is said that this statement is more efficient than direct insertion sorting. The principle is to use the binary method to locate the position and move it, insert data.

But in fact, this method is not as efficient as directly inserting and sorting in the above Code, because in the above Code, moving data and finding locations are merged, and no matter how sort is inserted, the number of data moves cannot be reduced, so the code above is equivalent to reducing the number of locations directly to 0, which is certainly more efficient than searching a location using the binary 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.