段异常(段违例)在 key = *(q+last);出现的
代码
#include <stdio.h>
#include <malloc.h>
int partition(char *s,int begin,int last);
void Quicksort(char *s1,int first,int end);
int main(){
char array[]={6,4,1,2,3};
Quicksort(array,0,4);
printf("the sorted array is :\n");
printf("%s\n",array);
system("pause");
}
void Quicksort(char *s,int first,int end){
int p;
if (first < end){
p = partition(s,first,end);
Quicksort(s,first,p-1);
Quicksort(s,p+1,end);
}
}
int partition(char *s,int begin,int last){
int i,j,p;
char key,value;
char *q;
q = (char*)malloc(sizeof(char)*(last - begin+1));
i = 0;
p= i -1;
for(i = begin;i < last;i++){
if (*(q+i) <= key){
p ++;
value = *(q+i);
*(q+i) = *(q+p);
*(q+p) = value;
}
}
*(q+last) = *(q+1+p);
*(q+1+p) = key;
for(j = begin;j <= last;j++)
*(s+j) = *(q+j);
}