C + + realization Matrix in-situ transpose algorithm _c language

Source: Internet
Author: User

This article describes the C + + implementation matrix in situ transpose algorithm, is a very classical algorithm, I believe that learning C + + algorithm friends have a lot of help. Specifically as follows:

I. Description of the problem

Microsoft Interview question: Store an MXN matrix in a one-dimensional array, and programmatically implement the transpose of the Matrix.

Requirements: space Complexity of O (1)

Second, the analysis of ideas

The following is an analysis of a 4x2 matrix a={1,2,3,4,5,6,7,8}, and the transpose process is shown below:

The red number in the lower-right corner of the figure represents the subscript in a one-dimensional array. The transpose of the matrix is actually the movement of the elements in the array, the specific moving process is as follows:

We found that the subscripts of these moving elements were loops, the elements of subscript 1 moved to 4, the elements of subscript 4 moved to 2, and the elements of subscript 2 moved to 1. In writing a program, we need to solve two problems: the first one is how to determine whether the loop is repeated (processed), and the second is how to calculate the predecessor and successor of the subscript of the current element.

The first question: How do you tell if the loop is repeated and processed? As we traverse the entire array, the subscript is small to large, so if this is the first time the loop is traversed, the first subscript must be the smallest in the ring. If a ring is processed, then it is always possible to find a successor to it that is smaller than it. As you can see from the above picture.

Second question: How to calculate the precursor and successor of the current element subscript? Assuming that the array of an element is labeled I before the transpose, it is listed as (i/n, i%n), and the row is (i%n, i/n) after the transpose, which can be computed after the reset (i%n) *m+i/n, which is the successor of I. Assuming that after the transpose an array of an element is labeled I, then it is listed (i/m, i%m), then the row is listed (i%m, i/m) before the transpose, which is then evaluated at this point (i%m) *n+i/m, which is the precursor of I.

Three, the code implementation is as follows:

/************************************************************************* > File Name:matrix_transpose.cpp ; Author:songlee ************************************************************************/#include <iostream 
 
> Using namespace std; 
/* Successor/INT getNext (int i, int m, int n) {return (i%n) *m + i/n; 
}/* Precursor/int getpre (int i, int m, int n) {return (i%m) *n + i/m;    }/* handles the ring/void movedata (int *mtx, int i, int m, int n) {int temp = Mtx[i] With the following Mark I as the starting point;//staging int cur = i; 
  Current subscript int pre = Getpre (cur, m, n); 
    while (pre!= i) {mtx[cur] = Mtx[pre]; 
    cur = pre; 
  Pre = Getpre (cur, m, n); 
} Mtx[cur] = temp; /* Transpose, that is, loops all loops/void transpose (int *mtx, int m, int n) {for (int i=0; i<m*n; ++i) {int next = GETN 
    Ext (i, M, N); 
    while (Next > I)//If there is successor less than I the description repeats next = GetNext (Next, M, N); 
  if (next = i)//handle Current ring Movedata (MTX, I, M, N); }/* Output matrix/void PrinT (int *mtx, int m, int n) {for (int i=0; i<m*n; ++i) {if ((i+1)%n = = 0) cout << mtx[i] <&lt ; 
    "\ n"; 
  else cout << Mtx[i] << ""; 
  }/* Test/INT main () {int matrix[4*2] = {1,2,3,4,5,6,7,8}; 
  cout << "before matrix transposition:" << Endl; 
  Print (Matrix, 4, 2); 
  Transpose (Matrix, 4, 2); 
  cout << "After matrix transposition:" << Endl; 
  Print (Matrix, 2, 4); 
return 0;  }

The results of the operation are shown in the following illustration:

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.