Insert and sort ---- JAVA Implementation

Source: Internet
Author: User

Insertsort)

Sort by inserting the values in the sequence into a sorted sequence until the end of the sequence. Insert sorting is an improvement on Bubble sorting. It is twice faster than Bubble sorting. Generally, you do not need to use insert sorting when the data is greater than 1000, or repeat the sequence of data items that exceed 200.
1. Thoughts
Each time you select an element K and insert it to the previously sorted Part A [1... In I], K in the insert process is followed by forward and a [1... I. If a [x]> = K is found, insert K to the end of a [X]. Move the element before insertion.

2,Algorithm time complexity

In the best case, the order is in ascending order (from small to large), which only needs to be compared n times without moving. Therefore, the time complexity is O (n)
Worst case: reverse order, so that each element needs to be compared n times, a total of n elements, so the actual complexity is O (n2)
Average: O (n2)

3,Stability

Insert sorting is stable

4. Implementation

Public static void insertsort (INT [] data, int low, int high ){

For (INT I = low; I

Int temp = data [I];

Int J = I-1;

For (; j> = 0 & temp <data [J]; j --){

Data [J + 1] = data [J];

}

Data [J + 1] = temp;

}

}

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.