Insert sorting: two-way insert

Source: Internet
Author: User

In the previous blog, insert sorting: Direct insertion, exchange insertion, and semi-insertion. The specific implementation of three sort inserts is mentioned. However, there are still some improvements. For example, in sequence 2 1 3, when 1 is inserted forward, because 1 <2, 1 should be inserted before 2. In the implementation of the preceding three insertion sorting methods, positions 1 and 2 are exchanged. Therefore, we wonder if it is possible not to exchange, because the exchange is always quite time-consuming. But 1 must be in front of 2, but there is no position in front of 2? Well, it seems like this. Imagine this is a circular array? This is the core idea of two-way insertion.

Ideas:

Construct a loop array B of the same size, insert the elements of the original array in sequence, and assign values to the original array in the appropriate order. How to Implement loops? There is a solution. See how the array solution of Joseph's problem is implemented. Copy the first value of the original array, a [0], and B [0] = a [0], as the first number of the loop array. Of course, you can also select another number as the first number. If a [I] = B [last], the last: last ++ is changed (note that this is not necessary to write: last = (last + 1) % n ), B [last] = a [I] If B [first] <= a [I] In, first points to the first of the captured sequence, and last points to the last of the sorted sequence. In ascending order, first points to the smallest and last points to the largest. If a data entry is a and B [first] <= Void InsertSort1 (int a [], int n) // second insert {int first, last; first = last = 0; int * B = new int [n]; B [0] = a [0]; for (int I = 1; I <n; I ++) {if (a [I] <B [first]) {first = (first-1 + n) % n; // The first change must be written in this way. B [first] = a [I];} else if (a [I]> = B [last]) {last ++; // some people write as follows: last = (last + 1) % n, which is not necessary, the last value cannot exceed n-1. B [last] = a [I];} else {int k; for (k = last + 1; a [I] <B [(K-1 + n) % n]; k = (K-1 + n) % n) // use direct insert B [k] = B [(k-1 + n) % n]; B [k] = a [I]; last ++ ;}} for (int I = 0; I <n; I ++) a [I] = B [(I + first) % n]; delete [] B ;}
Obviously, it is more efficient to insert a binary path. The Code is as follows: Code 2:
Void InsertSort2 (int a [], int n) // second insert {int first, last; first = last = 0; int * B = new int [n]; B [0] = a [0]; for (int I = 1; I <n; I ++) {if (a [I] <B [first]) {first = (first-1 + n) % n; B [first] = a [I];} else if (a [I]> = B [last]) {last ++; B [last] = a [I];} else {int low, high, mid, d; low = first, high = last; while (low! = High) // half lookup {d = (high-low + n) % n; // The number of elements mid = (low + d/2) % n; // if (a [I] <B [mid]) high = mid; elselow = (mid + 1) % n ;}for (int k = last + 1; k! = Low; k = (k-1 + n) % n) // move Element B [k] = B [(k-1 + n) % n]; B [low] = a [I]; last ++ ;}} for (int I = 0; I <n; I ++) a [I] = B [(I + first) % n]; delete [] B ;}

Update: A test code is given below. Observe the changes of array B in detail,
# Include
    
     
# Include
     
      
# Include
      
       
Void printArray (int a [], int n) // print the Array {for (int I = 0; I
       
        
= B [last]) {last ++; B [last] = a [I] ;}else {int low, high, mid, d; low = first, high = last; while (low! = High) // half lookup {d = (high-low + n) % n; // The number of elements mid = (low + d/2) % n; // if (a [I] <B [mid]) high = mid; elselow = (mid + 1) % n ;}for (int k = last + 1; k! = Low; k = (k-1 + n) % n) B [k] = B [(k-1 + n) % n]; B [low] = a [I]; last ++ ;}} printArray (B, n); for (int I = 0; I <n; I ++) a [I] = B [(I + first) % n]; delete [] B;} int main () {const int N = 6; int a [N]; srand (unsigned) time (NULL); printf ("original array a \ n"); for (int I = 0; I <N; I ++) {a [I] = rand () % 100; printf ("%-4d", a [I]);} printf ("\ n"); InsertSort2 (, n); printf ("sorted array a \ n"); printArray (a, N); printf ("\ n"); system ("pause "); return 0 ;}
       
      
     
    
A running result is as follows:

Conclusion: here we use the left-closed and right-open interval to facilitate the termination of the subsequent loop. The first and last moves are easy to understand. When inserting data to the second path, elements do not need to be moved. This is the improvement of the second path insertion compared with the first three methods. If a [0] is the smallest or largest element, it degrades to direct insertion, and the number of moves cannot be reduced. In code 2, about the number of elements d = (high-low + n) % n, note: Because the selected range is left closed and right open [low, high ), for example, in the range [1, 2), the number of integers is 2-1 = 1, but in the range [1, 2], the number of integers is 2-1 + 1 = 2, so here is high-low, + n, you know. If it is written as d = (high-low + 1 + n) % n, it will enter an endless loop. You can try it.

Code is tossing, but more tossing makes progress!
Reprint please indicate the source, this article address: http://blog.csdn.net/zhangxiangdavaid/article/details/27958143
If you think it is good, do it first!

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.