C字符串的快速排序
我正在尝试制作一个对c字符串而不是int进行排序的快速排序。当我调用quickSort时,数组中的所有字符串都是空的和未排序的,我不知道为什么 “好的,好的。”C字符串的快速排序,c,string,quicksort,C,String,Quicksort,我正在尝试制作一个对c字符串而不是int进行排序的快速排序。当我调用quickSort时,数组中的所有字符串都是空的和未排序的,我不知道为什么 “好的,好的。” void交换(字符**A,整数i,整数j){ //printf(“交换”); char*temp=malloc(strlen(A[i])*sizeof(char)); 温度=A[i]; 自由(A[i]); A[i]=malloc((1+strlen(A[j])*sizeof(char)); A[i]=A[j]; 自由(A[j]); A[
void交换(字符**A,整数i,整数j){
//printf(“交换”);
char*temp=malloc(strlen(A[i])*sizeof(char));
温度=A[i];
自由(A[i]);
A[i]=malloc((1+strlen(A[j])*sizeof(char));
A[i]=A[j];
自由(A[j]);
A[j]=malloc((1+strlen(temp))*sizeof(char));
A[j]=温度;
}
整数分区(字符**A、整数p、整数r){
//printf(“partion\n”);
int i,j;
char*x;
//printf(“x\n”);
x=A[r];
i=p-1;
对于(j=p;jIs这是一个赋值还是什么?否则使用qsort()
。swap()
函数是错误的。你到底为什么要使用malloc()
in it?然后立即将新值分配给持有指向已分配内存指针的变量,导致大量内存泄漏,并在释放错误后使用…谢谢,TWA是导致问题的交换函数
void swap(char** A, int i, int j){
//printf("swap\n");
char* temp=malloc(strlen(A[i])*sizeof(char));
temp = A[i];
free(A[i]);
A[i]=malloc((1+strlen(A[j])) * sizeof(char));
A[i] = A[j];
free(A[j]);
A[j] = malloc((1+strlen(temp)) * sizeof(char));
A[j]=temp;
}
int Partition(char** A, int p, int r){
//printf("partion\n");
int i, j;
char* x;
//printf("x\n");
x = A[r];
i = p-1;
for(j=p; j<r; j++){
if( strcmp(A[j], x) < 0){
i++;
swap(A, i, j);
}
}
swap(A, i+1, r);
return(i+1);
}
void QuickSort(char** A, int p, int r){
int q;
if( p<r ){
q = Partition(A, p, r);
QuickSort(A, p, q-1);
QuickSort(A, q+1, r);
}
}