diff --git a/DS/C/Lab/Week4/fasttranspose.c b/DS/C/Lab/Week4/fasttranspose.c new file mode 100644 index 0000000..ca43420 --- /dev/null +++ b/DS/C/Lab/Week4/fasttranspose.c @@ -0,0 +1,40 @@ +#include + +#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; +}