Problem Description: The existing n red and white blue three different colors of the ball, disorderly order together, please exchange any two balls by 22, so that the ball from left to right is red ball, spin placed struck, blue ball in turn. The problem is called the Dutch flag because the red, white and blue balls are made into strips and arranged in order to form the Dutch flag.
Problem Solving Method 1: Pretty trying to solve
Solution 2: In order to discuss the convenience of the number 0 for the red ball, the number 1 for the white Ball, the number 2 for the blue Ball, so the final sort is 0 ... 1...2 ...
The quick sort is based on the partitioning process, selecting the entire array between the main elements into two sub-arrays. Is it possible to use the partitioning process to set three pointers to complete the re-arrangement of a traverse, so that all the balls are arranged into three different color balls?
(1) Set three pointers: a front pointer to begin, a pointer current, and a back pointer.
The current pointer traverses the entire array sequence
(2) When the current pointer refers to an element that is 0 o'clock, it is exchanged with the element referred to by the Begin pointer (except that the interchange element does not exchange the pointer position ), and then current++,begin++
(3) When the current pointer refers to the element is 1 o'clock, do not do any exchange (that is, do not move the ball), and then current++
(4) When the current pointer refers to the element is 2 o'clock, and the end pointer refers to the element is exchanged (the same straight swap element does not exchange the pointer position), and then the current pointer position does not move, end--
Pending completion ...
Netherlands flag Flag of the Kingdom of the Netherlands