Sorting Algorithm series: insert Sorting Algorithm

Source: Internet
Author: User

Sorting Algorithm series: insert Sorting Algorithm
Overview

The basic operation of directly inserting a Sort (Straight Insertion Sort) is to insert a record into an ordered table with a sorted order, so as to get a new ordered table with a 1 Increase in the number of records.

-Big talk Data Structure

 

Algorithm principle analysis

From the above overview, we can understand that the process of sorting arrays here requires two sequences to complete.
A disordered sequence to be sorted and a sorted sequence. What we need to do now is to insert unordered elements one by one from the disordered sequence to the ordered sequence. As shown below:

However, there are some bad points here. Obviously, we need to add an additional secondary array. If the data to be sorted is large, the policy for adding secondary arrays may be unavailable.
Here we can easily imagine that there is such an equation in the original array:Overall sequence = ordered sequence + disordered Sequence
That is to say, we can split the current sequence array into two parts, with the order on the left and the disordered order on the right.

In this way, the first element is extracted from the disordered sequence and inserted from the sequence. Until the sequence is ordered. For detailed steps, see the followingAlgorithm steps. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxociAvPg0KPGgxIGlkPQ = "algorithm step"> The 0th elements in the default sequence of the algorithm step are ordered (because there is only one element a [0], it is naturally ordered ); starting from the element whose subscript is 1 (subscript starts from 0), take the element a [I] at the position I of the current subscript and save it to a temporary variable waitInsert; traverse the First Half of the ordered sequence cyclically and compare it with waitInsert until an element smaller than waitInsert (which is sorted from small to large by default) is encountered. At this time, the subscript is j, now, you only need to assign a value to a [j + 1] to waitInsert, move the subscript I of the element to be inserted to a position backward, and repeat steps 2nd to 4th, until all the elements in the disordered sequence are inserted into the ordered sequence. After the above five steps, the overall sequence must be orderly and sorted completely.

Logical implementation
/** Core module of the Sorting Algorithm ** @ param array * array to be sorted */private void sortCore (int [] array) {int arraySize = array. length; for (int I = 1; I <arraySize; I ++) {int j = I; int waitInsert = array [I]; while (j> 0 & waitInsert <array [j-1]) {array [j] = array [j-1]; j --;} array [j] = waitInsert ;}}
Complexity Analysis
Sorting Method Time Complexity Space complexity Stability Complexity
Average Worst case Best case
Insert sort O (n2) O (n2) O (n) O (n) Stability Simple

The worst and average conditions here can be seen from the code, because there are two nested for loops. What is the best case? This is for an ordered sequence, there is no need to exchange, but it is compared n times, so the best time complexity here is O (n ).

Ref big talk Data Structure

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.