DSA-Assignments

Log | Files | Refs | README

Problem1.cpp (4613B)


      1 // Problem 1: Bucket Sort
      2 // Description: Sort a vector using a modified version of bucket sort.
      3 // Course: IT405G - Datastructures and Algorithms
      4 // Authors: William Lindholm, Lili Tran, Victor Adamson
      5 // Date: 13-11-2023
      6 //
      7 
      8 
      9 #include <iostream>
     10 #include <vector>
     11 #include <sstream>
     12 #include <string>
     13 #include <chrono>
     14 #include <algorithm>
     15 #include <tuple>
     16 
     17 using namespace std;
     18 
     19 
     20 // Function prototypes
     21 vector<int> bucketSort(vector<int> unsortedVector);
     22 vector<int> insertionSort(vector<int> unsortedVector);
     23 string vectorToString(vector<int> vector);
     24 int findMax(vector<int> v);
     25 vector<int> generateRandomVector(int low, int high, int size);
     26 void showProgressBar(int width, double progress);
     27 void printuple(tuple<int, int, double> t);
     28 
     29 int main()
     30 {
     31 	int startingSize = 1000;
     32 	int iterations = 100;
     33 	int arraySize = 10000;
     34 	int stepSize = 1000;
     35 
     36 	srand(12345);
     37 
     38 	vector<tuple<int, int, double>> durations;
     39 
     40 	for (int i = startingSize; i < arraySize; i += stepSize) {
     41 		cout << "\nArray element max value: " << i << endl;
     42 		for (int j = 1; j < iterations + 1; j++) {
     43 
     44 			showProgressBar(100, (double)j / iterations);
     45 
     46 			vector<int> unsorted = generateRandomVector(0, i, j * stepSize);
     47 
     48 			auto start = chrono::high_resolution_clock::now();
     49 
     50 			vector<int> sorted = bucketSort(unsorted);
     51 
     52 			auto end = chrono::high_resolution_clock::now();
     53 
     54 			chrono::duration<double> elapsed = end - start;
     55 
     56 			durations.push_back(make_tuple(i, j * 1000, elapsed.count()));
     57 		}
     58 	}
     59 	
     60 	cout << endl;
     61 
     62 	for (tuple<int, int, double> t : durations) {
     63 		printuple(t);
     64 	}
     65 
     66 	//vector<int> insertSorted = insertionSort(unsorted);
     67 	//cout << "The unsorted vector: " << vectorToString(unsorted) << endl;
     68 	//cout << "The sorted vector: " << vectorToString(sorted) << endl;
     69 	//cout << "The sorted vector using InsertSort: " << vectorToString(insertSorted) << endl;
     70 
     71 	return 0;
     72 }
     73 
     74 
     75 /*
     76  * Function: bucketSort
     77  * Sort an unsorted vector using a modified version of bucket sort
     78  * @param v: the unsorted vector
     79  * @return: the sorted vector
     80  */
     81 vector<int> bucketSort(vector<int> v)
     82 {
     83 	int max = findMax(v);
     84 
     85 	// Create vector of 0s
     86 	vector<int> w(max + 1, 0);
     87 
     88 	// Add values to buckets
     89 	for (int i = 0; i < (int)v.size(); i++)
     90 	{
     91 		w[v[i]] += 1;
     92 	}
     93 
     94 	// Create sorted vector
     95 	vector<int> sorted(0);
     96 
     97 	// Append from buckets in order to sorted vector
     98 	for (int i = 0; i < (int)w.size(); i++)
     99 	{
    100 		//check if bucket is empty
    101 		if (w[i] != 0)
    102 		{
    103 			// loop through each bucket
    104 			for (int j = 0; j < w[i]; j++) {
    105 				sorted.push_back(i);
    106 			}
    107 		}
    108 	}
    109 
    110 	return sorted;
    111 }
    112 
    113 /*
    114  * Function: insertSort
    115  * Sort an unsorted vector using insertionsort
    116  * @param v: the unsorted vector
    117  * @return: the sorted vector
    118  */
    119 vector<int> insertionSort(vector<int> v)
    120 {
    121 	int i, j, key;
    122 	for (i = 0; i < v.size(); i++)
    123 	{
    124 		key = v[i];
    125 		j = i - 1;
    126 
    127 		while (j >= 0 && v[j] > key)
    128 		{
    129 			v[j + 1] = v[j];
    130 			j = j - 1;
    131 		}
    132 		v[j + 1] = key;
    133 	}
    134 	return v;
    135 }
    136 
    137 /*
    138  * Function: findMax
    139  * Find the maximum value in a vector
    140  * @param v: the vector to search
    141  * @return: the maximum value in the vector
    142  */
    143 int findMax(vector<int> v)
    144 {
    145 	int max = v[0];
    146 	for (int i = 1; i < (int)v.size(); i++)
    147 	{
    148 		if (v[i] > max)
    149 		{
    150 			max = v[i];
    151 		}
    152 	}
    153 
    154 	return max;
    155 }
    156 
    157 
    158 /*
    159  * Function: vectorToString
    160  * Print a vector
    161  * @param vector: the vector to print
    162  * @return: the string representation of the vector
    163  */
    164 string vectorToString(vector<int> vector)
    165 {
    166 	string output = "{";
    167 	for (int i = 0; i < (int)vector.size(); i++)
    168 	{
    169 		output += to_string(vector[i]);
    170 		if (i != vector.size() - 1)
    171 		{
    172 			output += ", ";
    173 		}
    174 	}
    175 	output += "}";
    176 
    177 	return output;
    178 }
    179 
    180 
    181 /*
    182 * Function: generateRandomVector
    183 * Generate a random vector of a given size
    184 * @param low: the lowest possible value in the vector
    185 * @param high: the highest possible value in the vector
    186 * @param size: the size of the vector to generate
    187 */
    188 vector<int> generateRandomVector(int low, int high, int size)
    189 {
    190 	vector<int> v(size);
    191 	for (int i = 0; i < size; i++)
    192 	{
    193 		v[i] = rand() % (high - low + 1) + low;
    194 	}
    195 
    196 	return v;
    197 }
    198 
    199 /*
    200 * Function: showProgressBar
    201 * Show a progress bar in the console
    202 * @param width: the width of the progress bar
    203 * @param progress: the progress of the bar
    204 */
    205 void showProgressBar(int width, double progress) {
    206 	int pos = width * progress;
    207 
    208 	cout << "[";
    209 	for (int i = 0; i < width; ++i) {
    210 		if (i < pos) cout << "=";
    211 		else if (i == pos) cout << ">";
    212 		else cout << " ";
    213 	}
    214 	cout << "] " << int(progress * 100.0) << " %\r";
    215 	cout.flush(); // Important to flush the output
    216 }
    217 
    218 void printuple(tuple<int, int, double> t) {
    219 	cout << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << endl;
    220 }