# 75. Sort Colors

![](/files/-Miaq8i745SrfHwq5ReW)

![](/files/-MiaqCQMlbX-nxkU3BW4)

### use bucket sort

T: O(n)

S: O(1)

```java
class Solution {
    public void sortColors(int[] nums) {
        int[] bucket = new int[3];
        for (int num : nums) {
            bucket[num]++;
        }
        int idx = 0;
        for (int i = 0; i < nums.length; i++) {
            while (idx < 3 && bucket[idx] == 0) {
                idx++;
            }
            nums[i] = idx;
            bucket[idx]--;
        }
    }
}
```

### use pivot partition, run 2 times partition

time: O(n)

space: O(1)

```java
class Solution {
    public void sortColors(int[] nums) {
        // write your code here
        
        // put 0 on 1's left
        int start = partition(nums, 0, 1);
        
        // then put 1 on 2 left (begin with 0's end)
        partition(nums, start, 2);
    }

    private int partition(int[] nums, int start, int pivot) {

        int left = start;
        int right = nums.length - 1;

        while (left <= right) {
            while (left <= right && nums[left] < pivot) {
                left++;
            }
            while (left <= right && nums[right] >= pivot) { // equal, 所以最後 left, right 相交, 中間沒東西
                right--;
            }
            if (left <= right) {
                swap(nums, left, right);
                left++;
                right--;
            }
        }
        return left;
    }
    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
```

The problem is known as [Dutch National Flag Problem](https://en.wikipedia.org/wiki/Dutch_national_flag_problem) and first was proposed by [Edsger W. Dijkstra](https://en.wikipedia.org/wiki/Edsger_W._Dijkstra). The idea is to attribute a color to each number and then arrange them following the order of colors on the Dutch flag.

T: O(n)

S: O(1)

```java
class Solution {
    public void sortColors(int[] nums) {
        int p0 = 0;
        int cur = 0;
        int p2 = nums.length-1;

        while (cur <= p2) {
            if (nums[cur] == 0) {
                int temp = nums[p0];
                nums[p0++] = nums[cur];
                nums[cur++] = temp;
            } else if (nums[cur] == 2) {
                int temp = nums[p2];
                nums[p2--] = nums[cur];
                nums[cur] = temp;
            } else {
                cur++;
            }
        }
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/kth/75.-sort-colors.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
