# Sort Colors Ii

## Sort Colors Ii

## 题目地址

<https://www.lintcode.com/problem/sort-colors-ii>

## 题目描述

```
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k.
```

## 代码

```java
public class Solution {
    public void sortColors2(int[] colors, int k) {
    if (colors == null || colors.length == 0) return;

    rainbowSort(colors, 0, colors.length - 1, 1, k);
  }

  public void rainbowSort(int[] colrs,
                         int left,
                         int right,
                         int colorFrom,
                         int colorTo) {
    if (colorFrom == colorTo) return;
    if (left >= right) return;

    int colorMid = (colorFrom + colorTo) / 2;
    int l = left, r = right;
    while (l <= r) {
      while (l <= r && colors[l] <= colorMid) {
        l++;
      }
      while (l <= r && colors[r] > colorMid) {
        r--;
      }
      if (l <= r) {
        int temp = colors[l];
        colors[l] = colors[r];
        colors[r] = temp;
        l++;
        r--;
      }
    }

    rainbowSort(colors, left, r, colorFrom, colorMid);
    rainbowSort(colors, l, right, colorMid + 1, colorTo);
  }
}
```


---

# 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://wentao-shao.gitbook.io/leetcode/two-pointers/sort-colors-ii.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.
