Insertion Sort Insertion Sort

Source: Internet
Author: User

Insert Sort (insertion sort) is a simple and intuitive sorting algorithm. 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 an implementation, usually in the order of In-place (that is, the ordering of extra space using only O (1), so that in the backward-forward scanning process, the ordered elements need to be moved back and forth gradually, providing the insertion space for the newest elements.

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

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

If the cost of the comparison operation is larger than the exchange operation, a binary lookup method can be used to reduce the number of comparison operations. The algorithm can be thought of as a variant of the insertion sort , called a binary lookup insert sort.

iterate from the second element of the data0 1 2 3 4 5 20 +30 10 60) 5020 40 -10 60 5020) 30 40Ten60 5010 20) 30 40 -5010 20 30) 40 50 -I= 1;     temp=a[1]=40; j=i-1=0; A[0]>40, if the preceding element is larger than the current element. I= 2; TEMP=A[2]=30, j=i-1=1;a[1]>30 set up, a[2]=a[1], let a[1] move backwards. J-Continue forward, compare, j=0,a[0]>30, no, exit the loop. A[1]=30. I= 3; temp=a[3]==10, j=i-1=2; A[2]>10? Set up, a[3]=a[2], let a[2] move back one to A[3] position. J- -  ; j = 1; A[1]>10? Set up, a[2]=a[1], let a[1] move back one to A[2] position. J- - ; j=0; A[0]>10? Set up, a[1]=a[0], let A[0] move back one to A[1] position. J--; j=-1; Jump out of the loop, a[-1+1] =a[0] = 10; Insert the current value. I=4,TEMP=A[4]=60,J=I-1=3,A[3]>A[4]?not set up, jump out of the loop a[j+1] =a[i] =60I=5,TEMP=A[5]=50, j=i-1=4, Judge A[4]>50, established, a[5]=a[4] J- - ; j = 3; A[3]>50 established? Not set up, jump out of the loop. j[4]= 50; Insert the current value.

The process of sorting a column of numbers using an insert sort

Java code example:

 PackageCom.sort; Public classInsertsort {/** Direct Insert Sort * * Parameter description: A--the array to be sorted N--the length of the array*/     Public Static voidInsertsort (int[] A,intN) {intlength = A.length;//Array Length        intI//the position of the current value        intJ//point to the position before J        intTemp//the value currently to be inserted for sorting//iterates through the values from the second position of the array         for(i = 1; i < length; i++) {Temp=A[i]; J= I-1; //A[j] is larger than the current value, A[j] moves back one bit, vacated the position of J, so that the next loop value is moved back             while(J >= 0 && A[j] >temp) {A[j+ 1] = A[j];//move the a[j] value backj--;//J Move forward            }            //jump out of the loop (find the middle position to insert or have traversed to 0 subscript)a[j+ 1] = temp;//inserts the current value into the        }    }     Public Static voidMain (string[] args) {inti; int[] A = {20, 40, 30, 10, 60, 50 }; System.out.printf ("Before sort:");  for(i = 0; i < a.length; i++) System.out.printf ("%d", A[i]); System.out.printf ("\ n");        Insertsort (A, a.length); System.out.printf ("After sort:");  for(i = 0; i < a.length; i++) System.out.printf ("%d", A[i]); System.out.printf ("\ n"); }}

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