Insert Sort method (Java implementation)

Source: Internet
Author: User
public class Insertsort {public static void main (string[] args) {//array declaration int[] A = {5,1,3,0,1,4,-1};int L = a.length;//First make sure the top two An element is ascending if (a[1]<a[0]) {int tmp=a[0];a[0]=a[1];a[1]=tmp;} Next insert sort for (int i=2;i<l;i++) {//If the number to be inserted is less than or equal to the sorted header if (a[i]<=a[0]) {int tmp=a[i];for (int j=i;j>0;j--) {A[j] =A[J-1];} A[0]=tmp;} If the number to be inserted is greater than or equal to the ordered footer else if (a[i]>=a[i-1]) continue;//If the number to be inserted is between a two number in the sorted table else{for (int k=0;k<i-1;k++) {if (a[ I]>=a[k]&&a[i]<=a[k+1]) {int tmp1=a[i];for (int m=i;m>k+1;m--) {a[m]=a[m-1];} A[K+1]=TMP1;}}}} for (int element:a) System.out.println (element+ "");}}


Insert Sort method (Java implementation)

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.