1、非递归快排
首先建立结构体存储两个下标
struct Node{
int first;
int last;
};
void Quicksort(int first, int last, int a[]){
int key = a[first];
int i = first;
int j = last;
int temp;
struct Node stack[100];
int top = 0;
stack[top].first = first;
stack[top].last = last;
while (top>-1) //栈不为空时
{
i = first = stack[top].first;
j = last = stack[top].last;
top--;
key = a[first];
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 (first < i - 1){
top++;
stack[top].first = first;
stack[top].last = i - 1;
}
if (last>i + 1){
top++;
stack[top].first = i + 1;
stack[top].last = last;
}
}
}