[Leetcode] Sort Colors only 3 types of sorting

Source: Internet
Author: User

Given an array with n objects colored red, white or blue, sort them so, objects of the same color is Adjacen T, with the colors in the order red, white and blue.

Here, we'll use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Note:
You is not a suppose to use the library's sort function for this problem.

Click to show follow up.

Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0 ' s, 1 ' s, and 2 ' s, then overwrite array with total number of 0 ' s, then 1 ' s and Followed by 2 ' s.

Could you come up with a one-pass algorithm using only constant space?

Hide TagsArray of Pointers Sort This question is quite simple, that is, only 3 types (0 1 2) of the sort, the topic gave a two traversal of the sorting method, that is, the number of occurrences of statistics and then fill in the array, the request is to traverse a complete, look at the discuss, the implementation of similar logic, that is, how to write. Ideas:
    1. Create a zeroidx that represents the end subscript of 0, initialized to 0,twoidx to represent the subscript of 2, initialized to n, and different places are the former operation + +, the latter--。
    2. Iterating through an array if it encounters 1, continues the traversal.
    3. If 0 is encountered, replace ZEROIDX with the value on I, then Zerosidx +1, if the value at this position is bit 0, then the array is 0 from 0 to I, so zeroidx = i+1.
    4. If you encounter 2, then twoidx-1, then replace the value of twoidx with I, because the value on the I position after the substitution is not judged, so the i-1 does more traversal at that location.

3. In Zeroidx can always +1 not directly jump, so need to exchange many times, discuss implementation is so.

Here is the code I wrote:

1#include <iostream>2 using namespacestd;3 4 classSolution {5  Public:6     voidSortcolors (intA[],intN) {7         intZeroidx =0;8         intTwoidx =N;9          for(inti =0; i<n&&i<twoidx;i++){Ten             if(a[i]==1){ One                 Continue; A             } -             if(a[i]==0){ -A[i] =A[zeroidx]; theA[ZEROIDX] =0; -                 if(++zeroidx<n&&a[zeroidx]==0) zeroidx=i+1; -             } -             Else if(a[i]==2){ +twoidx--; -a[i]=A[twoidx]; +a[twoidx]=2; Ai--; at             } -             Else -                 return ; -         } -         return ; -     } in }; -  to intMain () + { -     intA[] = {1,2,0,1}; the solution Sol; *Sol.sortcolors (A,sizeof(a)/sizeof(int)); $      for(intI=0;i<sizeof(a)/sizeof(int); i++)Panax Notoginsengcout<<a[i]<<" "; -cout<<Endl; the     return 0; +}
View Code

There is a very good implementation in discuss, logic clear, you can expand the number of variables 3 to K, as long as not afraid to write trouble, its logic is similar to the insertion of the sort, the traversed item into the correct position:

    1. Create an index a B c=-1 for 3 (k) variables;
    2. Iterate over the array, if it is 0, order modify A[++c] a[++b] a[++a], so if ABC, eventually only modify the A[++a] This item, and then update the IDX of 3.
    3. If 1, the order modifies A[++c] a[++b], so that the index of 0 is unchanged.
    4. If 2, the order is modified A[++c].
1   Public voidSortcolors (int[] A) {2 3 4     inti=-1, j=-1, k=-1;5 6      for(intp =0; P < a.length; p++)7     {8         if(A[p] = =0)9         {Tena[++k]=2; Onea[++j]=1; Aa[++i]=0; -         } -         Else if(A[p] = =1) the         { -a[++k]=2; -a[++j]=1; -  +         } -         Else if(A[p] = =2) +         { Aa[++k]=2; at         } -     } -  -}
View Code

[Leetcode] Sort Colors only 3 types of sorting

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.