Binary, two-way insertion sort

Binary insertion sort: direct insertion sort elements is inserted in front of the elements to be compared one by one, to find the right position of the insert. It is to find the binary insertion position by the binary, a so-called binary (refer binary search) is the definition of two variables start point to the head and tail End, whichever is the middle value mid. If the element is larger than the element to be inserted into the mid position between the mid and end on search again, if less than 10, between start and mid lookup. Then shrinking range until find the right position (less than the right greater than left), and then is inserted element.

. 1 #include <stdio.h> 
 2  #define n-. 6
 . 3  
. 4  
. 5  void binaryInsertSort ( int NUM []) {
 . 6  
. 7      int Start, End, MID, I, J, T;
 . 8      for (I = . 1 ; I <n- ; I ++ ) {
 . 9          start = 0 ;                         // start position starts from 0 
10          end = I- . 1 ;                         // end position of a location in front of the element to be inserted 
. 11          the while (start <= end) {                 // cycle condition 
12 is              MID = (start + end) /2 ;         
 13 is              IF (NUM [I]> = NUM [mid])         // if this condition is satisfied 
14                  Start = mid + . 1 ;         // it shows the position of the insertion element should be inserted between the mid and End 
15              the else 
16                  End mid = - . 1 ;             // otherwise described elements to be inserted should be inserted between the start position and the mid 
. 17          }
 18 is          
. 19          IF (start> mid)         // If start> mid, described element to be inserted is larger than the mid position of the element, but smaller than mid + 1 position of the element 
20 is              mid ++;             // At this point insertion position 1 should be mid +
 21 is                              // Another case is the end <mid, described element to be inserted is smaller than the mid position of the element 
22                              // At this time, the insertion position should be MID 
23 is                  
24          T = NUM [I];                 // here is the insertion element 
25          for (J = I- . 1 ; J> = MID; J,)     // Use inline sorting code is inserted here thinking element          
26 is              NUM [+ J . 1 ] = NUM [J];
 27          NUM [+ J . 1 ] = T;
 28          
29      }
 30      
31 is }

 

Road insertion sort: in front of each insertion element insertion sort is going to be more mobile elements, Road insertion sort them improved. The idea is: In the first comparison element as the element, is greater than the number of all the elements on the front of all behind, a number less than all of the elements on the back, the element is greater than or less than the insertion portion when using the direct insertion sort to ensure sequence, when all the elements of good distribution, in fact, has become a two ordered array area, complete sequencing in a good combination.

Define first, final point two ordered regions, illustrated as follows:

 

 1 #include<stdio.h>
 2 #define n 6
 3 
 4 
 5 void twoInsertSort(int num[]){
 6     
 7     int i,j,first,final,temp[n+1]={0};    //临时存放数组比原有数组多一个空间 
 8     first = 0;                            //first、final分别指向临时存放数组的开头和结尾 
 9     final = n;
10     temp[0] = num[0];                    //数组第一个元素作为比较元素 
11     
12     for(i=1;i<n;i++){                    
13         if(num[i]>=num[0]){                //大于第一个元素的数放在临时数组的前面 
14             j = first;                    //first作为大于第一个元素的数的最后元素的下标 
15             while(num[i]<=temp[j]){        //在这里使用直接插入,使其有序 
16                 temp[j+1] = temp[j];     
17                 j--;
18             }
19             temp[j+1] = num[i];
20             first++;
21         }
22         else{
23             j = final;                    //小于第一个元素的数放在临时数组的后面 
24             while(num[i]>temp[j]){        //在这里使用直接插入,使其有序 
25                 temp[j-1] = temp[j];    
26                 if((++j)>=n+1)            //这里是为了防止数组向后越界 
27                     break;
28             }
29             temp[j-1] = num[i];
30             final--;                    //final作为小于第一个元素的数的最前元素的下标  
31         }                                //但final指向了最前元素的前一个位置    
32     }
33     
34     for(i=0;i<n-1-first;i++)            //将临时数组存放到原来的数组 
35         num[i] = temp[++final];
36         
37     for(j=0;i<n;j++,i++)
38         num[i] = temp[j];
39     
40 }
41 
42 void main(){
43     
44     int i,num[n] = {890,761,812,761,810,261};
45     twoInsertSort(num);
46     
47     for(i=0;i<n;i++)
48         printf("%d ",num[i]);
49     
50 }

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/lsy-lsy/p/12031835.html