Algorithms written questions (32): & merge algorithm for reverse order to convert the array elements in the array is the product of the remaining other elements ...

Topic: people are arranged in order from a low to high before and after the queue, if the person in front than the back of another job that is an error;
  example: [176,178,180,170,171] to errors in

    <176,170> <176,171> <178,170> <178,171> <180,170> <180,171>.
  Now asked to identify all such errors from a sequence of integers;

analysis:

  • Reverse order (Inversion Pair): the number of N may determine the size, the number of reverse order of [0, n (n-1) / 2], and merge sort request a sequence number to reverse, time complexity is O (NlogN), the spatial complexity is O (N);
  • Use m = (i + j) / 2 processing digital recursive sequence reverse to the first calculated kid file and sorting; kid file after sorting for obtaining large files involved in reverse, since the files are sorted kid already, can be avoid many of the comparison operation;

Problem solving:

. 1  int the Partition ( int * Array, int I, int J) {
 2          / * *
 . 3           * using additional space O (N) stored final sorted sequence
 . 4           * * / 
. 5          int m = (I + J) / 2 ;
 . 6          int the tarray [J-I + . 1 ]; int index = 0 ;
 . 7          int Ti = I, TJ = m + . 1 ;
 . 8          int COUNT = 0 ;
 . 9  
10          the printf ( " \ n-% D,% D,% D,% D ", I, J, Array [I], Array [J]);
 . 11          the while (Ti <= m && TJ <= J) {
 12 is                  IF (Array [Ti]> Array [TJ]) {
 13 is                          / * *
 14                           * Note that only the elements on the right sequence being less than the left sequence
 15                           * the element, count value does not increase, and according to
 16                           * to characteristics of the sort may be derived reverse order totals
 . 17                           * * / 
18 is                          COUNT + = m-Ti + . 1 ;
 . 19                          the tarray [index] = Array [TJ];
 20 is                          TJ ++ ;
 21 is                  } the else{
 22 is                          tarray [index] = Array [Ti];
 23 is                          Ti ++ ;
 24                  }
 25                  index ++ ;
 26 is          }
 27          / * *
 28           * Note process when the remaining elements of the left and right sequence, because already sorted,
 29           * can be copied directly to tarray in
 30           * * / 
31 is          IF (Ti> m) {
 32                  the while (TJ <= J) {
 33 is                          the tarray [index] = Array [TJ];
 34 is                          TJ ++; index ++ ;
35                 }
36 
37         } else if(tj>j) {
38                 while(ti<=m) {
39                         tarray[index]=array[ti];
40                         ti++;index++;
41                 }
42         }
43 
44         for(int k=i;k<=j;k++)
45                 array[k]=tarray[k];
46 
47         return count;
48 }
49 
50 int Merge(int * Array, int i, int j) {
 51 is          / * *
 52 is           * time when only one element, returns 0
 53 is           * when i and j are adjacent when using alternative direct comparison recursive call
 54 is           * * / 
55          the printf ( " \ n-D **%, D% " , I, J);
 56 is          IF (J == I) return  0 ;
 57 is          IF (I + . 1 == J) {
 58                  IF (Array [I]> Array [J] ) {
 59                          int T = Array [I];
 60                          Array [I] = Array [J];
61 is                          Array [J] = T;
 62 is                          return  . 1 ;
 63 is                  } the else 
64                          return  0 ;
 65          }
 66  
67          / * *
 68           * using the binary recursion, the value of count is determined by three parts:
 69           * around sequences inside each of the reverse order , and left sequences and
 70           reverse between the * right child sequence.
71           * Since after Merge around sequence has been sorted,
 72           * Partition can be done in (N) time complexity O, but
 73           * required complexity of additional O (N) space
 74           * * / 
75          int m = (I + J) / 2 ;
76         int count=0;
77         count+=Merge(array,i,m);
78         count+=Merge(array,m+1,j);
79         count+=Partition(array,i,j);
80 
81         return count;
82 }
83 
84 int main() {
85         int array[]={7,2,1,4,3,5,6};
86         printf("\n%d",Merge(array, 0, 6));
87 
88         return 0;
89 }

 

Topic: a length n of the array a [0], a [1 ], ..., a [n-1]. Now update the array name elements, i.e., a [0] becomes a [1] to a [n-1] of the product, a [1] becomes a [0] and a [2] to a [n-1 ] the product, ..., a [n-1 ] to a [0] to a [n-2] of the product (that is, removal of the current element, all other elements of the product);
  . 1)
  . 2)

Analysis: Create two left array [N] and right [N], for a [i] in terms of, left element product before [i] storing i, the product right after the element [i] storing i, so the left and right initialization requires only two scans of the array, the time complexity is linear, and the division is not used;

Problem solving:

 1 void Transfer(int *array, int length) {
 2         int leftarray[length];
 3         int rightarray[length];
 4 
 5         leftarray[0]=1;
 6         for(int i=1;i<length;i++)
 7                 leftarray[i]=leftarray[i-1]*array[i-1];
 8 
 9         rightarray[length-1]=1;
10         for(int i=length-2;i>-1;i--)
11                 rightarray[i]=rightarray[i+1]*array[i+1];
12 
13         for(int i=0;i<length;i++)
14                 array[i]=leftarray[i]*rightarray[i];
15 }
16 
17 int main() {
18         int array[]={5,2,3,4};
19         int length=4;
20         Transfer(array,length);
21         for(int i=0;i<length;i++)
22                 printf("%d, ",array[i]);
23         return 0;
24 }

 

Reproduced in: https: //www.cnblogs.com/leo-chen-2014/p/3749298.html

Guess you like

Origin blog.csdn.net/weixin_33912246/article/details/94232126