C ++ sorting series (1) Insert sorting-semi-insert sorting

Source: Internet
Author: User

Solemnly declare: This article is written by the author based on my personal understanding. errors are inevitable. Please be prepared!

You can reprint, modify, and indicate the source when reprinting!

Binary insertion sort is an improvement in the insertion sorting algorithm. Because the Sorting Algorithm inserts elements into the sequence that has been sorted in sequence. Because the first half is divided into sorted columns, we do not need to look for insertion points in sequence. We can use the half-lookup method to speed up the search for insertion points.

When inserting a new element into an sorted array, when looking for the insertion point, set the first element of the area to a [low]. when the last element is set to a [High], the elements to be inserted during the round comparison will be compared with a [m], where M = (low + high)/2, if it is smaller than the reference element, select a [low] To M-1] as the new inserted area (that is, high = s-1 ), otherwise, select a [M + 1] to a [High] as the new insert area (that is, low = m + 1) Until low <= high is not true, after this position, all elements are moved one by one, and the new element is inserted into a [high + 1].

The semi-insertion sorting algorithm is a stable sorting algorithm, which significantly reduces the number of comparisons between keywords than the direct Insertion Algorithm. Therefore, the speed is faster than the direct insertion sorting algorithm, however, the number of records moving remains unchanged. Therefore, the time complexity of the semi-insertion sorting algorithm is O (n ^ 2), which is the same as that of the direct insertion sorting algorithm. Additional space O (1 ).

Implementation Code:

1 template <typename T> 2 void binary_insert_sort (t v [], const int sz) 3 {4 for (INT I = 1; I <SZ; ++ I) 5 {6 t tmp = V [I]; 7 int low = 0; 8 int high = I-1; 9 10 while (low <= high) 11 {12 int m = (low + high)/2; // half 13 if (TMP <V [m]) 14 {15 high = m-1; // insert point in low half Zone 16} 17 else18 {19 low = m + 1; // insert point in high half zone 20} 21} 22 23 for (Int J = I-1; j> = high + 1; -- j) 24 {25 V [J + 1] = V [J]; 26} 27 V [high + 1] = TMP; // insert 28} 29}

 

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.