qq_39552268
儒雅随和狗粉丝
2017-09-13 13: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条回答