Utilization of bucket sorting

Utilization of bucket sorting

Bucket sort, or so-called bin sort , is a sorting algorithm that works by dividing numbers into a limited number of buckets. Sort each bucket individually

image-20210226235545685

example:

Give you a string s, please reconstruct the string according to the following algorithm:

Select the smallest character from s and connect it to the end of the result string.
Select the smallest character from the remaining characters in s, and the character is larger than the last added character, and it is appended to the result string.
Repeat step 2 until you cannot select characters from s.
Select the largest character from s and connect it to the end of the result string.
Select the largest character from the remaining characters in s, and the character is smaller than the last added character, and it is appended to the result string.
Repeat step 5 until you cannot select characters from s.
Repeat steps 1 to 6 until all characters in s have been selected.
In any step, if there is more than one minimum or maximum character, you can select any one of them and add it to the result string.

Please return the resulting string after reordering the characters in s.

Example 1:

Input: s = "aaaabbbbcccc"
Output: "abccbaabccba"
Explanation: After steps 1, 2, and 3 of the first round, the result string is result = "abc"
After steps 4, 5, and 6 of the first round, the result string The
first round of result = "abccba" is over, now s = "aabbcc", we go back to step 1, after the
second round of steps 1, 2, and 3, the result string is result = "abccbaabc" the
second round of steps After 4, 5, and 6, the result string is result = "abccbaabccba"
Example 2:

Input: s = "rat"
Output: "art"
Explanation: The word "rat" becomes "art" after the above algorithm is reordered.
Example 3:

Input: s = "leetcode"
Output: "cdelotee"
Example 4:

Input: s = "ggggggg"
Output: "ggggggg"
Example 5:

Input: s = "spo"
Output: "ops"

prompt:

1 <= s.length <= 500
s contains only lowercase English letters.

My code


class Solution
{
public:
   string sortString(string s)
   {
       map<char, int> hash;
       string ans = "";
       for (int i = 0; i < s.size(); i++)
       {
           hash[s[i]]++;
       }
       int cnt = hash.size();
       while (cnt)
       {
           // int n = hash.size();
           // for(int j=0;j<n;j++)
           for (auto it = hash.begin(); it != hash.end(); it++)
           {
               if (it->second)
               {
                   ans += it->first;
                   it->second--;
                   if (it->second == 0)
                       cnt--;
               }
           }
           // else
           for (auto it = hash.rbegin(); it != hash.rend(); it++)
           {
               if (it->second)
               {
                   ans += it->first;
                   it->second--;
                   if (it->second == 0)
                       cnt--;
               }
           }
       }
       return ans;
   }
};


The above code is correct. Since it is correct, in order to keep improving and optimizing time and space, we must consider how to optimize;

  1. Map is a waste of space. After all, it occupies a lot of space, but in fact, you don't need to use map.Instead use the array(After all, there are 26 letters,) This is a great optimization for space; ==> This method is bucket sorting; (Bucket sorting was learned at the beginning, but it was almost never used, and it was hardly thought about when doing questions, but I did it recently. This often appears in the question, so record it)

Then take a look at the following code

class Solution {
public:
    //题目描述的复杂,其实就是不停地按字典序升序追加,降序追加,每次加一个
    string sortString(string s) {
        vector<int>tmp(26);
        for(int i=0;i<s.size();i++)
            tmp[s[i]-'a']++;
        string ret;
        while(ret.size()<s.size())
        {
            for(int i=0;i<26;i++)
                if(tmp[i])
                    ret+=i+'a',tmp[i]--;
            for(int i=25;i>=0;i--)
                if(tmp[i])
                    ret+=i+'a',tmp[i]--;
        }
        return ret;
    }
};

You can take a look at the picture below: time and space are reduced;

image-20210227000349373


In addition, put the concept of hash tables below:

If the key is k , its value is stored in the storage location of f(k) . As a result, the checked records can be directly obtained without comparison. Call this corresponding relationship f a hash function, and the table built according to this idea is a hash table.

(I personally always confuse the idea of ​​hash table and bucket sorting, so record it)

to sum up
  • Bucket sorting can be used to store a group of elements with relatively uniform values ​​(includingLetters and numbers)

  • But if the value is uniform instead of starting from 0, then you can use hash table plus bucket sorting

  • 比如一群数:1020,1023,1021,1022,1024........
    那么大可以令f(x)=1050-x;等等
    

Knot

  • Bucket sorting can be used to store a group of elements with relatively uniform values ​​(includingLetters and numbers)

  • But if the value is uniform instead of starting from 0, then you can use hash table plus bucket sorting

  • 比如一群数:1020,1023,1021,1022,1024........
    那么大可以令f(x)=1050-x;等等
    
  • If the value is not uniform, use map storage;

Guess you like

Origin blog.csdn.net/weixin_45929885/article/details/114156953