leetcode sort quick sort

THE

problem

solution

Code


class Solution {
    
    
public:
   vector<int> sortArray(vector<int>& nums) {
    
    
       int n=nums.size();
       quicksort(nums, 0, n-1);
       return nums;
   }

   void quicksort(vector<int>& q, int l, int r) {
    
    
       if (l >= r) return;

       int x = q[(l+r)/2], i = l - 1, j = r + 1; 
       while (i < j) {
    
    
           do i ++ ; while (q[i] < x);
           do j -- ; while (q[j] > x);
           if (i < j) swap(q[i], q[j]);
       }

       quicksort(q, l, j);
       quicksort(q, j + 1, r);
   }
};




Summary and reflection

  1. A lot of fast queue thinking feels unclear, here I chose the one that I understand best.

Guess you like

Origin blog.csdn.net/liupeng19970119/article/details/114223791