Sort algorithm One: Insert sort (insertion sort)

Source: Internet
Author: User

Recently from NetEase Open class in the MIT Open Class "Introduction to the algorithm," The feeling is good, the next few articles shown in the study diary, not ready to do too much to describe the algorithm details, interested can see for themselves.

The article is divided into several classical sorting algorithms, directly on the code, according to the results of the algorithm performance has a visual understanding. This article first says insert sort (insertion sort).

(i) Algorithm implementation

1 protected voidSortint[] tosort) {2         if(Tosort.length <= 1) {3             return;4         }5          for(inti = 1; i < tosort.length; i++) {6             if(Tosort[i] < tosort[i-1]) {7                 intj =i;8                 inttemp =Tosort[i];9                  while(J > 0 && Temp < tosort[j-1]) {TenTOSORT[J] = tosort[j-1]; Onej--; A                 } -TOSORT[J] =temp; -             } the         } -}
Insertion Sort

1) Insert sort is in place sort, save space

2) The time complexity of insertion sequencing is O (n2)

(ii) Algorithmic performance

**************************************************
Number to Sort is:2500
Array to sort is:{665184,192100,475135,171530,869545,506246,640618,543738,91353,493005 ...}
Cost time of "Insertionsort" is (milliseconds): 3
Sort result of "Insertionsort": {856,985,2432,3792,3910,3915,4423,4516,4653,4780 ...}
**************************************************
Number to Sort is:25000
Array to sort is:{99880,631403,265087,597224,876665,955084,996547,879081,197806,926881 ...}
Cost time of "Insertionsort" is (milliseconds): 267
Sort result of "Insertionsort": {14,14,17,83,97,152,179,199,240,299 ...}
**************************************************
Number to Sort is:250000
Array to sort is:{777293,731773,508229,920721,338608,707195,940,445210,19071,768830 ...}
Cost time of "Insertionsort" is (milliseconds): 21,523
Sort result of "Insertionsort": {2,7,7,19,19,21,24,29,30,39 ...}

Related code:

1  PackageCom.cnblogs.riyueshiwang.sort;2 3 Importjava.util.Arrays;4 5  Public classInsertionsortextendsAbstractsort {6 @Override7     protected voidSortint[] tosort) {8         if(Tosort.length <= 1) {9             return;Ten         } One          for(inti = 1; i < tosort.length; i++) { A             if(Tosort[i] < tosort[i-1]) { -                 intj =i; -                 inttemp =Tosort[i]; the                  while(J > 0 && Temp < tosort[j-1]) { -TOSORT[J] = tosort[j-1]; -j--; -                 } +TOSORT[J] =temp; -             } +         } A     } at  -      Public Static voidMain (string[] args) { -          for(intj = 0, n = 2500; J < 3; j + +, n = n * 10) { - System.out -. println ("**************************************************"); -System.out.println ("Number to Sort is:" +n); in             int[] array = Commonutils.getrandomintarray (n, 1000000); -System.out.print ("Array to sort is:"); to Commonutils.printintarray (array); +  -             int[] Array1 =arrays.copyof (array, n); the             NewInsertionsort (). Sortandprint (array1); *         } $     }Panax Notoginseng}
Insertionsort.java
1  PackageCom.cnblogs.riyueshiwang.sort;2 3 ImportJava.text.MessageFormat;4 5  Public Abstract classAbstractsort {6     /**7      * 8      * @paramTosort9 * Array to sortTen      */ One     protected Abstract voidSortint[] tosort); A  -      Public voidSortandprint (int[] tosort) { -Long begin =System.currenttimemillis (); the sort (tosort); -Long end =System.currenttimemillis (); - System.out.println (Messageformat.format ( -"Cost time of" {0} "is (milliseconds): {1}", This. GetClass () +. Getsimplename (), (End-( begin) )); -System.out.print (Messageformat.format ("Sort result of" {0} ":", This + . GetClass (). Getsimplename ())); A Commonutils.printintarray (tosort); at     } -  -}
Abstractsort.java
1  PackageCom.cnblogs.riyueshiwang.sort;2 3 ImportJava.util.Random;4 5  Public classCommonutils {6     Private StaticRandom random =NewRandom ();7 8      Public Static voidPrintintarray (int[] Array) {9System.out.print (' {'));Ten  One         intLength = Math.min (Array.Length, 10); A          for(inti = 0; i < length; i++) { - System.out.print (Array[i]); -             if(I! = length-1) { theSystem.out.print (', '); -}Else { -                 if(Array.Length > 10) { -System.out.print ("..."); +                 } -System.out.println ('} '); +             } A         } at     } -  -      Public Static int[] Getrandomintarray (intSizeintmaxValue) { -         int[] Array =New int[size]; -          for(inti = 0; i < size; i++) { -Array[i] =Random.nextint (maxValue); in         } -         returnArray; to     } +  -      Public Static voidSwapint[] Tosort,intIintj) { the         inttemp =Tosort[i]; *Tosort[i] =Tosort[j]; $TOSORT[J] =temp;Panax Notoginseng     } -}
Commonutils.java

Sort algorithm One: Insert sort (insertion 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.