Insert sort (Java)

Source: Internet
Author: User

Insertion Sort)Is a simple and intuitive sorting algorithm. Its working principle is to build an ordered sequence. For unordered data, scan the sorted sequence from the back to the front, locate the corresponding position, and insert it.Insert sortIn terms of implementation, in-place sorting is usually used (that is, sorting only requires the extra space of O (1). Therefore, in the process of scanning from the back to the front, the sorted elements need to be moved backward repeatedly to provide the insert space for the new elements.

Generally,Insert sortAll are implemented on the array using in-place. The specific algorithm is described as follows:

  1. Starting from the first element, this element can be considered to have been sorted
  2. Extracts the next element and scans it forward from the back in the sorted element sequence.
  3. If the element (sorted) is greater than the new element, move the element to the next position.
  4. Repeat Step 3 until you find the position where the sorted elements are smaller than or equal to the new elements.
  5. Insert new elements to this position
  6. Repeat Step 2

Insert the sorting algorithm directly (Java ):

Public static void insertionsort (INT [] list ){
For (INT I = 1; I <list. length; I ++ ){
// Insert list [I] into a sorted sublist list [0... I-1] So that list [0... I] is sorted
Int currentelement = list [I];
Int J;
For (j = I-1; j> = 0 & list [J]> currentelement; j --)
List [J + 1] = list [J];

// Insert the current element into list [J + 1]
List [J + 1] = currentelement;
}
}

Algorithm complexity if the goal is to sort the sequences of n elements in ascending order, there are best cases and worst cases of adopting insert sorting. The best case is that the sequence is already in ascending order. In this case, the comparison operation needs to be performed. (N-1). The worst case is that the sequence is sorted in descending order, so there are a total N (n-1)/2Times. The value assignment operation for insert sorting is the number of comparison operations minus (N-1)Times. On average, the complexity of the insert sorting algorithm is O ( N2 ). Therefore, insert sorting is not suitable for sorting applications with large data volumes. However, if the amount of data to be sorted is small, for example, if the order is smaller than, insertion sorting is still a good choice.

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.