Java implementation Insert Sort algorithm _java

Source: Internet
Author: User

1, algorithm concept.

Each time the first element is removed from the unordered table, it is inserted into the proper position of the ordered table, so that the ordered table is still orderly.

2, algorithmic thinking.

Suppose the records to be sorted are stored in array R[1..N]. Initially, the r[1] is a 1 ordered region and the disordered region is R[2..N]. From i=2 up to I=n, the r[i] is inserted into the current ordered region r[1..i-1, in order to generate an ordered region containing N records.

public static void Insertsort (int[] array) {
    int len = array.length;
    for (int i = 1; i < Len; i++) {for
      (int j = i; j > 0; j--) {
        if (Array[j] < array[j-1]) {
          Sort.swa P (Array, J, j-1);//Swap J and J-1
        } else break
          ;}
  }

Insert Sort diagram:

The above is to share all the content of this article, I hope to be able to understand the insertion of a sorting algorithm to help.

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.