Sort Colors
Given an array withn_objects colored_red,white_or_blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers0
,1
, and2
to represent the color red, white, and blue respectively.
Notice
You are not suppose to use the library's sort function for this problem. You should do it in-place (sort numbers in the original array).
Example
Given[1, 0, 1, 2]
, sort it in-place to[0, 1, 1, 2]
.
分析:
快速排序, partition出一个Index,然后左右各自递归排序
答案;
Last updated