Insert sort (Direct insert sort, binary insert sort)

Source: Internet
Author: User

First, direct insertion sort

 PackageAlgorithm.sort.compare.insert;Importjava.util.Arrays; Public classDirectinsertsort { Public Static voidMain (string[] args) {int[] Arraya =New int[] {11, 213, 134, 65, 77, 78, 23, 43};        Directinsertsort (Arraya);    System.out.println (arrays.tostring (Arraya)); }                                     Public Static voidDirectinsertsort (int[] Array) {         for(inti = 1; i < Array.Length; i++ ){            intJ; inttemp =Array[i];  for(j = i; j > 0; j-- ){                if(Array[j-1] >temp) array[j]= Array[j-1]; Else                                   Break; } Array[j]= temp; }    }          } 

Second, binary insert sort

Binary Insert sort (binary insertion sort) is an improvement on the insertion sorting algorithm.

 PackageAlgorithm.sort.compare.insert;Importjava.util.Arrays;  Public classBinaryinsertsort { Public Static voidMain (string[] args) {int[] Arraya =New int[] {11, 213, 134, 65, 77, 78, 23, 43 };        Binaryinsertsort (Arraya);    System.out.println (arrays.tostring (Arraya)); }                                    Private Static voidBinaryinsertsort (int[] Array) {         for(inti = 1; i < Array.Length; i++ ){            //if (Array[i] > array[i-1])//from big to small            if(Array[i] < array[i-1]) {//from small to large                intTMP =Array[i]; intLow = 0; intHigh = I-1;  while(Low <=High ) {                    intMid = (low + high) >>> 1; //if (Array[mid] > tmp)//from big to small                    if(Array[mid] < TMP)//from small to largeLow = mid + 1; Else High= Mid-1; }                 for(intj = i; J > Low; j--) Array[j]= Array[j-1]; Array[low]=tmp; }        }    }          } 

Iii. sort of Hill

The Hill sort (Shell sort) is a sort of insertion. Also known as narrowing incremental sorting, is a more efficient and improved version of the direct insertion sorting algorithm.

 PackageAlgorithm.sort.compare.insert;Importjava.util.Arrays; Public classShellsort { Public Static voidMain (string[] args) {int[] Arraya =New int[] {213, 134, 65, 77, 78, 23, 43 }; Shellsort (Arraya,0, arraya.length);    System.out.println (arrays.tostring (Arraya)); }                             Private Static voidShellsort (int[] Array,intStartintLen) {        intPower = 1;  while(Power + 1) * 2 <len) Power= (Power + 1) * 2-1;  for(intK = power; K >= 1; K = (k + 1)/2-1 ){             for(inti = 0; I < K; i++ ){                if(Len-i <= 1)                                    Break; inttmp;  for(intj = start + i + K; J < start + Len; J + =k) {tmp=Array[j]; intm =J;  for(; m > start + i; m-=k) {if(Array[m-k] > tmp)//from small to largeARRAY[M] = array[m-K]; Else                                                   Break; } Array[m]= tmp; }            }        }    }                 } 

Insert sort (Direct insert sort, binary insert 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.