V
vaste
Why does java use to quicksort to sort byte arrays? Wouldn't it be
much faster to just count the elements? (Almost count-sort.)
Similarily for shorts, although not always obviously faster (requires
65k*4).
E.g. one could do:
public static void sort(byte[] a) {
int[] reg = new int[256];
for(byte b : a)
reg[b+128]++;
int i = 0;
for(int k = 0; k < 256; k++)
for(int j = 0; j < reg[k]; j++)
a[i++] = (byte)(k-128);
}
I tried some, and it is indeed faster. Then again, I suppose it's
quite rare that one need to "sort" byte arrays without sattelite data.
much faster to just count the elements? (Almost count-sort.)
Similarily for shorts, although not always obviously faster (requires
65k*4).
E.g. one could do:
public static void sort(byte[] a) {
int[] reg = new int[256];
for(byte b : a)
reg[b+128]++;
int i = 0;
for(int k = 0; k < 256; k++)
for(int j = 0; j < reg[k]; j++)
a[i++] = (byte)(k-128);
}
I tried some, and it is indeed faster. Then again, I suppose it's
quite rare that one need to "sort" byte arrays without sattelite data.