Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 计数排序混乱。不排序数组(c+;+;)_C++_Algorithm_Sorting - Fatal编程技术网

C++ 计数排序混乱。不排序数组(c+;+;)

C++ 计数排序混乱。不排序数组(c+;+;),c++,algorithm,sorting,C++,Algorithm,Sorting,我在研究计数排序,决定尝试一种我在网上找到的算法。不过,它似乎并没有对数组进行实际排序 void countSort2(int arr[], int n, int exp) { int *output = new int[n]; // output array int i, count[10] = {0}; // Store count of occurrences in count[] for (i = 0; i < n; i++) co

我在研究计数排序,决定尝试一种我在网上找到的算法。不过,它似乎并没有对数组进行实际排序

void countSort2(int arr[], int n, int exp)
{
    int *output = new int[n]; // output array
    int i, count[10] = {0};

    // Store count of occurrences in count[]
    for (i = 0; i < n; i++)
        count[ (arr[i]/exp)%10 ]++; 

    // Change count[i] so that count[i] now contains actual position of
    // this digit in output[]
    for (i = 1; i < 10; i++)
        count[i] += count[i - 1];

    // Build the output array
    for (i = n - 1; i >= 0; i--)
    {
        output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
        count[ (arr[i]/exp)%10 ]--;
    }

    // Copy the output array to arr[], so that arr[] now
    // contains sorted numbers according to curent digit
    for (i = 0; i < n; i++)
        arr[i] = output[i];
}

int main()
{
    int b[10] = {4,3,2,1,6,7,8,9,7,6};
    countSort2(b,10,10);
    int i = 0;
    while(i<10)
    {
        cout<<b[i]<<endl;
        i++;
    }
void countSort2(int arr[],int n,int exp)
{
int*output=newint[n];//输出数组
int i,计数[10]={0};
//在计数[]中存储发生次数的计数
对于(i=0;i=0;i--)
{
输出[count[(arr[i]/exp)%10]-1]=arr[i];
计数[(arr[i]/exp)%10]-;
}
//将输出数组复制到arr[],使arr[]立即
//包含根据当前数字排序的数字
对于(i=0;i而(i这就是你如何调用这个方法.)

10是元素的数量

int main()
{
   int b[10] = {14,23,22,11,66,67,58,49,17,16};
    countSort2(b,10,1);
    countSort2(b,10,10);

    int i = 0;
    while(i<10)
    {
        cout<<b[i]<<endl;
        i++;
    }
   return 0;
}
intmain()
{
int b[10]={14,23,22,11,66,67,58,49,17,16};
countSort2(b,10,1);
countSort2(b,10,10);
int i=0;

而(i这是一种基数排序,按十进制数字对数组进行排序。排序从最低有效位到最高有效位。这意味着一系列调用exp=1、10、100、1000、10000等

下面是一个基数排序的示例,它按整数中的字节对64位无符号整数数组进行排序,从最低有效到最高有效。在此示例中,临时数组作为参数传递给RadixSort()

typedef unsigned\uuu int64 UI64;
typedef unsigned__int64*PUI64;
PUI64 RadixSort(PUI64 pData、PUI64 pTemp、大小计数)
{
size_t mIndex[8][256]={0};//索引矩阵
PUI64 pDst、pSrc、pTmp;
尺寸i,j,m,n;
UI64U;
对于(i=0;i>=8;
}       
}
对于(j=0;j<8;j++){//转换为索引
n=0;
对于(i=0;i<256;i++){
m=mIndex[j][i];
mIndex[j][i]=n;
n+=m;
}       
}
pDst=pTemp;//基数排序
pSrc=pData;
对于(j=0;j<8;j++){
对于(i=0;im=(size_t)(u>>(jwill
arr[i]/exp
不会总是计算为零?所有
arr
元素都小于
exp=10
,并且是整数除法。我不理解您的构建输出循环:它应该在什么时候决定移动到
count
中的下一个元素?噢,我用countsort2(b,10,1)来调用它它对所有的数据进行了排序!我该如何对大于个位数的数字进行排序?@user2796815您应该按下面的两倍调用计数排序
typedef unsigned __int64    UI64;
typedef unsigned __int64 *  PUI64;

PUI64 RadixSort(PUI64 pData, PUI64 pTemp, size_t count)
{
size_t mIndex[8][256] = {0};            // index matrix
PUI64 pDst, pSrc, pTmp;
size_t i,j,m,n;
UI64 u;

    for(i = 0; i < count; i++){         // generate histograms
        u = pData[i];
        for(j = 0; j < 8; j++){
            mIndex[j][(size_t)(u & 0xff)]++;
            u >>= 8;
        }       
    }
    for(j = 0; j < 8; j++){             // convert to indices
        n = 0;
        for(i = 0; i < 256; i++){
            m = mIndex[j][i];
            mIndex[j][i] = n;
            n += m;
        }       
    }

    pDst = pTemp;                       // radix sort
    pSrc = pData;
    for(j = 0; j < 8; j++){
        for(i = 0; i < count; i++){
            u = pSrc[i];
            m = (size_t)(u >> (j<<3)) & 0xff;
            pDst[mIndex[j][m]++] = u;
        }
        pTmp = pSrc;
        pSrc = pDst;
        pDst = pTmp;
    }

return(pSrc);