The topic of fast-line thinking (I.)--The Dutch flag issue

Source: Internet
Author: User

The topic of fast-line thinking (I.)--The Dutch flag issue


The implementation of the fast-line we all know that the main thing is a partition and exchange process. This idea is actually very ingenious, based on this, a lot of topics can be used to solve it well. This article we talked about the flag of the Netherlands, is that you can use the idea of the fast line-follow up and a series of topics, should be used in the fast-line thinking, the back slowly finishing ing~


1. Title Description


This topic is proposed by the Dutch scientist Dijkstra, first input disorderly order of three-color ball (red, white, blue), if through 22 exchange, so that all the red ball row in front, white ball in the middle, blue ball in the last ~



2. Illustrations to answer


Using the idea of a fast line, we first use 3 pointers head, middle, tail. Initially, head and middle point to the first ball, and tail points to the last ball. Then the rules for moving and swapping are as follows:

(0 = red, 1 for white, 2 for blue)


    • Middle pointer refers to the element is 0 o'clock, and the head pointer refers to the element Exchange, and then Middle+ +,head+ +;
    • Middle pointer refers to the element is 1 o'clock, do not do any exchange (that is, the ball does not move), and then Middle+ +;
    • The Middle pointer refers to an element that is 2 o'clock, exchanged with the element pointed to by the tail pointer, and then themiddle pointer does not move,tail--。
until Current>tail, stop moving.


In fact, we can understand: when middle points to 0, this 0 should be in the front, so we need to switch it and head exchange, after the natural head++,middle++, because at this time has ensured that the head of the front is 0; back when middle points to 2 , and the above reasons, we need to exchange with the tail, after the exchange as long as the tail--, then middle do not move, because the value after the exchange we do not know the situation, we need further judgment.


The moving diagram is as follows:






The code is as follows:

#include <iostream>using namespace std; void Dutchflag (int *a, int head, int middle, int tail) {while (Middle<=tail) {if (a[middle]==0) {swap (A[middle], A[head]) ; head++, middle++; }if (a[middle]==1) middle++; if (a[middle]==2) {swap (A[middle], a[tail]); tail--;}}} int main () {int a[10]={0, 1, 2, 1, 1, 2, 0, 2, 1, 0}; int num=10; for (int i=0; i<num; i++) cout<<a[i]<< "; c out<<endl; int head=0; int middle=0; int tail=num-1; Dutchflag (A, head, middle, tail); for (int j=0; j<num; j + +) cout<<a[j]<< "; return 0; }







The topic of fast-line thinking (I.)--The Dutch flag issue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.