Insert sorting of sorting algorithms

Source: Internet
Author: User

Insert sorting, as the name implies, is to find the appropriate location and insert records to make the entire table orderly. Insert sorting includes direct insertion sorting, semi-insertion sorting, 2-way insertion sorting, and Hill sorting.ArticleThis section describes direct insertion sorting, semi-insertion sorting, and Hill sorting.

1. Sort directly inserted

The best example of insertion sorting is playing cards. Many books use the process of capturing playing cards as an example to describe the insertion sorting process. The process of card capturing is similar to that of inserting sorting, but there is a difference, here, we use an example that is more appropriate for insertion sorting to show the process of insertion sorting. Assume that you are playing cards and you are about to go to the bathroom. If you give a card to your friends, they will only put your cards in your seat and will not sort them for you. When you come back, you picked up and saw the spades in one hand. Good cards:

Now you need to sort the cards from left to right in ascending order.

You start from the leftmost order. The leftmost card is obviously ordered for the first card.

Now, starting from the first 2nd cards on the left, 2nd cards and 1st cards are compared. We found that five cards are smaller than seven and do not need to be moved. The first two cards are sorted.

Next we will look at the 3rd cards:

The four cards of the 3rd black peach cards are compared with the cards on the left in sequence. The four cards are smaller than the seven cards, the four cards should be moved to the left, the four cards are compared with the five cards, and the four-to-five cards are also small.

In this way, the first three cards are sorted;

Look down at the 4th cards:

Compared with the cards on the left, if the game matches the three major cards, you can see that the three cards should be placed at the top of the page, the other three cards are moved back.

After moving, the first four cards have been sorted;

Check the fifth card:

Compared with the previous cards in sequence, we found that eight is bigger than the previous card, that is, eight is bigger than the previous four cards, and you do not need to move the cards, the first five images are sorted;

Let's look at the 6th cards:

We found that the first 6th black peaches were eight more than the first, so we can keep the first six cards in order without moving them;

Finally, let's look at the 7th cards:

The first card, black peach 6, is compared with the previous card. If the first card is bigger than the sixth card, it will be exchanged until the card is smaller than the sixth card or there is no card in front.

Black peach six should be inserted between five and seven, 9, 8, and 7 move in sequence, after the move, the hand is in ascending order.

The above is the whole process of direct insertion and sorting. Let's take a look at the direct insertion sorting.AlgorithmOfCode:

 1   Void Insertsort ( Int * Data, Int  Len ){ 2       Int Sentry; //  Sentinel  3       Int  I, J;  4       For (I = 1 ; I <Len; I ++ ){ //  The sorting starts from the second element. The previous element is sorted by default.  5           If (Data [I] <data [I- 1  ]) { 6 Sentry = data [I]; //  Copy to Sentinel  7               For (J = I- 1 ; Sentry <data [J]; j -- ){  8 Data [J + 1 ] = Data [J]; //  Record move-back  9   }  10 Data [J +1 ] = Sentry; //  Insert to an appropriate location  11   }  12   }  13 }

The idea of directly inserting a sort table is very simple. The length of each sort order table is increased by 1. While finding the entry and exit locations for the record, the post-shift ratio is greater (for non-descending sorting here. Analyzes the time-space complexity of directly inserting sorting algorithms.

In space, only one sentry is needed as the auxiliary space, that is, the space complexity is O (1 );

You need to complete the comparison and moving operations for each sort. In the best case, the records to be sorted have been sorted, and each sort only needs to be compared once, A total of Σ 1 = (n-1) Comparison times, no need to move elements; in the worst case, the records to be sorted are in reverse order. The number of comparisons is Σ I = (n + 2) (n-1)/2, and the number of moves is Σ (I + 1) = (n + 4) (n-1)/2. Therefore, on average, the time complexity of direct insertion sorting is O (n ^ 2 ).

I chose to use this example to go to the bathroom to clear the cards, instead of grabbing the cards, because I think the insertion sorting is in the same order and no additional record table is required. The card capturing process makes people feel that the cards in their hands are different from those that have not yet been captured. The cards on the cards are sorted in disorder when they are returned from the toilet, the card clearing process is performed on this table, obviously there is no other space.

 

Ii. Semi-insert sorting

In direct insertion sorting, each time you search for the insertion position, the insert position is compared in sequence. When we sort the I (I> 1), the previous (I-1) records are sorted, so we can use a half-fold to find the Insert Location. The algorithm is as follows:

 1   Void Binsertsort ( Int * Data, Int  Len ){  2       Int Sentry; //  Sentinel  3       Int  I, J;  4       Int Low, high, middle;  5       For (I = 1 ; I <Len; I ++ ){  6 Sentry = data [I]; //  Copy Sentinel  7 Low = 0  ;  8 High = I- 1  ;  9          While (Low <= high ){ //  Insert Location  10 Middle = (low + high )/ 2 ; //  Half subscript  11               If (Sentry> data [Middle]) //  Insertion point in the upper half District  12 Low = middle + 1  ; 13               Else                          //  Insertion point in the lower half Area  14 High = middle- 1  ;  15   }  16           For (J = I- 1 ; J> = high + 1 ; J -- ){  17 Data [J +1 ] = Data [J]; //  Record move-back  18   }  19 Data [J + 1 ] = Sentry; //  Insert to an appropriate location  20   }  21 }

Semi-insert sorting only reduces the number of comparisons for finding the insert location, but does not decrease the number of moving records. Therefore, the time complexity of semi-insertion is still O (N ^ 2 ).

 

Iii. Hill sorting

Whether it is direct insertion or semi-insertion sorting, the sorting efficiency is relatively low when the record table is to be sorted for a long time. Because the record table is very long, there will be an average number of records moving each row.

For example, assume that the length of the record table is 10005, and the current 1000 records are ordered. Now, when we need to sort 10,001st records, we find that 10,001st records need to be inserted to the first position, A total of 10000 elements need to be moved. Directly inserting the sort and move elements can only move adjacent parts, but cannot move the elements by leaps or bounds.

Hill sorting is to improve the direct insertion sorting from this point. The basic idea is: Split the entire log table to be sorted into several sub-tables, and insert and sort the sub-tables separately. The sub-table length is relatively small, the sorting efficiency is relatively high. The algorithm is as follows:

 1   Void Shellinsert ( Int * Data, Int Len, Int  DK ){  2       Int Sentry; //  Sentinel  3      Int  I, J;  4       For (I = dk; I <Len; I ++ ){  5           If (Data [I-dk]> Data [I]) {  6 Sentry = Data [I];  7               For (J = I-DK; j> = 0 & Amp; sentry <data [J]; j-= DK ){ 8 Data [J + dk] = data [J]; //  Record move-back  9   }  10 Data [J + dk] = sentry; //  Insert to an appropriate location  11   }  12           13   }  14   }  15  16   Void Shellsort ( Int * Data, Int Len, Int Dlta [], Int  Dlen ){  17       Int  K;  18       For (K = 0 ; K <dlen; k ++ ){  19 Shellinsert (data, Len, dlta [k]); //  Incremental sorting of dlta [k]  20   }  21 }

The subsequences in the hill sort are not simply segmented by segments, but are separated by an incremental record to form a subsequence. The three-hop sort increments are 5, 3, and 1 respectively.

We found that in the hill sorting, smaller records are not adjacent partial movements, but jump movements. In the last trip, the increment is 1, and the waiting for record table is basically ordered, the sort can be completed by direct insertion sorting. the time complexity of the hill sort is still O (N ^ 2), but the polynomial coefficient is less efficient than that of direct insertion sorting. In addition, Hill sorting is different from direct insertion sorting and semi-insertion sorting. Hill sorting is an unstable sorting algorithm. Two identical records 49 can be obtained and exchanged before and after sorting, direct insertion sorting and semi-insertion sorting are stable and do not change the relative positions between the same records.

References: [1] Yan Weimin, Wu weiming. Data Structure (C), Tsinghua University Press.

[2] Thomas H. cormen, etc. Introduction to algorithms, Mechanical Industry Press.

Drawing tools: PPT + QQ

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.