Insert sort-Direct insert sort

Source: Internet
Author: User

Today, Dapeng learned how to insert directly into the sort order.

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 a group of records to be sorted in sequence and insert them to the records in the sorted order.

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

Eg.1: insert directly into the following group of records: 49, 38, 65, 97, 76, 13, 27, 49.

Step 1. Select the first record in the set of records to be sorted as an ordered record. The obtained ordered record and unordered record are 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 records 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. Know that the last record 49 is inserted into 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 data to be wiped is smaller than the ordered data, the sorted data is moved backward.

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

 

Java implements the following sorting method:

 

Public classDirectinsert {

 

Public static voidMain (string [] ARGs ){

//TodoAuto-generatedmethod stub

Int[] A = };

System.Out. Print ("Before sorting :");

For(IntI: ){

System.Out. Print (I + ",");

}

System.Out. Println ("sorted ");

Insertsort();

System.Out. Print ("sorted :");

For(IntJ: ){

System.Out. Print (J + ",");

}

}

 

Private Static voidInsertsort (Int[] ){

//TodoAuto-generatedmethod stub

IntI, J;

For(I = 1; I <A. length; I ++ ){

IntTemp = A [I];

For(J = I-1; j> = 0 & temp <A [J]; j --){

A [J + 1] = A [J];

}

A [J + 1] = temp;

}

}

}

 

Time Complexity: O (N ^ 2). If the sequence to be sorted is positive, the time complexity can be increased to O (n ).

There are also several improved insertion sorting methods for direct insertion sorting: semi-insertion sorting (because the basic operation of insert sorting is to search and insert in an ordered table, this search operation can be implemented using this search, and the insert sorting is the semi-insert sorting), 2-path insertion sorting, and table insertion sorting (after a period of time, Dapeng elder brother added ).

 

Insert sort-Direct insert sort

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.