我试图让这种基于队列的基数排序工作,但我似乎无法弄清楚它有什么问题。它使用文本文件作为输入介质,当我尝试编译它并使用文本文件运行它时会引发大量错误。
在这一点上,任何建议都会有所帮助。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 10
#define SHOWPASS
//Compiled Using GNU GCC Compiler
void radixsort(int *a[], int n)
{
int i, b[MAX], m = *a[0], exp = 1;
for (i = 0; i < n; i++)
{
if (*a[i] > m)
m = a[i];
}
while (m / exp > 0)
{
int queue[10] =
{ 0 };
for (i = 0; i < n; i++)
queue[*a[i] / exp % 10]++;
for (i = 1; i < 10; i++)
queue[i] += queue[i - 1];
for (i = n - 1; i >= 0; i--)
b[--queue[*a[i] / exp % 10]] = *a[i];
for (i = 0; i < n; i++)
*a[i] = b[i];
exp *= 10;
#ifdef SHOWPASS
printf("\nPASS : ");
radixsort(a, n);
#endif
}
}
int main( int argc, char *argv[] )
{
if ( argc != 3 )
{
printf("Need two input parameters in the following order: \n 1. Input file path \n 2. Number of elements in file\n");
return 0;
}
int num_elements = atoi(argv[2]);
int *input_arr = (int*) calloc (num_elements, sizeof(int));
int i;
FILE *fin; //File pointer to read input file
fin = fopen(argv[1], "r"); //Initialize file pointer
for(i=0; i<num_elements; i++)
{
fscanf(fin, "%d", &(input_arr[0]));
}
radixsort(input_arr[0], i);
printf ( "\nArray before sorting: \n") ;
for ( i = 0 ; i < num_elements ; i++ )
printf ( "%d\t", input_arr[0] ) ;
printf ( "\n\n");
return 0;enter code here
}