Question: Please answer in C language(not C++/cpp nor C#). This program is quicksort. n = no. of array, the output will print out each step of
Please answer in C language(not C++/cpp nor C#).
This program is quicksort. n = no. of array, the output will print out each step of sort.

For the code in void my_qsort(char**a, int left, int right, int n, FILE *fout), while (compare(a[i],p)
while (compare(a[j],p) >= 0) j--;
above two lines I know they are wrong as p is integer but I must use the while (compare(xxxxxxx)
//while (j > 0 && a[--j] >= p); given in the example program, a [ ] is int* type instead of char** type in the example program, so when I copy it to my program,, it seems impossible for me to use compare() to do the same effect, but I have to, so how can I do that? note that most of my program you cannot change e.g. whole main(). You also cannot change all my function prototype e.g. int compare(char *A, char *B) and the return value, but you can change the codes under the line // Your code here. You can take the original code as reference. //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
//end of original code
my codes:
#include
int compare(char *A, char *B) { // Your code here if (strcmp(A,B)>0){ return 1; } return 0; }
void swap(char **a, int x, int y) { // Your code here char * tmp = malloc (sizeof(char)*100); 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 (a[l] > a[c]) swap(a,l,c); if (a[l] > a[r]) swap(a,l,r); if (a[c] > a[r]) swap(a,c,r); // we are sure that: // a[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 (1){ while (compare(a[i],p) //while (i while (compare(a[j],p) >= 0) j--; //while (j > 0 && a[--j] >= p); 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 Sample Input: 4 Apple Orange Banana Pear Sample Output: Banana Apple Orange Pear Apple Banana Orange Pear
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
