4/10/2014

Leetcode -- Sort Colors

 class Solution {  
 public:  
   void sortColors(int A[], int n) {  
     quickSort(A, n, 0, n - 1);  
   }  
   void quickSort(int A[], int n, int start, int end) {  
     if(start >= end) {  
       return;  
     }  
     int pivot = start;  
     int i = start + 1, j = start + 1;  
     for(; i <= end; ++i) {  
       if(A[i] <= A[pivot]) {  
         int temp = A[i];  
         A[i] = A[j];  
         A[j] = temp;  
         j++;  
       }  
     }  
     int temp = A[pivot];  
     A[pivot] = A[j - 1];  
     A[j - 1] = temp;  
     quickSort(A, n,start, j - 2);  
     quickSort(A, n, j, end);  
   }  
 };  

No comments: