Sorting algorithm of common algorithms two "insert Sort"

Source: Internet
Author: User

What is the insertion sort! Simply introduce the idea (assuming ascending): Insert a new element in an already sequenced array, search from behind, and when you find a number smaller than it, stop and insert it between this and the previous number. Let's show you a picture:


The specific algorithm is described as follows:

1. Start with the first element, because there is only one element, so it is ordered.

2. Take an element behind it and compare it.

3. The element behind it is larger than it, move it back, and assign the element behind it to its original position.

4. Repeat the action of 3 until you find an element that is smaller or equal to it, and insert the element behind the element that is smaller than it

5. Repeat the 2~5 operation

Let's take a look at the code first:

<span style= "FONT-SIZE:14PX;" > public   static void sort (int[] array) {for        (int i = 1; i < Array.Length; i++) {            int j = i-1;            int temp = Array[i];            while ((J >= 0) && (array[j] > Temp)) {                array[j + 1] = Array[j];                j--;            }            ARRAY[J+1] = temp;        }    } </span>

Relatively concise, perhaps you will say can be compared from the go after, I would say yes, but, in order to know whether to jump out of the loop is to go to the last or to find the first number than it, or the first number is larger than it, we need to introduce a flag bit, as follows:

    private static void Move (int[] array, int from, int to) {for        (int i = to; i > from; i--) {            Array[i] = array[i-1 ];        }    }    public static void Sort1 (int[] array) {for        (int i = 1; i < Array.Length; i++) {            int j = 0;            Boolean flag = false;            while (!flag && (J < i)) {                if (Array[i] < array[j]) {                    flag = true;                }                j + +;            }            if (flag) {                int temp = array[i];                Move (array,--j, i);                ARRAY[J] = temp;}}    }
It looks a lot more complicated than the above, so it's recommended to compare back and forth.

Can be easily obtained, the time complexity is O (n^2), the space complexity is O (n) + O (1), the optimal time complexity is O (n).

Sorting algorithm of common algorithms two "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.