Sort Colors

Try to solve the Sort Colors problem.


Given an array, colors, which contains a combination of the following three elements:

  • 00 (representing red)

  • 11 (representing white)

  • 22 (representing blue)

Sort the array in place so that the elements of the same color are adjacent, with the colors in the order of red, white, and blue. To improve your problem-solving skills, do not utilize the built-in sort function.


  • 11 \leq colors.length \leq 300
  • colors[i] can only contain 00s, 11s, or 22s.


Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy