(C)Radix从文本文件中排序数组

时间:2013-03-15 03:15:00

标签: c algorithm sorting radix

我试图让这个基于队列的基数排序工作,但我似乎无法弄清楚它有什么问题。它使用文本文件作为输入媒体,当我尝试编译并使用文本文件运行它时会抛出大量错误。

此时任何建议都会有所帮助。

#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
}

1 个答案:

答案 0 :(得分:0)

您的代码中存在大量错误。首先,你输入的方式不正确。

fscanf(fin, "%d", &(input_arr[0]));

在获取输入时,数组input_arr在input_arr [0]处填充了单个输入值。输入的其余部分被覆盖在input_arr [0]。

将其替换为fscanf(fin, "%d", &(input_arr[i]));

即使您以不正确的方式显示输出。排序后,由于以下错误的陈述,相同的输出将显示num_elements次:

printf ( "%d\t", input_arr[0] ) ;

再次将上述语句替换为printf ( "%d\t", input_arr[i] )

由于遵循错误陈述的影响,

fscanf(fin, "%d", &(input_arr[i]));

您的程序遇到Segmentation fault,因为在函数radixsort中,您正在从0迭代到n-1(元素数量)。

 for (i = 0; i < n; i++)
 {
   if (*a[i] > m)
   m = a[i];
 }

由于只有[0]填充了输入值,并且数组的其余部分(从[1]到[n-1])包含垃圾值,因此在执行代码时会出现运行时错误。< / p>

还有许多其他错误,我已经修复了。这是完美的运行代码:

#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[i]));
    }


    radixsort(input_arr, i);


    printf("\nArray before sorting: \n");

    for (i = 0; i < num_elements; i++)
        printf("%d\t", input_arr[i]);
    printf("\n\n");
    return 0;
}