简体   繁体   中英

Using C to implement radix sort

I got stuck in the below question for a few days. I used C to implement radix sort, everything was fine except for one line of code. Could you please help me to solve this issue?

My problem is in the first line of radix_sort function. While I use int semi_sort[12] , I can run the program correctly. However, I wanna use the size variable that I passed into the function, but when I use int semi_sort[size] instead of int semi_sort[12] , my program would crash. Could anyone tell me why is that? BY the way, I referred to this link , in this author's codes, he did int semiSorted[size] . Why does this line of code work this time?

Thank you in advance!!

#include <stdio.h>
#include <stdlib.h>

#define bucket_size 10

int find_the_largest(int arr[],int size);
void display(int arr[],int size);
void radix_sort(int arr[],int size);

int main()
{
    printf("------------------------------------------------------\n");
    printf("        Hey! This is a radix sort algorithm!\n");
    printf("------------------------------------------------------\n\n");
    int array[] = {10, 2, 303, 4021, 293, 1, 0, 429, 480, 92, 2999, 14};
    int size = sizeof(array)/sizeof(int);
    int largest_num = find_the_largest(array,size);
    printf("The unsorted array:");
    display(array,size);
    printf("The radix sort algorithm:\n\n");
    radix_sort(array,size);
    display(array,size);
    return 0;
}

int find_the_largest(int arr[],int size){
    int i,max_num=0;
    for(i=0;i<size;i++){
        if(arr[i]>max_num)
            max_num = arr[i];
    }
    return max_num;
}

void display(int arr[],int size){
    int i;
    for(i=0;i<size;i++){
        printf(" %d",arr[i]);
    if(i==size-1)
        printf("\n\n");
    }
}

void radix_sort(int arr[],int size){

    int semi_sort[12];
    int max_num = find_the_largest(arr,size);
    int i,significant_num = 1;

    while(max_num/significant_num>0){
        int bucket[bucket_size] = {0};

        for(i=0;i<size;i++){
            bucket[(arr[i]/significant_num)%10]++;
        }

        for(i=1;i<size;i++){
            bucket[i] += bucket[i-1];
        }

        for(i=size-1;i>=0;i--){

            semi_sort[--bucket[(arr[i]/significant_num)%10]] = arr[i];
        }


        for(i=0;i<size;i++)
            arr[i] = semi_sort[i];

        significant_num *= 10;
    }
}

You have problem with code:

for(i=1;i<size;i++){
    bucket[i] += bucket[i-1];
}

Because size can be greater then bucket_size .

And probably you have problem with:

for(i=size-1;i>=0;i--){
    semi_sort[--bucket[(arr[i]/significant_num)%10]] = arr[i];
}

Because --bucket[(arr[i]/significant_num)%10] can be greater then 11 or less then 0.

And find_the_largest does not work correct on negative numbers.

You can dynamically allocate memory for your buffers, like this:

semi_sort = malloc(size * (sizeof *semi_sort));

And don't forget to free memory on end ( free(semi_sort) ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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