C 具有分段错误的快速排序程序
今天,我正在学习R.Sedgewick的C语言算法中的快速排序算法 我对算法的工作原理有很大的了解。编码部分让我有点困惑,最后我遇到了一个分段错误。代码如下:C 具有分段错误的快速排序程序,c,algorithm,quicksort,C,Algorithm,Quicksort,今天,我正在学习R.Sedgewick的C语言算法中的快速排序算法 我对算法的工作原理有很大的了解。编码部分让我有点困惑,最后我遇到了一个分段错误。代码如下: #include <stdio.h> void quicksort(int[], int, int); // prototype void quicksort(int a[], int l, int r) // What is the value of l. Why hasn't the author
#include <stdio.h>
void quicksort(int[], int, int); // prototype
void quicksort(int a[], int l, int r) // What is the value of l. Why hasn't the author
// mentioned it. Is it the size of the array?
{
int i, j, v, t;
if(r > l)
{
v = a[r];
i = l - 1; // What is l here? Is it the size if yes look at first while statement
j = r;
for(;;)
{
/*The algorithm says: scan from right until an element < a[r] is found. Where
r is the last position in the array. But while checking in the second while
loop elements > a[r] is searched */
while (a[++i] < v); // How can you increment again after reaching end of arrray
// size if l is the size of the array
while (a[--j] > v);
if(i >= j) break;
t = a[i]; a[i] = a[j]; a[j] = t;
}
}
t = a[i]; a[i] = a[r]; a[r] = t;
quicksort(a, l, i - 1);
quicksort(a, i + 1, r);
return;
}
int main()
{
int i, a[10]; // assuming size is 10
for(i = 0; i < 10; i++)
{
scanf("%d", &a[i]);
}
int l = 10; // I am passing size of the array
int r = 9; // position of last element
quicksort(a, l, r);
return 0;
}
这是调试器返回的结果:
Breakpoint 1, quicksort (a=0xbffff808, l=0, r=0) at quick.c:11
11 if(r > 1)
(gdb) c
Continuing.
Program received signal SIGSEGV, Segmentation fault.
0x080484fb in quicksort (a=0xbffff808, l=0, r=0) at quick.c:28
28 t = a[i]; a[i] = a[r]; a[r] = t;
(gdb) c
Continuing.
Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb) c
The program is not being run.
执行上述程序的正确方法是这样。左右指针没有任何内容。若数组占用n个内存位置,则左指针应指向第0个位置,右指针应指向n-1个位置。我犯了一个愚蠢的错误,没有在if条件中包含快速排序的递归函数。因此,所有的头痛。正确的程序是:
/* Working quicksort
* Robert sedgewick best
*/
#include <stdio.h>
void quicksort(int[], int, int); // prototype
void quicksort(int a[], int l, int r)
{
int i, j, v, t;
if(r > l)
{
v = a[r];
i = l - 1;
j = r;
for(;;)
{
while (a[++i] < v);
while (a[--j] > v);
if(i >= j) break;
t = a[i]; a[i] = a[j]; a[j] = t;
} // End for here
t = a[i]; a[i] = a[r]; a[r] = t;
quicksort(a, l, i - 1);
quicksort(a, i + 1, r);
} /* End if here. That is include the recursive
functions inside the if condition. Then it works
just fine. */
return;
}
int main()
{
int i, a[5]; // assuming size is 10
for(i = 0; i < 5; i++)
{
scanf("%d", &a[i]);
}
int l = 0; // I am passing size of the array
int r = 4; // position of last element
quicksort(a, l, r);
int s;
for(s = 0; s < 5; s++)
{
printf("%d ", a[s]);
}
return 0;
}
/*工作快速排序
*罗伯特·塞吉威克最佳
*/
#包括
无效快速排序(int[],int,int);//原型
无效快速排序(int a[],int l,int r)
{
int i,j,v,t;
如果(r>l)
{
v=a[r];
i=l-1;
j=r;
对于(;;)
{
而(a[++i]v);
如果(i>=j)断裂;
t=a[i];a[i]=a[j];a[j]=t;
}//到此为止
t=a[i];a[i]=a[r];a[r]=t;
快速排序(a、l、i-1);
快速排序(a、i+1、r);
}/*如果在此处结束。这包括递归
函数在if条件内。然后它工作
很好*/
返回;
}
int main()
{
int i,a[5];//假设大小为10
对于(i=0;i<5;i++)
{
scanf(“%d”、&a[i]);
}
int l=0;//我正在传递数组的大小
int r=4;//最后一个元素的位置
快速排序(a、l、r);
int-s;
对于(s=0;s<5;s++)
{
printf(“%d”,a[s]);
}
返回0;
}
l
和r
分别代表“左”和“右”
分段错误的发生是因为您正在传递l=10
,所以while(a[++i]中断
[编辑]
while (a[++i] < v);
while (a[--j] > v);
while(a[++i]v);
这两个循环也有问题:您需要测试i
和j
是否超出范围。l
和r
分别代表“左”和“右”
分段错误的发生是因为您正在传递l=10
,所以while(a[++i]中断
[编辑]
while (a[++i] < v);
while (a[--j] > v);
while(a[++i]v);
这两个循环也有问题:您需要测试i
和j
是否超出范围。请在诸如gdb
之类的调试器中运行。这将显示发生故障的确切线路。如果你在谷歌上搜索“gdb备忘表”,那就很容易开始了。请记住使用-g
标志编译。”
我的会议:
dan@dev1:~ $ gcc -g quick.c
dan@dev1:~ $ gdb a.out
...
(gdb) r
Starting program: /home/dan/a.out
1 4 8 2 3 6 4 7 10 9
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400572 in quicksort (a=0x7fffffffe530, l=10, r=9) at quick.c:21
21 while (a[++i] < v); // How can you increment again after reaching end of arrray
dan@dev1:~$gcc-g quick.c
dan@dev1:~$gdb a.out
...
(gdb)r
启动程序:/home/dan/a.out
1 4 8 2 3 6 4 7 10 9
程序接收信号SIGSEGV,分段故障。
快速排序时0x0000000000400572(a=0x7fffffffe530,l=10,r=9)。c:21
21 while(a[++i]
请在诸如gdb
之类的调试器中运行。这将向您显示发生segfault的准确线路。如果您搜索“gdb备忘单”,则很容易开始。请记住使用-g
标志编译
我的会议:
dan@dev1:~ $ gcc -g quick.c
dan@dev1:~ $ gdb a.out
...
(gdb) r
Starting program: /home/dan/a.out
1 4 8 2 3 6 4 7 10 9
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400572 in quicksort (a=0x7fffffffe530, l=10, r=9) at quick.c:21
21 while (a[++i] < v); // How can you increment again after reaching end of arrray
dan@dev1:~$gcc-g quick.c
dan@dev1:~$gdb a.out
...
(gdb)r
启动程序:/home/dan/a.out
1 4 8 2 3 6 4 7 10 9
程序接收信号SIGSEGV,分段故障。
快速排序时0x0000000000400572(a=0x7fffffffe530,l=10,r=9)。c:21
21而(a[++i]
inta[10];
int l=10;
int r=9;
快速排序(a、l、r);
称为快速排序(a、l、r)
//l=10,r=9
如果(r>1)//9>1为真
{
i=l-1;//i=10-1=9
对于(;;)
{
而(a[++i]
inta[10];
int l=10;
int r=9;
快速排序(a、l、r);
称为快速排序(a、l、r)
//l=10,r=9
如果(r>1)//9>1为真
{
i=l-1;//i=10-1=9
对于(;;)
{
而(a[++i]
l
和r
代表“左”和“右”请分别粘贴错误消息。当错误出现时,你真的不应该让人们在你的代码上翻来翻去,这应该有助于确定错误发生在代码中的位置。如果必要,我也会添加打印输出。如果数组中的元素数为10,左表示0,r表示9吗?@user2147954:是的,但还有其他问题。例如,如果r==1
,则t=a[i]…
将中断,因为i
的值未定义。i(可能是SO的C/C++论坛中的大多数其他人)拥有Sedgewicks的书,这个算法与他的算法并不完全相同。在几个不同的地方,abein指出了上面的问题。你可能想再检查一遍。l
和r
代表“左”和“右”请分别粘贴错误消息。当错误出现时,你真的不应该让人们在你的代码上翻来翻去,这应该有助于确定错误发生在代码中的位置。如果必要,我也会添加打印输出。如果数组中的元素数为10,左表示0,r表示9吗?@user2147954:是的,但还有其他问题。例如,如果r==1
,则t=a[i]…
将中断,因为i
的值未定义。i(可能是SO的C/C++论坛中的大多数其他人)我有Sedgewicks的书,这个算法不是他一字不差的。在几个不同的地方,abein指出了上面的问题。你可能想再次检查一下。好吧,假设我把0传递给l,也就是说:l=0或l=1