I'm trying to find a good elegant way to bubble sort a 24 bits input. it seems I have an issue with my code but I can't figure out the problem. I'm new to Java, so please be compassionate. thanks!
public static void sortTriBytes(RandomAccessFile file) throws IOException {
for (long i = 1; i < file.length()/3; i=i+3){
for (long j = 0; j < file.length()-(3*(i-1)); j=j+3){
String leftStr = "";
String rightStr = "";
file.seek(j);
int left1 = file.readUnsignedByte();
int left2 = file.readUnsignedByte();
int left3 = file.readUnsignedByte();
leftStr+= Integer.toBinaryString(left1);
leftStr+=Integer.toBinaryString(left2);
leftStr+=Integer.toBinaryString(left3);
int actualleft = Integer.parseInt(leftStr,2);
int right1 = file.readUnsignedByte();
int right2 = file.readUnsignedByte();
int right3 = file.readUnsignedByte();
rightStr+= Integer.toBinaryString(right1);
rightStr+= Integer.toBinaryString(right2);
rightStr+= Integer.toBinaryString(right3);
int actualright = Integer.parseInt(rightStr,2);
if (actualleft > actualright) {
file.seek(j);
file.write(right1);
file.write(right2);
file.write(right3);
file.write(left1);
file.write(left2);
file.write(left3);
}
}
}
}
thanks again!