#include <iostream>
#include <stack>
using namespace std;
int partition(int* arr, int left, int right)
{
int key = arr[left];
int i = left, j = right;
while (1)
{
while ((i<j)&&(arr[j] >= key)) {
j--;
}
while ((i<j)&&(arr[i] <= key)) {
i++;
}
if (i < j)
swap(arr[i], arr[j]);
else break;
}
swap(arr[i], arr[left]);
return i;
}
void quickSort1(int* arr, int left, int right)
{
if (left<right)
{
int p = partition(arr, left, right);//分界点
quickSort1(arr, left, p - 1);
quickSort1(arr, p + 1, right);
}
}
void quickSort2(int* arr, int left, int right)
{
stack<int> t;
if (left<right)
{
int p = partition(arr, left, right);
if (p - 1>left)
{
t.push(left);
t.push(p - 1);
}
if (p + 1<right)
{
t.push(p + 1);
t.push(right);
}
while (!t.empty())
{
int r = t.top();
t.pop();
int l = t.top();
t.pop();
p = partition(arr, l, r);
if (p - 1>l)
{
t.push(l);
t.push(p - 1);
}
if (p + 1<r)
{
t.push(p + 1);
t.push(r);
}
}
}
}
int main()
{
int n = 5;
int arr[5] = { 8, 3, 6, 4, 9 };
//int* arr;
//cin >> n;
//arr = new int[n];
//for (int i = 0; i < n; i++){
// cin>>arr[i];
//}
//function(arr, 0, 4);
stack<int> t;
int index = arr[0];
int i = 0;
int j = n - 1;
quickSort1(arr, i, j);
for (int i = 0; i < n; i++){
cout << arr[i] << " ";
}
cout << endl;
return 0;
}