Insertion algorithm Reprint

Source: Internet
Author: User

The Insert sort algorithm is an efficient algorithm for ordering a small number of elements. The insertion sort works similar to the way the cards are organized at cards, and when we start to draw, our left hand is empty, then one card is touched from the table and inserted into the right position of the left hand. To find the right position for this card, compare it to the hand that is already in the hands from right to left, regardless of when the hand is sorted.

Java implements the algorithm as follows:

1  Public voidInsertsort (inta[]) {  2         intLength=a.length;//Array Length3         intJ//the position of the current value4         intI//point to the position before J5         intKey//the value currently to be inserted for sorting6         //iterates through the values from the second position of the array7          for(j=1;j<length;j++){  8key=A[j]; 9I=j-1; Ten             //A[i] is larger than the current value, A[i] moves back one bit, vacated the position of I, so that the next cycle of the value of the move One              while(I>=0 && a[i]>key) {   AA[i+1]=a[i];//move the a[i] value back -i--;//I move forward -}//jump out of the loop (find the middle position to insert or have traversed to 0 subscript) theA[i+1]=key;//inserts the current value into the -         }   -}

Insertsort the process on the array a={5,2,4,6,1,3}, the subscript of the array is now above the mega, and the value of the black box is key=a[j].

A[i] is the value to the left of A[j], each time the loop key is compared to a[i], if key<a[i], then a[i] moves to the i+1, and i--moves to the left until A[i]<=key or i<0 is found, at which point the key is inserted into the a[i+1].

Question 1:a[i] is the a[i+1] value overwritten the first time you move to the right, does it cause data loss?

A: No data loss, should be the first right before the i+1=j, at this time a[j] value exists in the key. Each right shift leaves a position for the next right shift or insert, which is where the key to the insertion sort is.

Insertion algorithm Reprint

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.