# Algorithm 37 Big Sort: Direct insert sort and hill sort

Source: Internet
Author: User

The previous article summarizes the direct selection sorting and heap sorting, which summarizes the direct insertion sort and the hill sort in the insert sort, which we summarize mainly from the following points.

1. Direct insertion sequencing and algorithm implementation

2. Hill Sorting and algorithm implementation

3. Direct Insert sort PK Hill sort

1. Direct insertion sequencing and algorithm implementation

What is the direct insertion sort? The basic idea of a direct insert sort is that each time a first element is taken out of an unordered sequence into an ordered sequence that is already sorted, a new ordered sequence of numbers plus 1 is obtained.

1-1.

The following is a graphical description of the direct insertion sort.

1-2. Code

The following is an algorithm implementation code that inserts a sort directly.

Insertsort.java

` Public classInsertsort { Public Static voidMain (string[] args) {int[] list = {90, 10, 20, 50, 70, 40, 80, 60, 30, 52}; System.out.println ("************ Direct Insert Sort ************"); System.out.println ("Before sorting:");        Display (list); System.out.println (""); System.out.println ("After sorting:");        Insertsort (list);    Display (list); }    /*** Direct insertion sorting algorithm*/     Public Static voidInsertsort (int[] list) {        //Remove the first element from an unordered sequence (note that the unordered sequence starts with the second element)         for(inti = 1; i < list.length; i++) {            inttemp =List[i]; intJ; //traversing an ordered sequence//if the elements in an ordered sequence are larger than the temporary elements, then the elements larger than the temporary elements in the ordered sequence are moved back             for(j = i-1; J >= 0 && list[j] > temp; j--) {list[j+ 1] =List[j]; }            //insert temporary elements into vacated positionsList[j + 1] =temp; }    }        /*** Traverse Print*/     Public Static voidDisplayint[] list) {System.out.println ("******** Show begins ********"); if(List! =NULL&& list.length > 0) {             for(intnum:list) {System.out.print (num+ " "); } System.out.println (""); } System.out.println ("******** Show ends ********"); }}`

Test results:

2. Hill Sorting and algorithm implementation

Hill sort is a more efficient and improved version of direct insertion sorting, also known as "narrowing incremental sorting".

2-1.

2-2. Code

Shellsort.java

` Public classShellsort { Public Static voidMain (string[] args) {int[] List = {8, 9, 1, 7, 2, 3, 5, 4, 6, 0}; System.out.println ("************ Hill sort ************"); System.out.println ("Before sorting:");        Display (list); System.out.println (""); System.out.println ("After sorting:");        Shellsort (list);    Display (list); }    /*** Hill Sort algorithm*/     Public Static voidShellsort (int[] list) {        //Take increment        intGap = LIST.LENGTH/2;  while(Gap >= 1) {            //unordered sequence             for(inti = gap; i < list.length; i++) {                inttemp =List[i]; intJ; //ordered sequence                 for(j = i-gap; J >= 0 && list[j] > temp; j = J-Gap) {List[j+ Gap] =List[j]; } list[j+ Gap] =temp; }            //Decrease IncrementGap = GAP/2; }    }    /*** Traverse Print*/     Public Static voidDisplayint[] list) {System.out.println ("******** Show begins ********"); if(List! =NULL&& list.length > 0) {             for(intnum:list) {System.out.print (num+ " "); } System.out.println (""); } System.out.println ("******** Show ends ********"); }}`

Test results:

3. Direct Insert sort PK Hill sort

Since the hill sort is an improved version of the direct insert sort, let's test if the hill sort is really faster than the direct insert sort?

Code:

Insertsortpkshellsort.java

` Public classInsertsortpkshellsort { Public Static voidMain (string[] args) {testinsertsort ();    Testshellsort (); }    /*** Test the time spent on direct insertion sequencing*/     Public Static voidTestinsertsort () {int[] List =New int[10000];  for(inti = 0; I < 10000; i++) {List[i]= (int) (Math.random () * 100000); }        //Direct Insert Sort        LongStart =System.currenttimemillis ();        Insertsort.insertsort (list); LongEnd =System.currenttimemillis (); System.out.println ("Time spent directly inserting a sort:" + (End-start));    Display (list); }    /*** Test the time spent in Hill sequencing*/     Public Static voidTestshellsort () {int[] List =New int[10000];  for(inti = 0; I < 10000; i++) {List[i]= (int) (Math.random () * 100000); }        //Hill Sort        LongStart =System.currenttimemillis ();        Shellsort.shellsort (list); LongEnd =System.currenttimemillis (); System.out.println ("The time spent in Hill sorting:" + (End-start));    Display (list); }    /*** Traverse the first 10 digits of printing*/     Public Static voidDisplayint[] list) {System.out.println ("******** The first 10 numbers after sorting start********"); if(List! =NULL&& list.length > 0) {             for(inti = 0; I < 10; i++) {System.out.print (List[i]+ " "); } System.out.println (""); } System.out.println ("******** The first 10 numbers after sorting end**********"); System.out.println (""); }}`

Test results:

As you can see from the test results, the hill sort is faster than the direct insert sort.

Welcome reprint, but please keep the original source of the article

Algorithm 37 Big Sort: Direct insert sort and hill sort

Related Keywords:

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.