Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar07 / counting-sort.cpp
diff options
context:
space:
mode:
Commit8ab4cd05e3195432d7e1df5839424313db0348d9 (patch)
Author
 Stoycho Kyosev <stoycho.kk@gmail.com>
2023-05-28 18:52:22 +0300
Committer
 Stoycho Kyosev <stoycho.kk@gmail.com>
2023-05-28 18:52:22 +0300
Files4d2403fca8ae0b5da68aeb3091479415414be9fa /Seminar07/counting-sort.cpp
Data-structures-and-algorithms-8ab4cd05e3195432d7e1df5839424313db0348d9.tar
Data-structures-and-algorithms-8ab4cd05e3195432d7e1df5839424313db0348d9.tar.gz
Data-structures-and-algorithms-8ab4cd05e3195432d7e1df5839424313db0348d9.zip
Parentc33d698d7385be241e85fde64422b5610cde1279 (diff)
fix counting sort
1 files changed, 8 insertions, 8 deletions
-rw-r--r--Seminar07/counting-sort.cpp16
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);