这个非递归快速排序有一步没看懂,求教。
下面是从网上下载的程序,有一步没看懂;
#include "stdafx.h"
#include "stdio.h"
struct node
{
int min;
int max;
};
void fun(int min,int max,int a[])
{
int key = a[min];
int i = min;
int j = max;
int temp;
struct node Stack[100];
int top = 0;
Stack[top].min = min;
Stack[top].max = max;
while(top>-1)
{
i = min = Stack[top].min;
j = max = Stack[top].max;
top--;
key = a[min];
while(i<j)
{
while((i<j) && (key <= a[j]))
{j--;}
if(i < j)
{
temp = a[i];a[i] = a[j];a[j] = temp;
i++;
}
while((i<j) && (key >= a[i]))
{i++;}
if(i < j)
{
temp = a[i];a[i] = a[j];a[j] = temp;
j--;
}
}
if(min < i-1)
{
top++;
Stack[top].min = min;
Stack[top].max = i-1;
}
if(max>i+1)
{
top++;
Stack[top].min = i+1;
Stack[top].max = max;
}
}
}
int main(void)
{
int i;
int a[10] = {49,38,65,97,76,13,27,9,2,1};
for(i=0;i<10;i++)
printf(" %d ",a[i]);
printf("\n");
fun(0,9,a);
for(i=0;i<10;i++)
printf(" %d ",a[i]);
printf("\n");
return 0;
}
if(min < i-1)
{
top++;
Stack[top].min = min;
Stack[top].max = i-1;
}
if(max>i+1)
{
top++;
Stack[top].min = i+1;
Stack[top].max = max;
}
i = min = Stack[top].min;
j = max = Stack[top].max;
单步调试和设断点调试是程序员必须掌握的技能之一。
[解决办法]
看看数据结构先!理解理解栈