段异常(段违例)在 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);
}
各位大神帮忙看看。。。
[解决办法]
简直就是乱七八糟啊,
你的partition函数和s数组有毛关系?
[解决办法]
先
http://www.microsoft.com/visualstudio/chs/downloads#d-2010-express
点开Visual C++ 2010 Express下面的语言选‘简体中文’,再点立即安装
再参考C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\qsort.c
[解决办法]
30 i = 0;
31 p= i -1;
37 *(q+p) = value;
p始终是-1,这样(q+p) == q-1
于是就引用了不属于自己的内存范围,出现segmentation violation。