Semi-insert sorting

Source: Internet
Author: User

This section introduces a Insertion Method missing during insertion sorting, that is, semi-insertion. This method uses the binary search method to find the insert point, which can reduce the number of element comparisons, but cannot reduce the number of moves. The complexity is the same as that of direct insertion, and all values are O (N ^ 2 ).

Directly run the Code:

// Binary insert sorting void binary_insert_sort (INT arr [], int Len) {If (ARR = NULL | Len <= 1) {return;} int I, J; for (I = 1; I <Len; I ++) {int low = 0, high = I-1; int target = arr [I]; while (low <= high) {int middle = low + (high-low)> 1); If (ARR [Middle]> Target) {high = middle-1 ;} the position of else {LOW = middle + 1 ;}// low is exactly the insert point. // The elements between I and low are moved one by one (excluding I) for (j = I; j> low; j --) {arr [J] = arr [J-1];} // Insert the target to the correct position. Arr [low] = target ;}}


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.