Common sort algorithm (a) Insert sort

Source: Internet
Author: User

Insert sort is divided into: Direct insert sort, binary insert sort (also called binary Insert sort), list insert sort, Hill sort (also known as narrowing incremental sort). A stable sort (in layman's words, two equal numbers do not swap positions).


Here I specifically talk about the direct insertion sort and the hill sort.

Direct Sort Insert

A direct insert sort consists of a two-layer nested loop. The outer loop identifies and determines the value to be compared. The inner loop determines its final position for the value to be compared. A direct insert sort compares the value to be compared to its previous value, so the outer loop starts with the second value. The current value continues to loop compared with a value larger than what is to be compared until it is found to be smaller than the value to compare and the value to be compared is placed in a subsequent position, ending the cycle.


Basic ideas

Pending sort records R1,R2, ..., rn–1, Rn

First step: R1

Step Two: (R1), R2

Step Three: (R1, R2), R3

......

Step J: (R1,r2, ..., rj–1), Rj

......

Nth Step: (R1,r2, ..., rn–1), Rn.


As shown in the slices, direct ordering always precedes the previous paragraph, then sequentially, knowing that the entire array is ordered.

void Insersort (int * a,size_t size)//Direct Insert sort {assert (a); for (int i = 1;i < size; i++) {int tem = A[i];int end = I-1;w Hile (end >= 0 && A[end]>tem) {a[end+1] = a[end];--end;} A[END+1] = tem;}}




Hill sort

The Hill sort (Shell sort) is a sort of insertion. Also known as narrowing incremental sorting, is a more efficient and improved version of the direct insertion sorting algorithm. Hill Sort is a non-stable sorting algorithm. The method is due to DL. The shell was named after it was introduced in 1959.

Hill sort is the insertion of elements according to different steps, when the first element is very disordered, the step is the largest, so the number of elements inserted in the order is very small, fast; When the elements are basically ordered, the step size is very low, and the insertion sort is very efficient for ordered sequences. So, the time complexity of hill sorting would be better than O (n^2).

As

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/80/6D/wKiom1dBOjuTu0BWAAAxcKyEX04636.png "title=" 5.png " alt= "Wkiom1dbojutu0bwaaaxckyex04636.png"/>

Here, my stride is in 3, as can be seen from the top, the step includes the entire array.

The first one is: 2,9,8,1--"1,2,8,9

The second one is: 5,3,7--"3,5,7

The third step is: 4,6,1-"1,4,6

The code is as follows


void shellsort (int* a, size_t size)    //Hill Sort {assert (a); int gap =  size / 3;while (gap > 1)   {  gap = gap/3 +  1;  //gap has been getting smaller, knowing it is 1;for (int i = 0;i <  (SIZE&NBSP;-&NBSP;GAP); ++i) { Int end = i;int tem = a[end + gap];while ( end >= 0  && a[end]>tem) {a[end+gap] = a[end];end -= gap;} A[end+gap] = tem;}   }} 

The feature of Hill sorting is that it does not require a lot of auxiliary space and is as easy to implement as a merge sort. Hill sort is an algorithm based on the insertion sort, which adds a new feature to the algorithm and improves the efficiency. Hill sort does not have fast sorting algorithm fast O (n (logn)), so the medium size is good and the size of a very large data sort is not the best choice. But the algorithm is much faster than the O (n^2) complexity. And the hill sort is very easy to implement, the algorithm code is short and simple. In addition, the hill algorithm does not have much difference in the worst case and average execution efficiency, while fast sequencing performs poorly in the worst case scenario.





Common sort algorithm (a) 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.