우선이 것은 숙제이며 같은 주제에 대해 다른 주제를 찾았지만 대답이 없습니다.비트 단위 연산을 사용하는 기수 정렬
정렬 할 값이 정수 B 비트 (따라서 0과 2B-1 사이) 인 것으로 가정하여 비트별로 정렬합니다.
주요 문제는 이런 종류의 정렬을 만드는 방법입니다. 각 정수를 비트로 변환하고 비교해야합니까? 해답을 알려주지 마십시오. 힌트 나 해명 방법에 대한 설명이 없습니다. 도움 주셔서 감사합니다. [편집] 나는 인터넷에서이 스크립트를 찾았지만 어떻게 작동하는지 나는 이해하지 못했다 : 모든
#include <cstdlib>
#include <iostream>
#include <string>
#include <cctype>
#include<algorithm>
#include<string>
#include <iterator>
using namespace std;
// Radix sort comparator for 32-bit two's complement integers
class radix_test
{
const int bit; // bit position [0..31] to examine
public:
radix_test(int offset) : bit(offset) {} // constructor
bool operator()(int value) const // function call operator
{
if (bit == 31) // sign bit
return value < 0; // negative int to left partition
else
return !(value & (1 << bit)); // 0 bit to left partition
}
};
// Least significant digit radix sort
void lsd_radix_sort(int *first, int *last)
{
for (int lsb = 0; lsb < 32; ++lsb) // least-significant-bit
{
std::stable_partition(first, last, radix_test(lsb));
}
}
// Most significant digit radix sort (recursive)
void msd_radix_sort(int *first, int *last, int msb = 31)
{
if (first != last && msb >= 0)
{
int *mid = std::partition(first, last, radix_test(msb));
msb--; // decrement most-significant-bit
msd_radix_sort(first, mid, msb); // sort left partition
msd_radix_sort(mid, last, msb); // sort right partition
}
}
int main(int argc, char *argv[])
{
int data[] = { 170, 45, 75, -90, -802, 24, 2, 66 };
lsd_radix_sort(data, data + 8);
// msd_radix_sort(data, data + 8);
std::copy(data, data + 8, std::ostream_iterator<int>(std::cout, " "));
system("PAUSE");
return EXIT_SUCCESS;
}
b 비트의 부호없는 정수 값 범위는 0 ~ 2 ** b-1입니다. – STLDeveloper
"각 정수를 비트로 변환"이란 무엇을 의미합니까 *? *? 그냥 문자열을 묶어서 프로그램이 나오기를 바랍니다. –
@KerrekSB : 숫자를 이진수로 변환하고 다른 이진수와 비교하는 것입니다 (이 제안은 해결책이 아닙니다). – satyres