[Leetcode] [Java] Sort Colors

Source: Internet
Author: User

Title:

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?

Test Instructions:

Given a set of objects that have red, white, and blue colors, reorder them so that objects of the same color are adjacent. The order after sorting is red, white and blue.

Here we use 0, 1, and second respectively to represent the red and white blue three colors.

Note: You cannot use the sort function in the library.

Algorithm Analysis:

Method One:

A three-pointer approach, similar to a quick sort

left = 0, pointer right = n-1, one traversal, if 0 is encountered, swap to left, encounter 2, swap to right, encounter 1 forget it.

Method Two:

Here's a way to compare low, but it's better to understand
Count the number of 0,1,2 separately, and re-assign the array

AC Code:

public class Solution {public    void Sortcolors (int[] a)     {        if (a = = NULL | | A.length = = 0) return;        int left = 0;        int right = A.length-1;        int cur = left;                while (cur <= right)        {        if (a[cur] = = 0)         {        swap (A, left++, cur);        Cur = (cur <= left)? left:cur;        }         else if (a[cur] = = 2)         {        swap (A, right--, cur);        }         else         {        cur++;}}    }    public void Swap (int[] A, int i, int j)     {        int temp = a[i];        A[i] = a[j];        A[J] = temp;    }}

Method Two:

public class solution{public    void Sortcolors (int[] A)     {        int count0 = 0;int Count1 = 0;int Count2 = 0;for (int i = 0; i < a.length; i++) {if (a[i] = = 0) {count0++;} if (a[i] = = 1) {count1++;} if (a[i] = = 2) {count2++;}} for (int i = 0; i < count0; i++) {a[i] = 0;} for (int i = count0; i < count0+count1; i++) {a[i] = 1;} for (int i = count0+count1; i < Count0+count1+count2; i++) {a[i] = 2;}}    }


Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source

[Leetcode] [Java] 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.