您的位置:首页 > 理论基础 > 数据结构算法

不使用递归实现的快速排序

2014-10-30 00:45 218 查看
#include<iostream>
#include<queue>

using namespace std;

void QSort(int* array, int count) {
queue<int> First;
queue<int> Last;
First.push(0);
Last.push(count - 1);
while(!First.empty()) {
int first = First.front();
int last = Last.front();
First.pop(); Last.pop();
int ctrl = array[first];
int curr = 0;
int i = first, j = last;
while(i < j) {
if(curr == 0) {
while(array[j] > ctrl && j != i) j--;
array[i] = array[j];
curr = 1;
} else {
while(array[i] <= ctrl && j != i) i++;
array[j] = array[i];
curr = 0;
}
}
array[i] = ctrl;
if(first < i - 1) {
First.push(first);
Last.push(i - 1);
}
if(last > i + 1) {
First.push(i + 1);
Last.push(last);
}
}
}

int main() {
int test[10] = { 4,1,7,9,3,2,6,5,0,8 };
QSort(test, 10);
for(int i = 0; i < 10; i++)
cout << test[i] << " ";
cout << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息