Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <forward_list>
- #include <algorithm>
- #include <fstream>
- #include <vector>
- std::vector<double> readVectorFromFile(std::string fileName)
- {
- double x;
- std::vector<double> vec;
- std::ifstream fin(fileName);
- while (fin >> x)
- {
- if (x >= 0 && x < 1)
- vec.push_back(x);
- }
- return vec;
- }
- std::vector<double> bucketSort(std::vector<double> vec)
- {
- int n = vec.size();
- std::vector<std::forward_list<double>> buckets(n);
- for (double x : vec)
- {
- int pos = x * n;
- buckets[pos].push_front(x);
- }
- for (auto list : buckets)
- list.sort();
- std::vector<double> sortedVec;
- for (auto list : buckets)
- for (double x : list)
- sortedVec.push_back(x);
- return sortedVec;
- }
- void showVector(std::vector<double> vec)
- {
- for (double x : vec)
- std::cout << x << ' ';
- std::cout << std::endl;
- }
- int main()
- {
- std::vector<double> vec = readVectorFromFile("vector.txt");
- showVector(vec);
- vec = bucketSort(vec);
- std::cout << "Sorted: ";
- showVector(vec);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment