Insert sort: insert sort directly. Insert sort directly.

Source: Internet
Author: User

Insert sort: insert sort directly, insert sort directly

1. Sort directly inserted

1. Thoughts

Direct sorting can be divided into two parts: ordered and unordered.

From this figure, we can see the idea of directly inserting sorting.

Compare the first part of the unordered part with the ordered part.

Compare the data from the back of the ordered part, and then constantly move the data position of the ordered part.

Static void InsertSort (List <int> list ){
// Starts from the second number and loops for n-1 times for (int I = 1; I <list. Count; I ++ ){
// Take out the number of items to be sorted so that int temp = list [I] can be moved back.
// J is the final subscript of the Maximum/minimum number int j = I; while (j> = 1 & temp <list [j-1]) {
// Move the data that meets the condition one by one, and leave it blank to insert the move table list [j] = list [j-1]; j --;} list [j] = temp ;}}

 

2. Complexity

In the best case of direct insertion sorting, the time complexity is O (n), and in the worst case, the complexity is O (n2 );

For proof, see:

Insert sorting and Complexity Analysis

 

3. Direct insertion and sorting vs quick sorting

From the code above, direct insertion of sorting requires constant data movement. If a continuous integer is encountered, more data will be moved. can you improve the direct insertion of sorting for this problem?

Can I skip the comparison during the comparison?

 

Ii. Hill sorting

1. Thoughts

During the comparison, the incremental comparison method is introduced.

Step 1. Make incremental d = count/2, regard the number of every d as a group of unordered numbers, and then insert and sort the unordered numbers in this group.

Step 2. Make incremental d = d/2 and perform the same operation as step 1 until d = 1

Code:

static void ShellSort(List<int> list){    int step = list.Count / 2;    while (step >= 1)    {        for (int i = step; i < list.Count; i++)        {            var temp = list[i];            int j = i;            while (j >= step && temp < list[j - step])            {                list[j] = list[j - step];                j -= step;            }            list[j] = temp;        }        step = step / 2;    }}

Hill sorting and direct insertion sorting. The code in the middle is basically the same. The difference is only the dimension. The dimension for direct insertion of sorting is fixed 1,

The dimensions of Hill's sorting are changed. From the code point of view, it is actually quite simple. Just change it by directly inserting the sorting.

 

2. Complexity

The time complexity of Hill's sorting is the same as the time complexity of directly inserting sorting. Can you believe it.

 

Iii. Direct insertion sorting vs Hill sorting

Now that hill sorting is directly inserted into the sort version of the sorting, who are more powerful? Will the change be worse?

Static void Test () {// compare for (int I = 1; I <= 5; I ++) {List <int> list = new List <int> (); List <int> listA = new List <int> (); // insert 2 k random numbers to the array for (int j = 0; j <10000; j ++) {Thread. sleep (1); list. add (new Random (int) DateTime. now. ticks ). next (0, 100000);} listA. addRange (list); Console. writeLine ("\ n" + I + "Times comparison: {0 }... ", string. join (",", list. take (10); Stopwatch watch = new Stopwatch (); watch. start (); InsertSort (list); watch. stop (); Console. writeLine ("\ n direct insertion sorting time:" + watch. elapsedMilliseconds); Console. writeLine ("Top 10 output:" + string. join (",", list. take (10 ). toList (); watch. restart (); ShellSort (listA); watch. stop (); Console. writeLine ("\ n Hill sorting time-consuming:" + watch. elapsedMilliseconds); Console. writeLine ("Top 10 output:" + string. join (",", listA. take (10 ). toList ()));}}

From the result, the improvement effect of Hill's sorting is quite obvious, but Hill's sorting is not a stable sorting method. That is to say, it may still be slower than fast sorting.

 

Related Article

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.