这段非递归的快速排序代码哪里错了?

paschen 版主 2012-05-11 05:12:16
写了个非递归的快速排序,建了个栈,没用模板库的,但下面主函数里第二种情况运行时会出错,在VS2010上编译,
错误是:HEAP CORRUPTION DETECTED:after Normal block(#88) at 0x00244E70
单步了好几次都没有发现,请高手帮看,要怎么修改??


//划分函数
template<class T>
int Partition(T* a,int left,int right)
{
T pivot = *(a+right);
while(left<right)
{
while(*(a+left)<pivot && left<right)
left++;
if(left<right)
*(a+right--) = *(a+left);
while(*(a+right)>pivot && left<right)
right--;
if(left<right)
*(a+left++) = *(a+right);
}
*(a+left) = pivot;
return left;
}

//快速排序(非递归)
template<class T>
void sort(T* arr,int len)
{
if(len == 1)
return;
int left = 0;
int right = len - 1;
int *stack = new int[right-left+1];
int top = 0;
int mid;
stack[top++] = left;
stack[top++] = right;
while(top>0)
{
right = stack[--top];
left = stack[--top];
if(left<right)
{
mid = Partition(arr,left,right);
if(mid-left > right-mid)
{
stack[top++] = left;
stack[top++] = mid-1;
if (right > mid)
{
stack[top++] = mid + 1;
stack[top++] = right;
}
}
else
{
stack[top++] = mid + 1;
stack[top++] = right;
if(mid > left)
{
stack[top++] = left;
stack[top++] = mid - 1;
}
}
}
}
delete[] stack;
}

int _tmain(int argc, _TCHAR* argv[])
{
//下面这个可以通过
int arr[] = {4,3,2};
sort(arr,3);
//print arr...


//这个不能通过
int arr2[] = {2,2,2};
sort(arr2,3);
//....

return 0;
}
...全文
89 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 版主 2012-05-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

仅供参考C/C++ code
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
int xs[10000];
int ys[10000];
int i=0,xx,yy;
int fc,bc;
void push(int x,int y) {
xs[i]=x;
ys[i]……
[/Quote]


什么意思??
赵4老师 2012-05-11
  • 打赏
  • 举报
回复
仅供参考
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
int xs[10000];
int ys[10000];
int i=0,xx,yy;
int fc,bc;
void push(int x,int y) {
xs[i]=x;
ys[i]=y;
if (i<10000-1) {
i=i+1;
} else {
printf("stack overflow!\n");
exit(1);
}
}
void pop(void) {
i=i-1;
xx=xs[i];
yy=ys[i];
}
int check(int x,int y) {
int c;

c=getpixel(x,y); /* 获取当前点的颜色 */
return ((c!=bc)&&(c!=fc));/* 如果颜色为边界色或填充色则不填充 */
}
void seedfilling(int x,int y,int fill_color,int boundary_color) {
fc=fill_color;
bc=boundary_color;
push(x,y);
while (1) {
if (i<=0) return;
pop();
if (check(xx,yy)) {
putpixel(xx, yy, 14);getch(); /* 加上这行显示当前填充状态 */

putpixel(xx, yy, fill_color); /* 画点 */

if (check(xx-1,yy )) push(xx-1,yy );
if (check(xx ,yy+1)) push(xx ,yy+1);
if (check(xx ,yy-1)) push(xx ,yy-1);
if (check(xx+1,yy )) push(xx+1,yy );
/* 去掉下面四句就是四连通 */
if (check(xx-1,yy-1)) push(xx-1,yy-1);
if (check(xx-1,yy+1)) push(xx-1,yy+1);
if (check(xx+1,yy-1)) push(xx+1,yy-1);
if (check(xx+1,yy+1)) push(xx+1,yy+1);
}
}
}
void main() {
int a,b,color;
int gdriver = DETECT, gmode, errorcode;
int poly[10];

initgraph(&gdriver, &gmode, "d:\\bc\\bgi");
a=150;
b=140;
color=4;
poly[0]=110;/* 第一个点的x坐标以及y坐标 */
poly[1]=110;
poly[2]=200;/* 第二点 */
poly[3]=105;
poly[4]=170;/* 第三点 */
poly[5]=120;
poly[6]=150;/* 第四点 */
poly[7]=170;
poly[8]=110;/* 多边形的起点与终点一样 */
poly[9]=110;
drawpoly(5,poly);/* 显示各点连接起来的多边形 */

/* 保证边界对八连通是封闭的 */
setviewport(0,1,600,300,0);
drawpoly(5,poly);/* 显示各点连接起来的多边形 */
setviewport(1,0,600,300,0);
drawpoly(5,poly);/* 显示各点连接起来的多边形 */
setviewport(1,1,600,300,0);
drawpoly(5,poly);/* 显示各点连接起来的多边形 */

/* 恢复默认viewport */
setviewport(0,0,600,300,0);

seedfilling(a,b,color,15); /* 种子填充多边形 */

getch();
closegraph();
}
paschen 版主 2012-05-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

楼主的stack开小了,在第二例里面,最多存放了4个吧,但是好像你只开辟了容量为3的数组,导致写入的位置错错了。所以delete的时候crash掉了
[/Quote]

上面代码参考于http://www.oschina.net/code/snippet_54100_8734
分配的时候多分配一个大小就可以从根本上解决吗??
W170532934 2012-05-11
  • 打赏
  • 举报
回复
楼主的stack开小了,在第二例里面,最多存放了4个吧,但是好像你只开辟了容量为3的数组,导致写入的位置错错了。所以delete的时候crash掉了
赵4老师 2012-05-11
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧