C ++ insert sorting two-way insertion (comparison between loops and non-loops)

Source: Internet
Author: User

C ++ insert sorting two-way insertion (comparison between loops and non-loops)

# Include
  
   
Using namespace std; void Grial (int a [], int n) {int I, j; I = j = 0; int B [n]; B [0] = a [I]; int k; for (k = 1; k
   
    
A [k]) {B [(J-1 + n) % n] = a [k]; j = (J-1 + n) % n;} else {I ++; int m; for (m = I; B M-1]> a [k]; m --) {B [(m + n) % n] = B [(m-1 + n) % n];} B [(m + n) % n] = a [k] ;}} I ++; k = 0; do {a [k ++] = B [(I) % n]; I ++;} while (I % n! = J);} int main () {int a [] = {10, 4, 2, 5, 88,1, 23}; Grial (a, 7); for (int I = 0; I <7; I ++) {cout <using namespace std; // 2-way insertion sorting. void Grial (int a [], int n) {int I; int j; int B [n]; // new space is used for sorting. B [0] = a [0]; I = 1; j = n-1; int k; for (k = 1; k
    
     
(N-j )? 1:-1; // compare the length on the left with the length on the right. If the left is large, flag 1 and else-1. if (a [k]> B [I-1]) {B [I] = a [k]; I ++;} else if (a [k]
     
      
B [m + 1]) B [m] = B [m + 1]; else {B [m] = a [k]; break ;}} j --;} else {int m; for (m = I; B M-1]> a [k] & m> 0; m --) {B [m] = M-1];} B [m] = a [k]; I ++ ;}} j = I; k = 0; do {a [k ++] = B [(I + n) % n]; I ++;} while (I % n )! = J);} int main () {int a [] = {, 10, 8,-1}; Grial (a, 10 ); for (int I = 0; I <10; I ++) {cout <
     
    
   
  

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.