I know I already asked this kind of question before dealing with vb6 and it was too slow, so I decided to use C# for this job; now the same code runs at double the speed, but still way too slow.
The reason why it's slow is that it starts lexicographical sorting from the end of each column checking all rows.
What I believe will speed this up is if I start the sorting process from the first column checking all rows and detecting the lowest row by first byte for that column and possibly multiple rows with the same identical first low byte and grouping those for the next step which checks the second (next) column finding which of the second bytes is the lowest byte if they are both the same move on to the next column etc.. if it detects where the next row byte is different then the column code is done for the first byte and moves on to finding the second lowest.. that's actually how I thought this process should work to get a good speed boost.. but unfortunately i had a great confusion with this sorting technology and ended up using what somebody helped me with.
The current code works by brute force sorting from the last column it sorts all the rows.. then it moves one column to the left and re-sorts every row again keeps doing this until it reaches the first column and sorts it. This is slow because since it does iterations for no apparent reason.
Say there is 256 columns and 256 rows a total of 65,536 array elements.. using the current code and say it would have to sort each row multiple times until each row gets a proper sorted order. for each column it could possibly take 65,536 iterations. So a total estimated of 256*65536=16,777,216 iterations everytime I call the function and that's the actual reason why it's slow.
I know this is alot to ask for but if anyone got some free time and maybe already did this before could help me out I'd appreciate it.
Here is the code I have to work with so far.
byte[] sortArrayOfArraysLexicoGraphically(ref byte[] data) {
byte[] lexicoGraphicalIndexes;
long dataSize = data.Length;
long squareRootMinusOne;
int squareRoot;
int row = 0;
bool rowSwapped;
byte[] tmpRow;
squareRoot = (int)Math.Sqrt(dataSize);
tmpRow = new byte[squareRoot];
squareRootMinusOne = squareRoot - 1;
lexicoGraphicalIndexes = new byte[squareRoot];
for(short column = 0; column < lexicoGraphicalIndexes.Length; column++) {
lexicoGraphicalIndexes[column] = (byte)column;
}
for(long column = squareRootMinusOne; column >= 0; column -= 1) {
do {
rowSwapped = false;
do {
if(data[(row * squareRoot) + column] > data[((row + 1) * squareRoot) + column]) {
//Swaps a full row in a few copies.
//Copies full row to tmpRow
Buffer.BlockCopy(data, (row * squareRoot), tmpRow, 0, squareRoot);
//Replace first row with second row.
Buffer.BlockCopy(data, ((row + 1) * squareRoot), data, (row * squareRoot), squareRoot);
//Replace second row with tmpRow
Buffer.BlockCopy(tmpRow, 0, data, ((row + 1) * squareRoot), squareRoot);
swapBytes(ref lexicoGraphicalIndexes, row, row + 1);
rowSwapped = true;
}
row++;
} while (row < squareRootMinusOne);
row = 0;
} while (rowSwapped != false);
}
return lexicoGraphicalIndexes;
}
public void swapBytes(ref byte[] data, long firstIndex, long secondIndex) {
byte tmpFirstByte = data[firstIndex];
data[firstIndex] = data[secondIndex];
data[secondIndex] = tmpFirstByte;
}