My java solution2


  • 0
    C
    public class Solution {
    public void sortColors(int[] nums) {
        quicksort(nums,0,nums.length-1);
    }
    
    public void quicksort(int [] A,int p,int r)
    {   if(p<r){
        int q = partition(A, p ,r);
        quicksort(A,0,q-1);
        quicksort(A,q+1,r);
        }
    }
    
    public int partition(int [] A,int p,int r)
    {   int i=p;
        int j=p-1;
        for(i=p;i<r;i++)
        {
            if(A[i]<=A[r])
            {   int tmp = A[++j];
                A[j] = A[i];
                A[i] = tmp;
            }
        }
        int tmp = A[++j];
        A[j]= A[r];
        A[r] = tmp;
        return j;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.