2014-05-24 2 views
-1

C의 빠른 정렬 프로그램은 오류없이 컴파일됩니다. 하지만 실행하고 정렬 할 난수를 선택하십시오. 다음과 같이 나는임의 번호를 사용하려고하면 QuickSort.C 세그먼트 화 오류가 발생합니다. 발전기

[email protected] ~ $ gcc quick.c 
[email protected] ~ $ ./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); 
} 
+2

귀하의 질문은 "제 코드를 제발 디버깅 해주세요"와 같습니다. 디버거를 사용하여 간단한 비 작동 예제에서 코드를 단계별로 실행하거나'valgrind'와 같은 도구를 사용하는 것이 좋습니다. – NPE

+1

는 내일 밤 자정 PST에 만기가 끝날 예정입니까? ;) – Andbdrew

+0

세그멘테이션 폴트는 일반적으로 발생하는 위치를 알려줍니다. – Leeor

답변

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 

주 - 추가 :

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

출력은 시작 : 여기가 계측 및 소폭 코드의 버전을 청소 것 (이 수정되어야 많은 일들이 여전히) 주요 장소에서 적절한 인쇄 문. <<-- quick:을 시작하는 행이 없으므로 빠른 정렬 알고리즘이 결코 반환되지 않습니다.

+0

나는 그것을 얻고, 분할 기능을 변경했다. int 파티션 (int low, int high) { \t int i, j, key, temp; \t 키 = a [낮음]; \t i = 로우 + 1; \tj = 하이; \t 동안 (1) { \t \t \t 동안 (I A [I]) \t \t \t I ++; \t \t (a [j]> 키) \t \t \tj--; \t \t \t 경우 (전 J를 <) \t \t \t \t { \t 온도 = A [I]; \t \t \t a [i] = a [j]; \t \t \t a [j] = 임시; 다른 \t \t \t \t} \t \t \t \t { \t 온도 = A [저]; \t \t \t a [낮음] = a [j]; \t \t \t a [j] = 임시; \t \t \t return j; \t \t \t} } 이전에 사용한 파티션 기능에 오류가있었습니다. 하지만 이제는 고칠 수있었습니다. 나를 도와 주신 모든 분들께 감사드립니다. –

관련 문제