Java Insert Sort

Source: Internet
Author: User

Concept:

  Usually, the way people organize bridge is one by one, inserting each card into the proper place in the other already ordered cards. In the implementation of the computer, in order to make room for the inserted element, we need to move all the remaining elements to the right one bit before inserting. As with select Sort, all elements on the left side of the current index are ordered, but their final position is uncertain, and they may be moved in order to make room for smaller elements. But when the index reaches the right end of the array, the array sort is done. Unlike selection sorting, the time it takes to insert a sort depends on the initial order of the elements in the input.

 Packagecom.test;Importjava.util.Arrays; Public classInsertsort { Public Static voidMain (string[] args) {intTest[] = {49,38,65,97,76,13,27,49};        Sort (test);    System.out.println (arrays.tostring (test)); }         Public Static voidSortint[] a) {intJ, sort;  for(inti = 1; i < a.length; i + +) {//starts a loop from the second element of the array to insert the elements in the arraySort= A[i];//sets the second element in the array to the element to be inserted for the first iterationj = I-1;  while(J >= 0 && Sort <A[j]) {A[j+ 1] = A[j];//if the element to be inserted is less than the J element, the J element is moved backwardsJ--; } a[j+ 1] = sort;//The sort is inserted into the array until the element to be inserted is not less than the first J element        }    }}

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.