Sorting algorithm of data structure and algorithm (i): Insert sort

Source: Internet
Author: User

Insert sort can be divided into: Direct insert sort and hill sort (known element, find position)1. Direct Insert Sort

principle : A unsorted array is divided into unordered and ordered areas, and the first element of the unordered area is inserted into the ordered area by size, and finally until the elements of the unordered area are inserted into the ordered area, sorting is done.

Code implementation:
 for(inti = 1; i < a.length; i++) {//unordered area start subscript    if(A[i] < a[i-1]) {//determine if sorting should be        intCurrentValue = A[i];//Save A[i] temporarily.        intj = I-1; //the order of two conditions can not change, when J=0, j--after 1, if the first Judge A[j], the array will appear out of bounds         while(J >= 0 && A[j] >CurrentValue) {A[j+1] = A[j];//Shiftj--; } a[j+1] =CurrentValue; }}

Analysis : Stable (no jump), Space complexity O (1), Time complexity "best O (N), average, worst O (n*n)" Advantage: for (1) Array basic order (2) small amount of data

2. Hill sort (improved algorithm for direct insertion sequencing, utilizing its two advantages)

principle : The data is divided into groups in different increments (steps), and each group is directly inserted into the sort. When the initial element is very disordered, the increment is the largest, the number of groups is the most, each group is directly inserted in the order of the least element, fast, when the elements are basically ordered (small in front, large in the back), the increment is very small, the direct insertion of the order for the basic order of the sequence of high efficiency.

Code implementation:
 for(intGap = A.LENGTH/2; Gap >= 1; Gap/= 2) {//The increment gap is half the length of the array, then the current half, then the last 1//group the elements from gap to scan all groups     for(inti = gap; i < a.length; i++){        intTMP = A[i];//temporarily stores the current value for post-swap use         for(intj = I-gap; J >=0 && tmp < A[J]; J-= Gap) {//sort elements that are distance gapA[j + gap] =A[j]; } A[j]=tmp; }}

Analysis : Instability (there is a jump, the back of the exchange may be to the front), Spatial complexity O (1), time complexity "depends on the selection of increments, between O (Nlogn) ~o (n*n)"

Sorting algorithm of data structure and algorithm (i): 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.