[Leetcode] Special Solution of Bucket sorting, for example, sort color

Source: Internet
Author: User
Sort colors

Given an arrayNObjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

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

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

class Solution {public:    void sortColors(int A[], int n) {    }};

 

Such values are ordered within a certain range and can be sorted by buckets.

However, the question is limited to avoid sorting by buckets. Or, only one traversal is allowed.

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.

Cocould you come up with an one-pass algorithm using only constant space?

 

The topic features that there are only three very few values: 0, 1, 2. In this case, we can define three pointers: I, J, K, pointing to the end of 0, 1, and 2 respectively. Of course, I, J, and K are overlapped at the first time, and the value is-1.

K goes first. No matter what value you encounter, save the value as TMP and assign the current position to 2. Because even if TMP is not 2, for example, 0, K also needs to move the location for this 0, so it is not wrong to set the K position to 2. Then, if TMP <= 1, J advances and the J bit is assigned to 1, the same is true, and it is also for the purpose of place. Final process I.

The reason why K must first go, followed by J, and finally I, first is because the sorting requirements must be 0, 1, 2; second, consider this situation: the current I, j, k overlap, k, the first step is 0. Because K goes first, although K is set to 2 first, the J I will overwrite the value of this position to the correct value.

class Solution {public:    void sortColors(int A[], int n) {        if(n == 0) return;        int i = -1, j = -1, k = -1, tmp = 0;        while(k < n-1){            tmp = A[++k];            A[k] = 2;            if(tmp <= 1) A[++j] = 1;            if(tmp == 0) A[++i] = 0;        }        return;    }};

 

Summary

Sort within a fixed value range by bucket. If the value range is small, there are only a few values. We can directly define the same amount of pointers, and solve the problem through one traversal.

[Leetcode] Special Solution of Bucket sorting, for example, sort color

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.