For a project that I'm working on, I need to implement Burrows-Wheeler's MoveToFront transformation in O(n) space. For some reason, though, my code works on most values that I throw at it, but not all.
My implementation looks something like this:
public byte[] transform (byte[] input)
{
if (input.length == 0)
return input;
IndexedByte[] bytes = new IndexedByte[input.length];
for (int i = 0; i < input.length; i++)
{
bytes[i] = new IndexedByte(input[i],i);
}
for (int i = 0; i < input.length -1; i++)
{
bytes[i].next = bytes[i+1];
}
bytes[input.length - 1].next = bytes[0];
Arrays.sort(bytes);
byte[] newBytes = new byte[input.length];
for (int i = 0; i < bytes.length; i++)
newBytes[i] = bytes[i].b;
int[] indexes = new int[input.length];
for (int i = 0; i < indexes.length; i++)
indexes[i] = (bytes[i].origIndex + (input.length - 1)) % input.length;
int x = 0;
String str = new String(input);
for (int i = 0; i < input.length; i++)
{
if (bytes[i].origIndex == 0)
{
x = i;
break;
}
}
byte[] header = intToByteArray(x);
byte[] result = new byte[indexes.length+header.length];
for (int i = 0; i < header.length; i++)
result[i] = header[i];
for (int i = 0; i < indexes.length; i++)
result[i+header.length] = input[indexes[i]];
return result;
}
Any advice on what I'm doing wrong here? It seems that this does not work when a non-alphanumeric character is encountered (i.e. encoding itself, it seems that the /*, etc. will screw it up).