Sort Algorithm -- insert sorting directly

Source: Internet
Author: User

I. Basic Idea of direct insertion and sorting: Insert a record to an ordered table that has already been sorted to obtain a new ordered table with an increase of 1 in the number of records.

That is, the first element is extracted from the unordered table each time, and inserted into the proper position of the ordered table to make the ordered table still orderly.

The first step is to compare the first two numbers, and then insert the second number into the ordered table by size. The second step is to scan the third data and the first two numbers from the back to the front, insert the third number to the ordered table by size. perform the following operations in sequence. After (n-1) scanning, the entire sorting process is completed.

ExampleCode:

# Include <iostream>
# Include <malloc. h>
Using namespace STD;

# Define maxsize 10
Typedef struct
{
Int R [maxsize];
Int length;
} Sqlist;

Void insertsort (sqlist * l)
{
Int I, j, k = 0;
Int temp;
For (I = 1; I <L-> length; I ++)
{
Temp = L-> r [I];
For (j = I-1; j> = 0 & L-> r [J]> temp; j --)
{
L-> r [J + 1] = L-> r [J];
}
L-> r [J + 1] = temp;

 
Cout <"I =" <I <Endl;
For (k = 0; k <L-> length; k ++)
{

Cout <L-> r [k] <'';
}
Cout <Endl;

}

}

Void main ()
{
Int I, cout = 0;
Int A [] = {9, 8 };

Sqlist * l;
L = (sqlist *) malloc (sizeof (sqlist ));
For (I = 0; I <10; I ++)
{
L-> r [I] = A [I];
Cout ++;

}
L-> length = cout;

// Cout <"original array data sequence:" <Endl;
For (I = 0; I <10; I ++)
Cout <L-> r [I] <'';
// Cout <"result of each sort:" <Endl;
Insertsort (L );

}

Ii. Analysis of directly inserted sorting complexity

in terms of space complexity, it only requires a record's auxiliary space. Therefore, the key is its time complexity.
In the best case, that is, the table to be sorted itself is ordered, so we have compared the number of times, so there is no moving record, and the time complexity is O (n ).
when the worst case is that the table to be sorted is in reverse order, for example, {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, it needs to be compared, the number of records moved has reached the maximum.
If the sorting records are random, the average comparison and moving times are about N2/4 times based on the same probability principle. Therefore, we can conclude that the time complexity of direct insertion sorting is O (n2 ). From this point, we can see that the same O (n2) time complexity, direct insertion sorting method is better than bubble and simple selection sorting.

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.