Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <cstdlib>
- #include <vector>
- #include <algorithm>
- #include <iostream>
- #include <vector>
- #include <iomanip>
- using namespace std;
- void bucketSort(vector<float> &x, int n)
- {
- // 1) Create n empty buckets
- vector<float> b[n];
- // 2) Put aay elements in different buckets
- for (int i = 0; i<n; i++)
- {
- int bi = n * x[i]; // Index in bucket
- b[bi].push_back(x[i]);
- }
- // 3) Sort individual buckets
- for (int i = 0; i<n; i++)
- sort(b[i].begin(), b[i].end());
- // 4) Concatenate all buckets into x[]
- int index = 0;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < b[i].size(); j++)
- x[index++] = b[i][j];
- }
- int main() {
- ifstream input("input.txt");
- int n(0);
- input >> n;
- vector<vector<float>> matrix(n, vector<float>(n));
- for (int i(0); i < n; ++i) {
- for (int j(0); j < n; ++j)
- input >> matrix[i][j];
- }
- input.close();
- for (int i(0); i < n; ++i)
- bucketSort(matrix[i]);
- ofstream output("output.txt");
- for (int i(0); i < n; ++i) {
- for (int j(0); j < n; ++j)
- output << matrix[i][j] << ' ';
- output << 'n';
- }
- output.close();
- }'
- for (int i = 0; i < n; i++)
- for (int j = 0; j < b[i].size(); j++)
- x[index++] = b[i][j];
- }
Add Comment
Please, Sign In to add comment