Advertisement
Okorosso

bucketSort

Apr 15th, 2021
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. std::vector<std::string> bucketSort(std::vector<std::string> &arr) {
  6.     using namespace std;
  7.     vector<vector<string>> bucket(74);
  8.     vector<string> answer;
  9.  
  10.     for (auto elem : arr) {
  11.         int index = ((int) elem[0]) - 48;
  12.         bucket[index].push_back(elem);
  13.     }
  14.  
  15.     for (int i = 0; i < bucket.size(); i++) {
  16.         sort(bucket[i].begin(), bucket[i].end());
  17.     }
  18.  
  19.     for (int i = 0; i < bucket.size(); i++) {
  20.         if (!bucket[i].empty()) {
  21.             for (int j = 0; j < bucket[i].size(); j++) {
  22.                 answer.push_back(bucket[i][j]);
  23.             }
  24.         }
  25.     }
  26.     return answer;
  27. }
  28.  
  29. int main() {
  30.     using namespace std;
  31.     ifstream in("input.txt");
  32.     ofstream out("output.txt");
  33.     int count;
  34.     in >> count;
  35.     vector<string> toSort(count);
  36.  
  37.     for (int i = 0; i < count; i++){
  38.         in >> toSort[i];
  39.     }
  40.  
  41.     vector<string> answ = bucketSort(toSort);
  42.     for (auto elem:answ) {
  43.         out << elem << '\n';
  44.     }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement