Java Insert Sort

Source: Internet
Author: User

Insert Sort
It works by constructing an ordered sequence, for unsorted data, to scan from backward forward in the sorted sequence, to find the appropriate position and insert it.
The insertion sort is implemented on the implementation, usually in the in-place sort (in-place order, that is, the order of the extra Space of O (1) is only used),
Thus, in the process of backward-forward scanning, the ordered elements need to be moved backwards and forwards to provide the insertion space for the newest elements.

Algorithm description
In general, the insertion sort is implemented using In-place on the array. The specific algorithm is described as follows:

Starting with the first element, the element can be thought to have been sorted
Takes the next element and scans the sequence of elements that have been sorted from back to forward
If the element (sorted) is greater than the new element, move the element to the next position
Repeat step 3 until you find the sorted element is less than or equal to the position of the new element
After inserting a new element into the position
Repeat step 2~5

If there is such a sequence int[] arrs=new int[]{1,3,4,5,2};
Remove 2 and temporarily store it, comparing it to the previous one
Arrs[3]=5 and 2 comparisons, 5 is greater than 2, 5 is moved to Arrs[4], at this time arrs[3] and arrs[4] are 5
Move forward from back to arrs[2]
Arrs[2]=4 and 2 comparisons, 4 is greater than 2, 4 is moved to Arrs[3], at this time arrs[2] and arrs[3] are 4
Move forward from back to Arrs[1]
Arrs[1]=3 and 2 comparisons, 3 is greater than 2, 3 is moved to Arrs[2], at this time arrs[1] and arrs[2] are 3
Move forward from back to Arrs[0]
Arrs[0]=1 and 2 comparison, 1 is less than 2, so 2 is placed in the back of Arrs[0], arrs[1], arrs[1]=2 (formerly Arrs[1]=3), complete the sorting

Schematic sample diagram:

Help code:

Code Public classInsertsort { Public Static voidMain (string[] args) {int[] Arrs =New int[] {9,8,7,6,5,4,3,2,1,0 }; System. out. println ("before array sorting");        PrintArray (ARRS);        Insertsort (ARRS); System. out. println ("after array sorting");    PrintArray (ARRS); }    //Insert Sort Algorithm    Private Static voidInsertsort (int[] arrs) {        //TODO auto-generated Method Stub         for(inti =0; i < arrs.length; i++) {            //first, the value of the element is stored temporarily .            inttemp =Arrs[i]; intj =i;  while(J >0&& Arrs[j-1] >temp) {                //move the value back, that is, the previous value, assigned to the position behind it, as if moving backwards,ARRS[J] = arrs[j-1]; J--; }            //inserts the temporarily extracted value into the position of the last moving elementARRS[J] =temp; }    }    //Print Array Method     Public Static voidPrintArray (int[] arrs) {System. out. Print ("[");  for(inti =0; i < arrs.length; i++) {System. out. Print (Arrs[i]); if(I! = arrs.length-1) {System. out. Print (","); }} System. out. println ("]"); }}

Java 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.