Guest User

Untitled

a guest
Mar 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstdlib>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <vector>
  7. #include <iomanip>
  8. using namespace std;
  9. void bucketSort(vector<float> &x, int n)
  10. {
  11. // 1) Create n empty buckets
  12. vector<float> b[n];
  13.  
  14. // 2) Put aay elements in different buckets
  15. for (int i = 0; i<n; i++)
  16. {
  17. int bi = n * x[i]; // Index in bucket
  18. b[bi].push_back(x[i]);
  19. }
  20.  
  21. // 3) Sort individual buckets
  22. for (int i = 0; i<n; i++)
  23. sort(b[i].begin(), b[i].end());
  24.  
  25. // 4) Concatenate all buckets into x[]
  26. int index = 0;
  27. for (int i = 0; i < n; i++)
  28. for (int j = 0; j < b[i].size(); j++)
  29. x[index++] = b[i][j];
  30. }
  31. int main() {
  32. ifstream input("input.txt");
  33.  
  34. int n(0);
  35. input >> n;
  36. vector<vector<float>> matrix(n, vector<float>(n));
  37. for (int i(0); i < n; ++i) {
  38. for (int j(0); j < n; ++j)
  39. input >> matrix[i][j];
  40. }
  41. input.close();
  42. for (int i(0); i < n; ++i)
  43. bucketSort(matrix[i]);
  44. ofstream output("output.txt");
  45. for (int i(0); i < n; ++i) {
  46. for (int j(0); j < n; ++j)
  47. output << matrix[i][j] << ' ';
  48. output << 'n';
  49. }
  50. output.close();
  51. }'
  52.  
  53. for (int i = 0; i < n; i++)
  54. for (int j = 0; j < b[i].size(); j++)
  55. x[index++] = b[i][j];
  56. }
Add Comment
Please, Sign In to add comment