#include <iostream> #include <stack> #include <vector> #include <algorithm> using namespace std; int sort(vector<int>& nums, int l, int r) { int ll = l; for(int i = l+1; i < r; i++) { if (nums[l] > nums[i]) { ll++; swap(nums[ll], nums[i]); } } swap(nums[l], nums[ll]); return ll; } void qsort(vector<int>& nums, int l, int r) { if(l >= r) return; int mid = sort(nums, l, r); qsort(nums, l ,mid); qsort(nums, mid+1, r); } void qsort1(vector<int>& nums, int l, int r) { stack<int> st; if(l >= r) return; st.push(r); st.push(l); while(!st.empty()) { int l1 = st.top(); st.pop(); int r1 = st.top(); cout<<l1<<" "<<r1<<endl; st.pop(); if(l1 < r1) { int mid =sort(nums, l1, r1); st.push(mid); st.push(l1); st.push(r1); st.push(mid+1); cout << "mid "<< mid; cout<<" l "<<l; cout<<" r "<<r; cout<<" mid+1 "<< mid+1; } } } int main() { vector<int> nums = {9,8,7,65,4,2,3,1}; qsort1(nums, 0, nums.size()); for(auto v : nums) { cout<<v<<" "; } cout<<endl; }