2 qq 39552268 qq_39552268 于 2017.09.13 21:56 提问

请教一些数据结构的问题~~~

void main()
{
int inputNumber;

    int a[] = { 70,32,40,12,85,23,901,100,75,60 };
    int len = sizeof(a) / sizeof(a[0]);
    RadixSort(a, len);
    for (int i = 0; i < len; i++)
    {
        cout << a[i] << " ";
    }

}
int maxbit(int a[], int len)//最大是几位数
{
    int d = 1;
    for (int i = 0; i < len; i++)
    {
        int c = 1;
        int p = a[i];
        while (p / 10)
        {
             p = p / 10;
            c++;
        }
        if (c > d)
        {
            d = c;
        }
    }

    return d;

}
void RadixSort(int a[], int len)
{

    int temp[10];
    int count[10];
    int r = 1;
    int d = maxbit(a, len);

    for (int i = 1; i <= d; i++)
    {
        for (int i = 0; i < 10; i++)//装桶前先清桶
        {
            count[i] = 0;
        }
        for (int i = 0; i < 10; i++)
        {
            int k = a[i] / r;
            int q = k % 10;
            count[q]++;

        }
        for (int i = 1; i < 10; i++)//计算位置
        {
            count[i] += count[i - 1];
        }


        **for (int j = len-1; j>=0; j--)
        {
            int k = a[j] / r;
            int q = k % 10;
            temp[count[q] - 1] = a[j];
            count[q]--;
        }**
        for (int i = 0; i < len; i++)

        {
            a[i] = temp[i];
        }
        r = r * 10;
    }

}

问题:
for (int j = 0; j<len; j++)
{
int k = a[j] / r;
int q = k % 10;
temp[count[q] - 1] = a[j];
count[q]--;
}
为什么这段代码不能用下面这段代码代替 ,下面这段代码代替的话 排序不对
for (int j = 0; j<len; j++)
{
int k = a[j] / r;
int q = k % 10;
temp[count[q] - 1] = a[j];
count[q]--;
}

2个回答

m0_38124502
m0_38124502   2017.09.14 12:12

你两段代码都一样吧?

qq_39552268
qq_39552268 。。。不好意思 for (int j = len-1; j>=0; j--) { int k = a[j] / r; int q = k % 10; temp[count[q] - 1] = a[j]; count[q]--; }**
2 个月之前 回复
qq_39552268
qq_39552268   2017.09.17 22:43

for (int j = len-1; j>=0; j--)
{
int k = a[j] / r;
int q = k % 10;
temp[count[q] - 1] = a[j];
count[q]--;
}
是这段代码 为什么不能用下面这段代替
for (int j = 0; j<len; j++)
{
int k = a[j] / r;
int q = k % 10;
temp[count[q] - 1] = a[j];
count[q]--;
}

Csdn user default icon
上传中...
上传图片
插入图片