尝试使用随机编号生成器时出现QuickSort.C分段错误

尝试使用随机编号生成器时出现QuickSort.C分段错误,c,gnuplot,C,Gnuplot,这是一个C语言的快速排序程序,该程序编译时没有任何错误。 但当运行时,选择随机数进行排序。我得到如下输出: sam@TechTosh ~ $ gcc quick.c sam@TechTosh ~ $ ./a.out 1> Read from file 2> Random no. Generator Enter the Choice 2 Starting 10 Segmentation fault 这是节目, 我调试了这个程序中的很多错误,最后它运行了,但不能排序,也不能排序。

这是一个C语言的快速排序程序,该程序编译时没有任何错误。 但当运行时,选择随机数进行排序。我得到如下输出:

sam@TechTosh ~ $ gcc quick.c
sam@TechTosh ~ $ ./a.out

1> Read from file
2> Random no. Generator


Enter the Choice
2
Starting 10
Segmentation fault
这是节目, 我调试了这个程序中的很多错误,最后它运行了,但不能排序,也不能排序。它从文件读取的两种类型的输入或随机数生成中获取输入

#include<stdio.h>
#include<time.h>
#include<values.h>
#include<malloc.h>
#include<stdlib.h>
#include<unistd.h>
int *a;
void swap(int i,int j)
{
    int temp;
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
}
int partition(int l,int r)
{
    int p,i,j;
    p=a[l];
    i=l;
    j=r+1;
    while(i<j)
    {   
        for(i=i+1;i<r&&a[i]<p;i++)
            for(j=j-1;j>l&&a[i]>p;j++)
                swap(i,j);
    }
    swap(i,j);
    swap(l,j);
    return j;
}
void quick(int l,int r)
{
    int s,i;
    if(l<r)
    {
        s=partition(l,r);
        //delay(1);
        quick(l,s-1);
        quick(s+1,r);
    }
}
void main()
{
    FILE *fp,*fp1;
    clock_t c1,c2;
    int n,i,j,l,r,datasize=1,ch,x,c;
    long int m;
    char file[10];
    do
    {
        printf("\n1> Read from file\n2> Random no. Generator\n\n");
        printf("\nEnter the Choice\n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: printf("\nEnter n value\n");
                scanf("%d",&n);
                a=(int*)calloc(n,sizeof(int));
                printf("Enter the filename\n");
                scanf("%s",file);
                fp1=fopen(file,"r");
                i=0;
                while(!feof(fp1))
                {
                    fscanf(fp1,"%d",&a[i]);
                    i++;
                }
                fclose(fp1);
                for(i=0;i<n;i++)
                    printf("%d\t",a[i]);
                quick(0,n-1);
                printf("\nSorted Elements are\n");
                for(i=0;i<n;i++)
                    printf("%d\t",a[i]);
                free(a);
                break;
            case 2: m=100;
                fp=fopen("new.dat","w");
                while(datasize<=10)
                {
                    printf("Starting %ld\n",m);
                    a=(int*)calloc(m,sizeof(int));
                    for(i=0;i<=m-1;i++)
                    {
                        a[i]=rand()%MAXINT;
                        printf("%d",a[i]);
                    }
                    c1=clock();
                    quick(0,m-1);
                    c2=clock();
                    free(a);
                    fprintf(fp,"%ld\t %ld\n",m,(c2-c1)/CLOCKS_PER_SEC);
                    datasize++;
                    m=m+100;
                }
                fclose(fp);
                break;
            default: break;
        }
        printf("To continue, Press 1 else other for Exit!");
        scanf("%d",&c);
    }
    while(c==1);
}
#包括
#包括
#包括
#包括
#包括
#包括
int*a;
无效交换(整数i,整数j)
{
内部温度;
温度=a[i];
a[i]=a[j];
a[j]=温度;
}
整数分区(整数l,整数r)
{
int p,i,j;
p=a[l];
i=l;
j=r+1;

然而(i您的递归并没有结束,因为您的分区代码工作不正常。下面是您的代码的一个经过检测和略微清理的版本(仍然有许多问题需要解决):

#包括
#包括
#包括
#包括
#定义最大值
静态int*a;
静态无效交换(int i,int j)
{
内部温度;
温度=a[i];
a[i]=a[j];
a[j]=温度;
}
静态整数分区(整数l,整数r)
{
printf(“-->>%s:(%d,%d)\n”、\uu函数、l、r);
int p,i,j;
p=a[l];
i=l;
j=r+1;
而(il&&a[i]>p;j++)
互换(i,j);
}
互换(i,j);
互换(l,j);
printf(“>%s:(%d,%d)\n”、\uu函数、l、r);
if(l>快速:(0,9)
-->>分区:(0,9)
>快速:(0,9)
-->>分区:(0,9)
>快速:(0,9)
-->>分区:(0,9)
>快速:(0,9)
-->>分区:(0,9)
>快速:(0,9)
-->>分区:(0,9)
>快速:(0,9)
-->>分区:(0,9)
>快速:(0,9)
-->>分区:(0,9)
>快速:(0,9)
-->>分区:(0,9)
>快速:(0,9)
-->>分区:(0,9)
>快速:(0,9)
分段错误:11

请注意我是如何调试的-在关键位置添加适当的打印语句。从来没有一行开始
你的问题就等同于“请为我调试我的代码”。我建议在一个简单的非工作示例中使用调试器逐步检查代码,或者使用类似
valgrind
的工具。这是明晚太平洋标准时间午夜的时候吗?;)分段错误通常会将您指向它发生的位置。您如何知道它是最后一个错误?您显示的输出与您显示的源不对应。当您输入
2
时,
m
的值设置为
100
,但您随后声称它打印为
10
。如果您不显示,我们将无法调试代码(另外,我个人的一个缺点是在没有使用
提供的额外设施时使用
——只使用
)我得到了它,并修改了分区函数。..int partition(int low,int high){int I,j,key,temp;key=a[low];i=low+1;j=high;while(1){while(ia[i])i++;while(a[j]>键)j--;if(i)
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAXINT INT_MAX

static int *a;

static void swap(int i, int j)
{
    int temp;
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}

static int partition(int l, int r)
{
    printf("-->> %s: (%d, %d)\n", __func__, l, r);
    int p, i, j;
    p = a[l];
    i = l;
    j = r + 1;
    while (i < j)
    {
        for (i = i + 1; i < r && a[i] < p; i++)
            for (j = j - 1; j > l && a[i] > p; j++)
                swap(i, j);
    }
    swap(i, j);
    swap(l, j);
    printf("<<-- %s: (%d, %d) => %d\n", __func__, l, r, j);
    return j;
}

static void quick(int l, int r)
{
    int s;
    printf("-->> %s: (%d, %d)\n", __func__, l, r);
    if (l < r)
    {
        s = partition(l, r);
        // delay(1);
        quick(l, s - 1);
        quick(s + 1, r);
    }
    printf("<<-- %s: (%d, %d)\n", __func__, l, r);
}

int main(void)
{
    FILE *fp, *fp1;
    clock_t c1, c2;
    int n, i, datasize = 1, ch, c;
    long int m;
    char file[10];
    do
    {
        printf("\n1> Read from file\n2> Random no. Generator\n\n");
        printf("\nEnter the Choice\n");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            printf("\nEnter n value\n");
            scanf("%d", &n);
            a = (int *)calloc(n, sizeof(int));
            printf("Enter the filename\n");
            scanf("%s", file);
            fp1 = fopen(file, "r");
            i = 0;
            while (!feof(fp1))
            {
                fscanf(fp1, "%d", &a[i]);
                i++;
            }
            fclose(fp1);
            for (i = 0; i < n; i++)
                printf("%d\t", a[i]);
            quick(0, n - 1);
            printf("\nSorted Elements are\n");
            for (i = 0; i < n; i++)
                printf("%d\t", a[i]);
            free(a);
            break;
        case 2:
            m = 10;
            fp = fopen("new.dat", "w");
            while (datasize <= 10)
            {
                enum { PER_LINE = 7 };
                printf("Starting %ld\n", m);
                a = (int *)calloc(m, sizeof(int));
                for (i = 0; i <= m - 1; i++)
                {
                    a[i] = rand() % MAXINT;
                    printf("%11d", a[i]);
                    if (i % PER_LINE == PER_LINE - 1)
                        putchar('\n');
                }
                if (i % PER_LINE != 0)
                    putchar('\n');
                printf("Sorting\n");
                c1 = clock();
                quick(0, m - 1);
                c2 = clock();
                printf("Sorted\n");
                free(a);
                fprintf(fp, "%ld\t %ld\n", m, (c2 - c1) / CLOCKS_PER_SEC);
                datasize++;
                m = m + 10;
            }
            fclose(fp);
            break;
        default:
            break;
        }
        printf("To continue, Press 1 else other for Exit!");
        scanf("%d", &c);
    } while (c == 1);
    return 0;
}
1> Read from file
2> Random no. Generator

Enter the Choice
Starting 10
      16807  282475249 1622650073  984943658 1144108930  470211272  101027544
 1457850878 1458777923 2007237709
Sorting
-->> quick: (0, 9)
-->> partition: (0, 9)
<<-- partition: (0, 9) => 10
-->> quick: (0, 9)
-->> partition: (0, 9)
<<-- partition: (0, 9) => 10
-->> quick: (0, 9)
-->> partition: (0, 9)
<<-- partition: (0, 9) => 10
-->> quick: (0, 9)
-->> partition: (0, 9)
<<-- partition: (0, 9) => 10
-->> quick: (0, 9)
-->> partition: (0, 9)
<<-- partition: (0, 9) => 10

…lots more of this…    

-->> quick: (0, 9)
-->> partition: (0, 9)
<<-- partition: (0, 9) => 10
-->> quick: (0, 9)
-->> partition: (0, 9)
<<-- partition: (0, 9) => 10
-->> quick: (0, 9)
-->> partition: (0, 9)
<<-- partition: (0, 9) => 10
-->> quick: (0, 9)
-->> partition: (0, 9)
<<-- partition: (0, 9) => 10
-->> quick: (0, 9)
Segmentation fault: 11