简单的快速排序函数,代码不长,求大神看下。我就是想问,这段代码是正常的,但是如果去掉if判断就会出现段错误,为什么啊,main的传参已经很明确了left小于right,为什么还非要加个判断呢?不加判断时的段错误是是吗原因造成的呢?
void quick_sort(int *a, int left, int right)
{
if(left < right)
{
int i = left;
int j = right;
int key = a[left];
while(i < j)
{
while(a[j] > key && i < j)
{
j--;
}
if(i < j)
a[i++] = a[j];
while(a[i] < key && i < j)
{
i++;
}
if(i < j)
a[j--] = a[i];
}
a[i] = key;
quick_sort(a, left, i - 1);
quick_sort(a, i + 1, right);
}
}
int main(int argc, char const *argv[])
{
int a[] = {63,54,1,76,44,77,90,43,3,7,8};
int len = sizeof(a)/sizeof(int);
int i;
quick_sort(a, 0, len);
for(i = 0; i < len; i++)
{
printf("%d ", a[i]);
}
return 0;
}
|