Question Based on the idea of fast sorting (I) -- Dutch flag

Source: Internet
Author: User

Question Based on the idea of fast sorting (I) -- Dutch flag

Question Based on the idea of quicksort (I) -- the Dutch flag


We all know that the implementation of quick sort is mainly a partition and exchange process. This idea is actually very clever. Based on this, many questions can be used to solve it well. In this article, we talked about the Dutch flag problem, which means the idea of "Quick Sort" can be used ~ There will also be a series of questions in the future, which should be able to use the Quick Sort idea ~


1. Description


This topic was proposed by the Dutch scientist Dijkstra. First, we entered a three-color ball (red, white, and blue) in an out-of-order arrangement. If we exchanged the red ball in two or two ways, we placed all the red balls in front, the white ball is in the middle, and the blue ball is at the end ~



2. images and texts are illustrated to answer


With the idea of "Quick Sort", we first use three pointers: head, middle, and tail. Initially, head and middle point to the first ball, and tail points to the last ball. Then, the rules for moving and switching are as follows:

(0 indicates red, 1 indicates white, and 2 indicates blue)


  • When the element referred to by the middle pointer is 0, it is exchanged with the element referred to by the head pointer, and then middle ++, head ++;
  • When the value of the element referred to by the middle pointer is 1, no exchange is made (that is, the ball does not move), and then the middle ++;
  • When the element referred to by the middle pointer is 2, it is exchanged with the element referred to by the tail pointer. Then, the middle pointer does not move and tail --.
Stop moving until current> tail.


In fact, we can understand that when the middle points to 0, this 0 should be in the front, so we need to exchange it with the head, and after the switch, the natural head ++, middle ++, because at this time it has been ensured that the front of the head is 0; when the latter is pointed to 2, we need to exchange with tail for the same reason as above, after the switch, we only need to put the tail --. At that time, the middle should not be dynamic, because we do not know the situation after the switch, we need to further judge.


The following figure shows the movement:






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]<<' '; cout<<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; }







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.