> For the complete documentation index, see [llms.txt](https://shangan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://shangan.gitbook.io/algorithm/he-xin-suan-fa-200-ti/sort-algorithms/partition-array.md).

# Partition Array

{% tabs %}
{% tab title="Java" %}

```java
public class Solution {
    /**
     * @param nums: The integer array you should partition
     * @param k: An integer
     * @return: The index after partition
     */
		public int partitionArray(int[] nums, int k) {
			if (nums == null || nums.length == 0) {
				return 0;
			}
			int i = 0;
			int j = nums.length;
			while (i <= j) {
				while (i <= j && nums[i] < k) {
			i++;
		}
		while (i <= j && nums[j] >= k) {
			j--;
		}
		
		if (i <= j) {
			int t = nums[i];
			nums[i++] = nums[j];
			nums[j--] = t;
			}
		}
			return i; //while循环没有进行的情况下是i在j的右边，循环停止
	}
}

```

{% endtab %}
{% endtabs %}
