1. with target outside the array
public int partitionArray(int[] nums, int k) { if (nums == null || nums.length == 0) { return 0; } int left = 0; int right = nums.length - 1; while (left <= right) { while (left <= right && nums[left] < k) { left++; } while (left <= right && nums[right] >= k) { right--; } if (left < right) { swap(nums, left, right); left++; right--; } } return left; } public void swap(int[] nums, int a, int b) { int tmp = nums[a]; nums[a] = nums[b]; nums[b] = tmp; }
2. with target with in the array(Quick select)