41 lines
889 B
C
41 lines
889 B
C
|
#include <stdio.h>
|
||
|
|
||
|
#define MAX_TERMS 100
|
||
|
#define MAX_COL 100
|
||
|
|
||
|
struct Element {
|
||
|
int row, col, value;
|
||
|
};
|
||
|
|
||
|
void fastTranspose(struct Element *a, struct Element *b, int m, int n, int num) {
|
||
|
int rowTerms[MAX_COL] = {0};
|
||
|
int rowStart[MAX_COL] = {0};
|
||
|
int i, j;
|
||
|
|
||
|
for (i = 0; i < num; i++)
|
||
|
rowTerms[a[i].col]++;
|
||
|
|
||
|
for (i = 1; i < n; i++)
|
||
|
rowStart[i] = rowStart[i-1] + rowTerms[i-1];
|
||
|
|
||
|
for (i = 0; i < num; i++) {
|
||
|
j = rowStart[a[i].col]++;
|
||
|
b[j].row = a[i].col;
|
||
|
b[j].col = a[i].row;
|
||
|
b[j].value = a[i].value;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
struct Element a[MAX_TERMS] = {{0, 1, 3}, {1, 2, 5}, {2, 0, 2}, {3, 1, 4}};
|
||
|
struct Element b[MAX_TERMS];
|
||
|
int m = 4, n = 3, num = 4;
|
||
|
|
||
|
fastTranspose(a, b, m, n, num);
|
||
|
|
||
|
for (int i = 0; i < num; i++)
|
||
|
printf("%d %d %d\n", b[i].row, b[i].col, b[i].value);
|
||
|
|
||
|
return 0;
|
||
|
}
|