"Deep understanding"-inserting sorting algorithms

Source: Internet
Author: User


Summarize your own understanding of the insertion sort.


Insertion sorting algorithm idea: Each pass inserts an element into the sequence of its keywords, followed by the size of its keyword, and repeats until all elements are inserted.

Insert sort includes: Direct insert sort, binary insert sort, and hill sort.




1. Direct Insert Sort:

public void inserts (int[] a) {for (int i=1;i<a.length;i++)     //n-1 This scan, and then forwards n-1 elements {int temp=a[i];       Each trip will insert a[i] into the preceding sort subsequence int j;for (j=i-1;j>=0&&temp<a[j];j--) {a[j+1]=a[j];  Move the previous larger element backward}a[j+1]=temp;      The temp value arrives at the insertion position}}
the inner loop here is the reason for a[j+1]=a[j] instead of A[I]=A[J]: the first time the inner loop is equal, the j+1 will not be equal to I. So it can only be the former.

2. Hill Sort:

Hill sort is also known as narrowing the incremental sort, the basic idea is grouping the direct insertion sort.

The analysis of the direct insertion sorting algorithm shows that if the data sequence is close to order, the time efficiency is higher and the time efficiency is high when n is small. The hill sort was improved based on these two points for the direct insertion sort.

Algorithm Description:

(1) A data series is divided into groups, each group consists of several elements separated by a distance (called an increment), which is sorted by a direct insertion sort algorithm within a group.

(2) The incremental initial value is usually half the length of the data series, and each subsequent increment is halved, and the last is 1. As the increment decreases gradually, the number of groups decreases, the number of elements in the group increases, and the data sequence is close to order.


Examples are as follows:

public class Shellsort {public static void main (string[] args) {//TODO auto-generated Method stub       int[] Arr={38,55,65 , 97,27,76};       Shell s=new Shell ();       S.shellsort (arr);      S.shellsort2 (arr);//For       (int a:arr)//       System.out.println (a);}} Class Shell{public void Shellsort2 (int[] a) {for (int d=a.length/2;d>0;d=d/2) {System.out.println ("d=" +d), for (int i=  d;i<a.length;i++) {for (int j=i-d;j>=0;j=j-d) {if (a[i]<a[j]) {int temp=a[i]; a[i]=a[j]; a[j]=temp;  }  } } for  (int z:a)       System.out.println (z);}} public void Shellsort (int[] a) {for (int d=a.length/2;d>0;d=d/2) {System.out.println ("d=" +d), for (int i=d;i< a.length;i++) {int temp=a[i]; int J; for (j=i-d;j>=0&&temp<a[j];j=j-d) {a[j+d]=a[j];  } a[j+d]=temp;} for (int z:a)       System.out.println (z);}}}

******************************************************************************************************

Note: In the upper Code, method Shellsort2 (int [] a) is the wrong way to sort the hill!!!

*************************************************************************************************************** ************


Method Shellsort2 (int [] a) is the wrong insertion sort method that I have always understood. This method does not implement an element-by-comparison. For example, when i=4, it only compared the size of a[4] and a[3], do not compare the previous size, so it is a wrong way to insert sorting.


From the above, the hill sort has a total of triple loops:

(1) The outermost loop for statement with incremental D change control for a number of scans, the initial value of D is half the length of the sequence, after each trip halved, until 1;

(2) The intermediate for loop carries on a scan, the sequence divides into the D group, each group consists of n/d elements separated by the D distance, each group of elements carries on the direct insertion sorting;

(3) The Inner loop for statement makes a set of direct insertion sorts, inserting a a[i] into the sort subsequence in front of the group in which it is located.


The difference between the hill sort and the direct insertion sort is actually that the hill sort has a layer of loops to control the number of scans lying, and in each direct insertion sort, the interval is d instead of the interval of 1, so the i=1,j=i-1 is changed to i=d,j=i-d and so on.


If you do not understand, then give examples of Oh, I will each trip to the data recorded on the paper, it will suddenly dawned oh ^_^




"Deep understanding"-inserting sorting algorithms

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.