diff options
fix counting sort
1 files changed, 8 insertions, 8 deletions
| -rw-r--r-- | Seminar07/counting-sort.cpp | 16 |
diff --git a/Seminar07/counting-sort.cpp b/Seminar07/counting-sort.cpp index a00f742..be662f2 100644 --- a/Seminar07/counting-sort.cpp +++ b/Seminar07/counting-sort.cpp @@ -3,16 +3,16 @@ #include<algorithm> #include<cassert> -const int ELEMENTS_RANGE = 1000; +void countingSort(std::vector<unsigned>& v) { + unsigned elementRange = *std::max_element(v.begin(), v.end()) + 1; -void countingSort(std::vector<int>& v) { - std::vector<int> count(ELEMENTS_RANGE, 0); - std::vector<int> result(v.size()); + std::vector<unsigned> count(elementRange, 0); + std::vector<unsigned> result(v.size()); for (size_t i = 0; i < v.size(); i++) count[v[i]]++; - for (size_t i = 1; i < ELEMENTS_RANGE; i++) + for (size_t i = 1; i < elementRange; i++) count[i] += count[i - 1]; for (int i = v.size() - 1; i >= 0; i--) { @@ -24,11 +24,11 @@ void countingSort(std::vector<int>& v) { } int main() { - std::vector<int> v; - const int length = 100000; + std::vector<unsigned> v; + const int length = 10000; for (size_t i = 0; i < length; i++) - v.push_back(rand() % ELEMENTS_RANGE); + v.push_back(rand() * rand()); countingSort(v); |
