这里借鉴了二叉树先序遍历的非递归算法。
<span style="font-size:14px;">typedef struct Ele {
Ele(int start_ = 0, int end_ = 0) : start(start_), end(end_) {}
int start;
int end;
} Ele;
int quickSort(int *list, int start, int end) {
if(start >= end) {
return - 1;
}
int temp = list[start];
int i = start;
int j = end;
while(i < j) {
while(i < j && list[j] >= temp) {
j --;
}
if(i < j) {
list[i] = list[j];
i ++;
}
while(i < j && list[i] <= temp) {
i ++;
}
if(i < j) {
list[j] = list[i];
j --;
}
}
list[i] = temp;
return i;
}
void sort(int *list, int len) {
stack<Ele> sta;
sta.push(Ele(0, len - 1));
int mid;
Ele ele;
while(sta.size() > 0) {
ele = sta.top();
sta.pop();
if((mid = quickSort(list, ele.start, ele.end)) >= 0) {
sta.push(Ele(mid + 1, ele.end));
sta.push(Ele(ele.start, mid - 1));
}
}
}
int main(void) {
int str[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
sort(str, 10);
for(int i = 0; i < 10; i ++) {
cout << str[i] << " ";
}
cout << endl;
return 0;
}</span>