정수에 대해 오른쪽/LSB 기수 정렬을 구현하려고 시도 중이고 일단 작동하면 병렬 처리하려고 시도합니다. 내 순차 코드는 부호없는 숫자에 대해서는 잘 작동하지만 일단 음의 정수를 던지면 부호있는 비트를 보지 않고 0부터 까지 양수 (소트 된 정수)로 끝납니다. n은 음수와 섞인입니다.) 정수는 -n부터 -1까지입니다.오른쪽 (LSB) 기수 - 2의 보수를 처리하는 방법?
public class SeqRadix {
static void radix2(int[] a) {
// 2 digit radixSort: a[]
int max = a[0];
int numBit = 2;
int n = a.length;
// a) find max value in a[]
for (int i = 1; i < n; i++){
if (a[i] > max) {
max = a[i];
}
}
while (max >= (1 << numBit)){
numBit++; // digits in max
}
// decide num of bits in bit1 and bit2
int bit1 = numBit/2, bit2 = numBit - bit1;
int[] b = new int[n];
radixSort(a, b, bit1, 0); // first digit from a[] to b[]
radixSort(b, a, bit2, bit1);// second digit, back from b[] to a[]
} // end
/**
* Sort a[] on one digit ; number of bits = maskLen, shiftet up �shift�
* bits
*/
static void radixSort(int[] a, int[] b, int maskLen, int shift) {
int acumVal = 0, j, n = a.length;
int mask = (1 << maskLen) - 1;
int[] count = new int[mask + 1];
// b) count=the frequency of each radix value in a
for (int i = 0; i < n; i++) {
count[(a[i] >> shift) & mask]++;
}
// c) Add up in 'count' - accumulated values
for (int i = 0; i <= mask; i++) {
j = count[i];
count[i] = acumVal;
acumVal += j;
}
// c) move numbers in sorted order a to b
for (int i = 0; i < n; i++) {
b[count[(a[i] >> shift) & mask]++] = a[i];
}
}// end radixSort
}// end SekvensiellRadix
LSB들에 첫번째 종류 시작하고 점점 더 중요한 비트, 마지막 2의 보수로/부호 비트가 잡히지 않습니다 보인다 :
여기 내 코드입니다. 미리 감사드립니다!
하지만 당신은 "서명 비트가"뿐만 아니라이 플래그의 역할을 기억해야하지만, 실제 음수 값으로 - 숫자의 다른 모든 비트는 양의 값과 비교하여 "거꾸로"됩니다. 이것이 2의 보완법이며 이것이 바로 해결하기가 어려운 이유입니다. http://en.wikipedia.org/wiki/Two%27s_complement –
여기서의 트릭은 부호 비트 반전 된 2의 보수가 부호없는 순서의 부호없는 순서가 부호있는 순서와 정확히 동일하다는 것입니다. 그래서 당신이 비교를 엉망으로 만들지 않는 한 (서명되지 않은 언어 요소가 없기 때문에 자바에서는 쉽게 알 수있다) 올바른 순서로 끝난다.위키 문서가 도움이되는 동안, 더 미세한 세부 사항은이 애플리케이션에 아무런 역할을하지 않습니다. – Durandal