Question
The C(not cpp) function of quicksort met Segmentation fault (core dumped) . Please debug for me. You can change everything except the function prototype and
The C(not cpp) function of quicksort met Segmentation fault (core dumped). Please debug for me. You can change everything except the function prototype and the whole main(). input: n=no. of words output: you have to print each step of sorting
the photo explained my above text. The following codes is written by a Chegg expert, but his codes met segmentation fault in my compiler.
#include
#include
#include
#include
#include
int compare(char *A, char *B) {
// Your code here
return strcmp(A, B);
}
void swap(char **a, int x, int y) {
// Your code here
char * tmp = a[x];
a[x] = a[y];
a[y] = tmp;
}
int median3(char** a, int l, int r){
int c = (l + r) / 2;
// Your code here
if (compare(a[l], a[c]) > 0)
swap(a, l, c);
if (compare(a[l], a[r]) > 0)
swap(a, l, r);
if (compare(a[c], a[r]) > 0)
swap(a, c, r);
// we are sure that:
// a[l]
swap(a, c, r); // hide pivot
return l;
}
void my_qsort(char**a, int left, int right, int n, FILE *fout){
int i, j, p;
if (left >= right) return;
if (left + 1 == right){
if (compare(a[left], a[right]) > 0) {
swap(a, left, right);
}
return;
}
p = median3(a, left, right);
i = left;
j = right;
while (i
while (compare(a[i], a[p])
while (compare(a[j], a[p]) >= 0) j--;
if (i
swap(a, i, j);
}
}
swap(a, j, left);
for (i = 0; i
fprintf(fout, "%s ", a[i]);
fprintf(fout, "%s ", a[n-1]);
my_qsort(a, left, j-1, n, fout);
my_qsort(a, j+1, right, n, fout);
}
int main(int argc, char *argv[]) { FILE *fin, *fout; int n; char **data; int i;
fin = fopen(argv[1], "r"); fout = fopen(argv[2], "w"); fscanf(fin, "%d", &n); data = (char**) malloc(sizeof(char*)*n); printf("n = %d ", n); for (i = 0; i
//original codes
int compare(char *A, char *B) { // Your code here }
void swap(char **a, int x, int y) { // Your code here }
int median3(char** a, int l, int r){ int c = (l + r) / 2; // Your code here return l; }
void my_qsort(char**a, int left, int right, int n, FILE *fout){ int i, j, p; if (left >= right) return; if (left + 1 == right){ if (compare(/* What is missing here? */) > 0) { /* What is missing here? */ } return; } p = median3(/* What is missing here? */); i = left; j = right; while (1){ while (compare(/* What is missing here? */) = 0) j--; if (i
int main(int argc, char *argv[]) { FILE *fin, *fout; int n; char **data; int i;
fin = fopen(argv[1], "r"); fout = fopen(argv[2], "w"); fscanf(fin, "%d", &n); data = (char**) malloc(sizeof(char*)*n); printf("n = %d ", n); for (i = 0; i
//end of original code
Segmentation fault (core dumped) Sample Input: 4 Apple Orange Banana Pear Sample Output: Banana Apple Orange Pear Apple Banana Orange PearStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started