[英]Radix sort digit compares
I have an implementation of LSD Radix Sort algorithm and was wondering how to count the number of digit comparisons during the sort procedure? 我有一个LSD Radix Sort算法的实现,想知道在排序过程中如何计算数字比较的次数? I know that the algorithm is not comparison based but there is still some kind of a comparison between digits of the Integer elements the algorithm sorts.
我知道该算法不是基于比较的,但是在该算法排序的Integer元素的数字之间仍然存在某种比较。 Can someone point out where the comparison is taking place?
有人可以指出进行比较的地方吗?
Thanks! 谢谢!
LSDRadixSort: LSDRadixSort:
public static void lsdRadixSort(int[] a)
{
final int BITS = 32; // each int is 32 bits
final int R = 1 << BITS_PER_BYTE; // each bytes is between 0 and 255
final int MASK = R - 1; // 0xFF
final int w = BITS / BITS_PER_BYTE; // each int is 4 bytes
int n = a.length;
int[] aux = new int[n];
for(int d = 0; d < w; d++)
{
// compute frequency counts
int[] count = new int[R+1];
for(int i = 0; i < n; i++)
{
int c = (a[i] >> BITS_PER_BYTE*d) & MASK;
count[c + 1]++;
}
// compute cumulates
for(int r = 0; r < R; r++)
{
count[r+1] += count[r];
}
//for most significant byte, 0x80-0xFF comes before 0x00-0x7F
if(d == (w - 1))
{
int shift1 = count[R] - count[R/2];
int shift2 = count[R/2];
for(int r = 0; r < R/2; r++)
{
count[r] += shift1;
}
for(int r = (R/2); r < R; r++)
{
count[r] -= shift2;
}
}
// move data
for(int i = 0; i < n; i++)
{
int c = (a[i] >> BITS_PER_BYTE*d) & MASK;
aux[count[c]++] = a[i];
}
// copy back
for(int i = 0; i < n; i++)
{
a[i] = aux[i];
}
}
}
Well, as you said, there are no comparisons. 好吧,正如您所说,没有可比之处。 The operation that comes closest to that is:
最接近的操作是:
count[c + 1]++;
where c
is a byte of the integer. 其中
c
是整数的字节。 Each integer has 4 bytes, so you do it exactly 4*n
times. 每个整数都有4个字节,因此您恰好执行了
4*n
次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.