public int partition(int[] nums, int begin, int end) {
        int pivot = nums[begin];
        int l = begin + 1, r = end;
        while (l <= r) {
//            if (nums[l] > pivot && nums[r] < pivot) {
//                swap(nums, l++, r--);
//            }
//            if (nums[l] <= pivot)
//                ++l;
//            if (nums[r] >= pivot)
//                --r;
        	while (l <= r && nums[l] <= pivot)
        		++l;
        	while (l <= r && nums[r] >= pivot)
        		--r;
            if (l <= r && nums[l] > pivot && nums[r] < pivot)
                swap(nums,l++, r--);
        }
        //put pivot to its final position
        swap(nums, begin, r);
        return r;
    }