DSA-Assignments

Log | Files | Refs | README

commit 56ab0f8a1f49640878f7f19c622a491f3f387f58
parent 7adc346f3296ae3f4c937cf47f5838546e8ac704
Author: William Lindholm <a22willi@student.his.se>
Date:   Thu, 16 Nov 2023 16:49:55 +0100

Now handling duplicate values.

Diffstat:
MProblem1/Problem1.cpp | 15++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/Problem1/Problem1.cpp b/Problem1/Problem1.cpp @@ -19,7 +19,7 @@ int findMax(vector<int> v); int main() { - vector<int> unsorted = { 41, 12, 53, 14, 5, 62, 7, 28, 9 }; + vector<int> unsorted = { 41, 12, 12, 53, 14, 5, 62, 7, 12, 28, 9 }; vector<int> sorted = bucketSort(unsorted); cout << "The unsorted vector: " << vectorToString(unsorted) << endl; @@ -39,13 +39,13 @@ vector<int> bucketSort(vector<int> v) { int max = findMax(v); - // Create buckets - vector<int> w(max + 1); + // Create buckets (each bucket is a vector) + vector<vector<int>> w(max + 1, vector<int>()); // Add values to buckets for (int i = 0; i < v.size(); i++) { - w[v[i]] = v[i]; + w[v[i]].push_back(v[i]); } // Create sorted vector @@ -54,10 +54,11 @@ vector<int> bucketSort(vector<int> v) // Append from buckets in order to sorted vector for (int i = 0; i < w.size(); i++) { - - if (w[i] != NULL || w[i] != 0) + if (!w[i].empty()) { - sorted.push_back(w[i]); + for (int x : w[i]) { + sorted.push_back(x); + } } }