简体   繁体   English

基数排序在输出中显示错误的值

[英]Radix Sort showing wrong values in the output

I am learning dsa and I tried to implement radix sort in c.我正在学习 dsa,我尝试在 c 中实现基数排序。 I used this tutorial from programiz.我使用了 programiz 的本教程。 I implemented it but the array of numbers they are using get sorted correctly but when I used a different array it shows some garbage value.我实现了它,但是他们使用的数字数组得到了正确排序,但是当我使用不同的数组时,它显示了一些垃圾值。

When I insert this当我插入这个
int array[] = {121, 432, 564, 23, 1, 45, 788}; int 数组 [] = {121, 432, 564, 23, 1, 45, 788};
The output is "1 23 45 121 432 564 788" output输出为“1 23 45 121 432 564 788”输出
But When I insert this但是当我插入这个
int array[] = {3,4,1,5,2};整数数组[] = {3,4,1,5,2};
The output is "1 2 3 4 2496" output输出为“1 2 3 4 2496”输出

I implemented the code as it is from the tutorial.我实现了教程中的代码。 Can any one point out the issue谁能指出问题

#include <stdio.h>

void radix_sort(int array[], int size);
void counting_sort(int array[], int size, int place);

int main(void)
{
    int array[] = {3,4,1,5,2};
    int size = sizeof(array) / sizeof(int);

    radix_sort(array, size);

    for (int i = 0; i < size; i++)
        printf("%i ", array[i]);
    printf("\n");
}

void radix_sort(int array[], int size)
{
     int max = array[0];
     for (int i = 1; i < size; i++)
     {
         if (array[i] > max)
         max = array[i];
     }

for (int place = 1; max / place > 0; place *= 10)
    counting_sort(array, size, place);
}

void counting_sort(int array[], int size, int place)
{
    int output[size + 1];
    int max = (array[0] / place) % 10;

    for (int i = 1; i < size; i++)
    {
         if (((array[i] / place) % 10) > max)
             max = array[i];
    }
    int count[max+1];

    for (int i = 0; i < max; ++i)
        count[i] = 0;

    for (int i = 0; i < size; i++)
        count[(array[i] / place) % 10]++;

    for (int i = 1; i < 10; i++)
        count[i] += count[i - 1];

    for (int i = size-1; i >= 0; i--)
    {   
       output[count[(array[i] / place) % 10] - 1] = array[i];
       count[(array[i] / place) % 10]--;
    }

    for (int i = 0; i < size; i++)
       array[i] = output[i];
 }

for (int i = 1; i < 10; i++) { will access the array out of bounds since count[max+1] => count[5+1] so your program has undefined behavior. for (int i = 1; i < 10; i++) {将访问数组越界,因为count[max+1] => count[5+1]所以你的程序有未定义的行为。 It could crash or output garbage or do just about anything.它可能会崩溃或输出垃圾或做任何事情。

You should use i <= max in that loop:您应该在该循环中使用i <= max

for (int i = 1; i <= max; i++)  // use `i <= max` here
    count[i] += count[i - 1];

You also need to zero out the memory of the variable length array count because it will contain indeterminate values after it's been created.您还需要将可变长度数组count的内存清零,因为它在创建后将包含不确定的值。 You tried doing that with for (int i = 0; i < max; ++i) count[i] = 0;您尝试使用for (int i = 0; i < max; ++i) count[i] = 0; but that misses the last element in the array.但这错过了数组中的最后一个元素。 You need i <= max there too:你也需要i <= max

int count[max + 1];
for (int i = 0; i <= max; ++i) count[i] = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM