Color Sort--sort Colors

Source: Internet
Author: User

https://leetcode.com/problems/sort-colors/

Sort Colors

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 R epresent The color red, white, and blue respectively.

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

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?

Test instructions: Sort into red, white, blue (0,1,2)

Analysis of the South Guo from http://www.cnblogs.com/zuoyuan/p/3775832.html

Problem solving: This problem does not allow the use of sort library functions. Then the most intuitive solution is: traverse the array two times, the first time on the 0,1,2 count, the second time the array is assigned, so it can be AC. But the requirement of the topic is that only constant space is used, and it can only be traversed once. Then the thinking is more ingenious. Set the two head-and-tail pointer, the position of the first pointer p0 to 0 where the position is placed, and the position where the tail pointer P2 points is 2. I used to traverse the entire array, hit 0 and P0 point to the number of exchanges, hit 2 it and P2 point to the number of exchanges, hit 1 continue to traverse backwards. A bit like a quick sort of split array this step.

1 classSolution:2     #@param {integer[]} nums3     #@return {void} does not return anything, modify Nums in-place instead.4     defsortcolors (Self, nums):5A=Nums6         ifA==[]:returnA7p0=0; P2=len (A)-1; I=0#P0 in the head, p2 in the tail8          whilei<=P2:9             ifa[i]==2:#Encounter 2, the number of 2 and A[P2] is interchanged, p2 forwardTenA[i],a[p2]=a[p2],a[i]#at this time not i+=1, because the number of changes still need to judge OneP2-=1 A             elifA[i]==0:#encountered 0, the number of 0 and A[p0] is interchanged, p0 backwards -a[i],a[p0]=A[p0],a[i] -P0+=1 theI+=1#at this time i+=1, because the change of a[p0] is either 0 or 1, so I continue backwards -             Else: -I+=1#hit 1 and continue to judge backwards -                                        #P0 will change in front of 1 to 0, not to change the 1 rows in the middle to become the final result +                                        #The results are sorted by 0,1,2 order that is required for red, white, and blue -     

Question: Why not return at the end?

Color Sort--sort Colors

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.