Sort algorithms for algorithm learning: insert sorting (direct insertion sorting, semi-insertion sorting, and 2-way insertion sorting)

Source: Internet
Author: User

Introduction:

Insert sorting is the simplest and easier-to-understand sorting algorithm. This article describes in detail the direct insertion sorting, provides implementation, briefly introduces the semi-insertion sorting, and provides two types of insert sorting: 2-path insertion sorting and table insertion sorting, but does not provide specific implementation.

1. Sort directly inserted

The basic operation of directly inserting sorting records is to insert a record to an ordered table in the sorted order to obtain a new ordered table with 1 Increase in the number of records.

Algorithm Description:

Step 1. Extract the 1st records from a group of records to be sorted as a group of ordered records (of course, there is only one record in this group ).

Step 2. Extract the records from the records to be sorted and insert them to the records sorted in the previous order.

Step 3. Repeat Step 2 until the last record in the group to be sorted is inserted into the sorted record.

For example, insert a sequence of records: 49, 38, 65, 97, 76, 13, 27, and 49.

Step 1: select the first record in the set of records to be sorted as an ordered record, and obtain the ordered record and unordered record as follows:

Ordered records: {49} unordered records: {38, 65, 97, 76, 13, 27, 49}

Step 2: select the first record 38 in the unordered record and insert it into the ordered record {49}. The new ordered record and unordered record are as follows:

Ordered records: {38, 49} unordered records: {65, 97, 76, 13, 27, 49}

Step 3: Repeat Step 2. Each time a record is inserted, a new set of ordered and unordered records are obtained as follows:

Ordered records: {38, 49, 65} unordered records: {97, 76, 13, 27, 49}

Ordered records: {38, 49, 65, 97} unordered records: {76, 13, 27, 49}

Ordered records: {38, 49, 65, 76, 97} unordered records: {13, 27, 49}

Ordered records: {13, 38, 49, 65, 76, 97} unordered records: {27, 49}

Ordered records: {13, 27, 38, 49, 65, 76, 97} unordered records: {49}

Step 4: insert the last record 49 to the ordered record to complete the insertion sorting process. The result is as follows:

Ordered records: {13, 27, 38, 49, 49, 65, 76, 97} unordered records :{}

Note:

1. When a new record is inserted into an ordered record, the Record Comparison and movement should be completed to compare the records to be inserted with the ordered record, if the number of records to be inserted is smaller than that of the ordered records, the ordered records are moved backward.

2. When comparing the records to be inserted with the ordered records, be sure not to cross-border operations.

Sample Code 1 (c ):

`/** Author: Li Bing Date: 2014-9-6 * Email: [email protected] * @ array: the pointer to the records * @ length: the length of the records */void insertsort (INT array [], int length) {If (array = NULL | length <= 0) return; int I, J; for (I = 1; I <length; I ++) {int TMP = array [I]; // auxiliary space of the record // Insert the record to be sorted into the ordered table for (j = I; j> 0 & array [J-1]> TMP; j --) array [J] = array [J-1]; array [J] = TMP ;}}`

Sample Code 2 (c ):

`/** Author: Li Bing Date: 2014-9-6 * Email: [email protected] * @ array: the pointer to the records * @ num: the length of the records */void insertsort (INT array [], int num) {If (array = NULL | num <= 0) return; for (INT I = 1; I <num; I ++) {Int J = I-1; int TMP = array [I]; while (j>-1 & array [J]> array [I]) {array [J + 1] = array [J]; j --;} array [J + 1] = TMP ;}}`

Function Detection:

1. Pass the Normal Array pointer and array length to the above function, which can be sorted normally.

2. Pass the NULL pointer or an array smaller than 0 to the sorting function above, and an error can also be detected.

Summary:

1. the time complexity of direct insertion and sorting is O (n ^ 2). From the perspective of space, it only needs the auxiliary space of one record, and the stability is stable.

2. Directly inserting the sorting algorithm is simple and easy to implement. When the number of records to be sorted is small, it is a good sorting method.

Ii. Semi-insert sorting

Semi-insert sorting: directly insert sorting, mainly to complete the "comparison" and "move" operations. Because direct insertion and sorting are performed for search and insertion in an ordered table, the "Search" operation can be achieved by "half-lookup, the insert sorting is called semi-insert sorting.

Compared with direct insertion sorting, semi-insertion sorting only performs binary search based on the ordered table in the process of searching for insertion points, reducing the number of comparisons between keywords, however, the number of records moved remains unchanged. Therefore, the time complexity of semi-insertion sorting is still O (N ^ 2 ). In addition, the secondary space required for semi-insertion sorting is the same as that for direct insertion sorting.

The Code is as follows:

Sample Code (c ):

`/** Author: Li Bing Date: 2014-9-6 * Email: [email protected] * @ array: the pointer to the records * @ num: the length of the records */void insertsort (INT array [], int num) {If (array = NULL | num <0) return; For (INT I = 1; I <num; I ++) {int low, high, mid; Low = 0; high = I-1; // use binary search, find the position to be inserted while (low <= high) {mid = low + (high-low)> 1); // This write method, can effectively avoid overflow if (array [I]> array [Mid]) Low = Mid + 1; elsehigh = mid-1;} int temp = array [I]; // move the record for (Int J = I; j> low; j --) array [J] = array [J-1]; array [low] = temp; // insert record }}`

Summary:

1. The complexity of semi-insertion sorting is still O (N ^ 2), and the auxiliary space is still 1.

2. Semi-insert sorting reduces the number of comparisons between keywords.

3. 2-path insertion sorting

2-The insertion sorting method is improved based on the semi-insertion sorting method. The goal is to reduce the number of records moving in the sorting process, but the auxiliary space for N records is required. 2-path insertion sorting can only reduce the number of records to be moved, but cannot avoid moving records. If you do not want to move records during the sorting process, you can only change the storage structure and insert and sort the tables.

Iv. Sort table Inserts

The basic operation of table insertion and sorting is to insert a record into an ordered table in the sorted order. Compared with direct insertion sorting, the difference is that the records are moved only by modifying the 2N pointer value, and the number of comparisons between keywords required in the sorting process is the same. Therefore, the time complexity of table insertion sorting is still O (N ^ 2 ).

References:

1. Edited by Yan Weimin Wu Weidong, data structure (C language version)

2. Data Structure and algorithm analysis-C language description Mark Allen Weiss translated by Feng shunxi

3. http://blog.csdn.net/cjf_iceking/article/details/7916194

4. http://blog.csdn.net/zhangxiangdavaid/article/details/27373183

5. http://blog.csdn.net/morewindows/article/details/6665714

Sort algorithms for algorithm learning: insert sorting (direct insertion sorting, semi-insertion sorting, and 2-way insertion sorting)

Related Keywords:

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.