Data structure (44) Insert Sort (1. Direct Insert sort 2. Hill sort)

Source: Internet
Author: User

First, the basic idea of inserting sort

Starting from the initial ordered subset, the new data elements are constantly inserted into the appropriate position of the ordered sub-set, so that the number of data elements in the child collection is increasing, and when the sub-collection equals the collection, the insertion sort algorithm ends. The commonly used insertion sorting algorithm has two kinds of direct insertion sort and hill sort.

  

Second, direct insertion sort

1. Definition of direct Insert sort

The basic idea of a direct insert sort is to insert the data element to be sorted sequentially into the appropriate position of the sorted data element sub-collection by the size of its value. The number of data elements for a sub-collection grows from only one data element at a successive start. Sorted when the child collection size is eventually the same as the collection size.

2. Implementation of direct insertion sequencing

     Public Static voidStraightinsertionsort (int[] L) {intI, J, temp;  for(i = 0; i < l.length-1; i++) {Temp= L[i+1];//Save the data element to insertj =i;  while(J >-1 && temp <= l[j]) {//inserting temp into the original array collectionL[J+1] =L[j]; J--; } l[j+1] =Temp }    }

Int[] array1 = {9,1,5,8,3,7,4,6,2};
Initially, the L0 in the sub-collection is already sorted, which is {9}
I=0, temp=l1=1,j=0,0 is greater than 1 and 1 is less than 9, then l1=9,j=-1,l0=1, 1 is inserted into the front of 9, the collection {1,9}
I=1, temp=l2=5,j=1,1 is greater than 1 and 5 is less than 9, then l2=9,j=0,0 is greater than 1 but 5 is not less than 1, then l1=5, in the collection {1,5,9}
i=2, Temp=l3=8, about 8 and 9 compared, 8 inserted in front of 9, 8 and 5 compared, motionless, 8 and 1 compared, also fixed, in the collection {1,5,8,9}
...

3. Performance analysis of direct insertion sequencing

(1) Time complexity of O (N2)

    • The best case is that the original data set is all sorted. At this time, the loop of the inner while loop is 0, and the number of each data element in the outer for loop is 1, and the number of assignment statements for the data element is 2. Therefore, the total number of comparisons during the sorting process is n-1, and the number of moves is 2 (n-1), at which time the complexity is O (n).
    • The worst case scenario is the reverse order of the original data set. At this time, the number of cycles of the inner while loop is I, so that the total number of comparisons in the outer for loop is (+) + (2+1) +...+ (n-1+1) = (n-1) (n+2)/2, and the number of moves is (1+2) + (+ +) +...+ (n-1+2) = ( n-1) (n+4)/2. The time complexity at this point is O (N2).
    • The random case is that the size of the data set is arranged randomly. The expectation of the number of comparisons and the expected number of moves is N2/4, at which time the complexity is O (N2).

(2) The space complexity is O (1).

(3) is a stable sorting algorithm.

Iii. sort of Hill

1. Definition of Hill Sort

The basic idea of hill sorting is to divide the data elements to be sorted into groups, to sort the data elements in the same group by direct insertion; The number of groups is reduced successively; when all the data elements are completed, the sorting process ends within a group. The hill sort is also known as narrowing the incremental sort.

In the performance analysis of the direct insertion sorting algorithm, it can be concluded that the more the original data set is close to the order, the more time efficiency of the direct insertion sorting algorithm, the time efficiency is between O (n) ~o (n2). This conclusion is the basis on which the hill sorting algorithm can be established. The Hill sorting algorithm divides the data elements to be sorted into groups, which are sorted by the direct insertion sort algorithm within the group, and when a group of groups is merged into a group, the set of data elements will be in close order, so that the time efficiency of the direct insertion sorting algorithm in each group is very good, and the time efficiency is very high.

  

Data structure (44) Insert Sort (1. Direct Insert sort 2. Hill 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.