Sort program in C language becomes Segmentation fault

Asked 1 years ago, Updated 1 years ago, 35 views

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

#define MAX_A10
#define MAX_B100

void quickSort(int(*E)[MAX_B]);
void q_sort(int*E, int left, int right);

int main(void){
  inti,j;
  int E[MAX_A][MAX_B];

  for(i=0;i<MAX_A;i++){
    for(j=0;j<MAX_B;j++){
      E[i][j]=land()%MAX_B;
    }
  }

  quickSort(E);
  for(i=0;i<MAX_A;i++){
    for(j=0;j<MAX_B;j++){
      printf("%d\t", E[i][j]);
    }
    printf("\n");
  }

  return 0;
}

void quickSort(int(*E)[MAX_B]){
  inti;
  for(i=0;i<MAX_B;i++){
    q_sort(E[i], 0, MAX_B-1);
  }
}

void q_sort(int*E, int left, int right) {
  int pivot, l_hold, r_hold;

  l_hold = left;
  r_hold = right;
  pivot = E[left];

  while(left<right) {
    while((E[right]>=pivot)&(left<right)){
      right--;
    }
    if(left!=right){
      E[left] = E[right];
      left++;
    }
    while((E[left]<=pivot)&(left<right)){ 
      left++;
    }
    if(left!=right){
      E[right] = E[left];
      right--;
    }
  }
  E[left] = pivot;
  pivot = left;
  left = l_hold;
  right = r_hold;
  if(left<pivot){
    q_sort(E,left,pivot-1);
  }
  if(right>pivot){
    q_sort(E,pivot+1,right);
  }
}

I wrote a program to sort in C language, but it turned out to be Segmentation fault.
What's wrong?

Sample Output:
1 2 3 5 5 6 7 9 0 0 2 3 6 6 7 9
0 1 2 3 5 7 8 9
One, two, two, three, six, seven, eight, nine. 0 1 3 4 4 5 7 899 0 0 1 2 3 3 6 6
3 4 5 5 6 7 7 9 0 2 3 4 4 4 4 5 7 1 3 4 6 7 8 8 8 9 0 2 4 6 6 8 9

c

2022-09-30 21:10

2 Answers

In quickSort(), i is looped to MAX_B, which should be up to MAX_A.


2022-09-30 21:10

When you are mossing, you see on which line you are mossing.

If you don't have a Core Dump when it falls, try to do Core Dump as ulimit-cunlimited.

Also, since qsort is in C's standard library, it is usually not self-made.


2022-09-30 21:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.