## C++ || Decrease By Half Sorting Using Bubble Sort, Quick Sort, & Optimized Bubble Sort

The following is another homework assignment which was presented in an Algorithm Engineering class. Using a custom timer class, the following is a program which tries to improve upon the sorting code demonstrated in the initial Empirical Analysis.

The following program will execute two approaches: (1) implementing an algorithm with better asymptotic performance, and (2) tuning an existing algorithm.

==== 1. THE OBJECTIVE ====

The purpose of implementing this program is to obtain empirical results that answer the following questions:

```• Are O(n log n) expected-time sorting algorithms, such as merge sort and quick sort, significantly faster than O(n2)-time algorithms in practice? • If so, by what margin? Is implementing a faster algorithm worth the effort? • Is it possible to get a O(n2)-time algorithm to beat a O(nlogn)-time algorithm by paying attention to implementation details? • If so, how much faster? Do you get better bang-for-the-buck by switching to an asymptotically-faster algorithm, or optimizing the same algorithm? ```

==== 2. THE ALGORITHMS ====

This program involves implementing and analyzing three algorithms:

```1. Baseline: The O(n2) sorting algorithm implemented in Project 1. 2. Decrease-by-half: An O(n log n) algorithm (Quick Sort). 3. Optimized: A tuned, optimized version of the O(n2) baseline algorithm. ```

==== 3. FLOW OF CONTROL ====

A test harness program is created which executes the above functions and measures the elapsed time of the code corresponding to the algorithm in question. The test program will perform the following steps:

``` ```

```1. Input the value of n. Your code should treat n as a variable. 2. Create an array or vector of n random integers to serve as a problem instance. 3. Use a clock function to get the current time t1 . 4. Execute one algorithm (Bubble Sort, Quick sort, or Optimized Bubble Sort), using the array of random integers as input. 5. Use a clock function to get the current time t2 . 6. Output the elapsed time, t2 − t1 . ```

The test harness is configured in such a way to run all of the three algorithms, using a switch statement to change between the algorithms.

==== 4. TEST HARNESS ====

Note: This program uses two external header files (Timer.h and Project1.h).
• Code for the Timer class (Timer.h) can be found here.
• Code for “Project1.h” can be found here.
• “Project3.h” is listed below.

``` Decrease By Half Test Harness C++ // ============================================================================= // Author: K Perkins // Date: Nov 2, 2013 // Taken From: http://programmingnotes.org/ // File: main.cpp // Description: This is the test harness program which runs the code and // measures the elapsed time of the code corresponding to the algorithm // in question. This program will try to improve the sorting methods // written in Project 1 by two approaches: // (1) Implementing an algorithm with better asymptotic performance // (2) Tuning an existing algorithm. // The sorting methods being reviewed are Bubble sort, Quick sort, and an // optimized version of Bubble sort. // ============================================================================= #include <iostream> #include <cstdlib> #include <ctime> #include "Timer.h" #include "Project1.h" #include "Project3.h" using namespace std; // typedefs for the functions to use enum Algorithms {bubble, quick, bubbleOptim}; // number of algs being tested int NUM_ALGS = 3; // default arry size const int ARRAY_SIZE = 150000; int main() { // declare variables srand(time(NULL)); int* arry = new int[ARRAY_SIZE]; int seed = rand(); // generate a random seed to use for array on all algorithms Timer timer; Project1 proj1; Project3 proj3; // display the array size cerr<<"nArray Size = "<<ARRAY_SIZE<<endl; // loop to automatically execute the proj1 being tested for(int x=0; x < NUM_ALGS; ++x) { cout<<"n----- STARTING ALGORITHM #"<<x+1<<" ----- nn"; timer.Reset(); // place data in the array proj1.Generate(arry, ARRAY_SIZE, seed); // display data in array if its within range if(ARRAY_SIZE <= 25) { proj1.Display(arry, ARRAY_SIZE); cout<<endl; } // start the timer timer.Start(); // determine which alg to execute switch(x) { case bubble: proj1.BubbleSort(arry, ARRAY_SIZE); break; case quick: proj3.QuickSort(arry, ARRAY_SIZE); break; case bubbleOptim: proj3.BubbleSort(arry, ARRAY_SIZE); break; default: cout<<"nThat option doesnt exist...n"; exit(1); break; } // stop the timer timer.Stop(); // display data in array if its within range if(ARRAY_SIZE <= 25) { cout<<endl; proj1.Display(arry, ARRAY_SIZE); cout<<endl; } // display time cout<<endl<<"It took "<<timer.Elapsed()*1000 <<" clicks ("<<timer.Elapsed()<<" seconds)"<<endl; cout<<"n----- ALGORITHM #"<<x+1<<" DONE! ----- nn"; } delete[] arry; return 0; }// http://programmingnotes.org/ 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 // =============================================================================//     Author: K Perkins//     Date:   Nov 2, 2013//     Taken From: http://programmingnotes.org///     File:  main.cpp//     Description: This is the test harness program which runs the code and //       measures the elapsed time of the code corresponding to the algorithm //       in question. This program will try to improve the sorting methods//       written in Project 1 by two approaches: //         (1) Implementing an algorithm with better asymptotic performance//         (2) Tuning an existing algorithm. //       The sorting methods being reviewed are Bubble sort, Quick sort, and an //       optimized version of Bubble sort.// =============================================================================#include <iostream>#include <cstdlib>#include <ctime>#include "Timer.h"#include "Project1.h"#include "Project3.h"using namespace std; // typedefs for the functions to useenum Algorithms {bubble, quick, bubbleOptim}; // number of algs being testedint NUM_ALGS = 3; // default arry sizeconst int ARRAY_SIZE = 150000; int main(){ // declare variables srand(time(NULL)); int* arry = new int[ARRAY_SIZE]; int seed = rand(); // generate a random seed to use for array on all algorithms Timer timer; Project1 proj1; Project3 proj3; // display the array size cerr<<"nArray Size = "<<ARRAY_SIZE<<endl;   // loop to automatically execute the proj1 being tested for(int x=0; x < NUM_ALGS; ++x) {     cout<<"n----- STARTING ALGORITHM #"<<x+1<<" ----- nn";      timer.Reset(); // place data in the array proj1.Generate(arry, ARRAY_SIZE, seed); // display data in array if its within range if(ARRAY_SIZE <= 25) { proj1.Display(arry, ARRAY_SIZE); cout<<endl; }  // start the timer timer.Start(); // determine which alg to execute switch(x) { case bubble: proj1.BubbleSort(arry, ARRAY_SIZE); break; case quick: proj3.QuickSort(arry, ARRAY_SIZE); break; case bubbleOptim: proj3.BubbleSort(arry, ARRAY_SIZE); break; default: cout<<"nThat option doesnt exist...n"; exit(1); break; } // stop the timer timer.Stop(); // display data in array if its within range if(ARRAY_SIZE <= 25) { cout<<endl; proj1.Display(arry, ARRAY_SIZE); cout<<endl; } // display time cout<<endl<<"It took "<<timer.Elapsed()*1000 <<" clicks ("<<timer.Elapsed()<<" seconds)"<<endl; cout<<"n----- ALGORITHM #"<<x+1<<" DONE! ----- nn"; }  delete[] arry; return 0;}// http://programmingnotes.org/ ```

==== 5. THE ALGORITHMS – “include Project3.h” ====

``` Bubble Sort, Quick Sort, & Optimized Bubble Sort C++ // ============================================================================= // Author: K Perkins // Date: Nov 2, 2013 // Taken From: http://programmingnotes.org/ // File: Project3.h // Description: This is a simple class which holds the functions for // project 3 // ============================================================================= #ifndef PROJECT3_H #define PROJECT3_H #include <cstdlib> #include <algorithm> class Project3 { public: Project3(){} // exchange unsorted elements with the last element // not the adjacent element void BubbleSort(int arry[], int size) { int last = size-1; do{ for(int current=0; current < last; ++current) { if(arry[current] > arry[last]) { std::swap(arry[current], arry[last]); } } --last; }while(last >= 0); }// end of BubbleSort void QuickSort(int arry[], int size) { if(size > 1) { // choose pivot int pivotIndex = rand()%(size-1); // partition the arry and get the new pivot position int newPiviotIndex = Partition(arry, size, pivotIndex); // quick sort the first part QuickSort(arry, newPiviotIndex); // quick sort the second part QuickSort(arry+newPiviotIndex+1, size-newPiviotIndex-1); } }// end of QuickSort int Partition(int arry[], int size, int pivotIndex) { int pivotValue = arry[pivotIndex]; arry[pivotIndex] = arry[size-1]; // swap pivot with last element arry[size-1] = pivotValue; int left = 0; // left index int right = size-2; // right index while(left < right) { // ( < pivot ), pivot, ( >= pivot) while((arry[left] < pivotValue) && (left < right)) { ++left; } while((arry[right] >= pivotValue) && (left < right)) { --right; } if(left < right) { std::swap(arry[left], arry[right]); ++left; --right; } } if(left == right) { if(arry[left] < pivotValue) { ++left; } } arry[size-1] = arry[left]; // move pivot to its final place arry[left] = pivotValue; return left; // return the position of the pivot }// end of Partition ~Project3(){} }; #endif // http://programmingnotes.org/ 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 // =============================================================================//     Author: K Perkins//     Date:   Nov 2, 2013//     Taken From: http://programmingnotes.org///     File:  Project3.h//     Description: This is a simple class which holds the functions for //       project 3// =============================================================================#ifndef PROJECT3_H#define PROJECT3_H #include <cstdlib>#include <algorithm> class Project3{public: Project3(){} // exchange unsorted elements with the last element // not the adjacent element void BubbleSort(int arry[], int size) { int last = size-1; do{ for(int current=0; current < last; ++current) { if(arry[current] > arry[last]) { std::swap(arry[current], arry[last]); } } --last; }while(last >= 0); }// end of BubbleSort  void QuickSort(int arry[], int size) { if(size > 1) { // choose pivot int pivotIndex = rand()%(size-1); // partition the arry and get the new pivot position int newPiviotIndex = Partition(arry, size, pivotIndex); // quick sort the first part QuickSort(arry, newPiviotIndex); // quick sort the second part QuickSort(arry+newPiviotIndex+1, size-newPiviotIndex-1); } }// end of QuickSort  int Partition(int arry[], int size, int pivotIndex) { int pivotValue = arry[pivotIndex]; arry[pivotIndex] = arry[size-1]; // swap pivot with last element arry[size-1] = pivotValue; int left = 0; // left index int right = size-2; // right index while(left < right) { // ( < pivot ), pivot, ( >= pivot) while((arry[left] < pivotValue) && (left < right)) { ++left; } while((arry[right] >= pivotValue) && (left < right)) { --right; } if(left < right) { std::swap(arry[left], arry[right]); ++left; --right; } } if(left == right) { if(arry[left] < pivotValue) { ++left; } } arry[size-1] = arry[left]; // move pivot to its final place arry[left] = pivotValue; return left; // return the position of the pivot }// end of Partition ~Project3(){}};#endif // http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Note: This page presents sample code for the above problem, but scatter plots will not be provided.

The following is sample output:

`Array Size = 150000`

``` ----- STARTING ALGORITHM #1 ----- It took 248290 clicks (248.29 seconds) ----- ALGORITHM #1 DONE! ----- ----- STARTING ALGORITHM #2 ----- It took 50 clicks (0.05 seconds) ----- ALGORITHM #2 DONE! ----- ----- STARTING ALGORITHM #3 ----- It took 164300 clicks (164.3 seconds) ```

```----- ALGORITHM #3 DONE! ----- ```

## C++ || Knapsack Problem Using Exhaustive Search

The following is another homework assignment which was presented in an Algorithm Engineering class. Using a custom timer class, the following is a program which reduces the problem of selecting which Debian Linux packages to include on installation media, to the classical knapsack problem. This program implements an exhaustive search algorithm for this problem, and displays its performance running time to the screen.

NOTE: Looking for the Dynamic Programming version to this problem? Click here!

==== 1. OVERVIEW ====

Debian is a well-established GNU/Linux distribution. Ubuntu Linux, arguably the most popular Linux distribution, is based on Debian. Debian software is organized into packages. Each package corresponds to a software component, such as the GCC compiler or Firefox web browser. There are packages not only for the software that comprises the operating system, but also end-user applications. Nearly every piece of noteworthy open source software has a corresponding package. There are currently over 29,000 Debian source code packages available.

This wide selection of packages is mostly a good thing. However it poses a problem for creating installation media – the floppy disks, CDs, DVDs, or flash drive images that administrators use to install the operating system. If a user wants to install a package that is missing from their installation media they need to download it over the internet, so it is beneficial to include as many packages as possible. It is impractical to include every package on those media, so the Debian project needs to select a small subset of important packages to include.

Another part of Debian is the Popularity Contest, or popcon:

The Popularity Contest tries to measure the popularity of each Debian package. Users may elect to participate, in which case the list of their installed packages is transmitted back to Debian. These lists are tallied as votes. So, thanks to popcon, we can get a vote tally for each package.

==== 2. THE PROBLEM ====

This program solves the following problem:

Input: A list of Debian packages, each with a size (in kilobytes) and number of votes, both of which are integers; and an integer `W` representing the capacity of the install media (in kilobytes).

Output: The set of packages with total size `≤ W` , such that the total number of package votes is maximized.

In terms of the knapsack problem, the Debian package votes correspond to knapsack values, and our Debian package sizes correspond to knapsack weights.

==== 3. THE INPUT ====

The following file (knapsack_packages.txt) contains the name, size, and votes for approximately 36,000 binary Debian packages that are currently available on the amd64 platform, and have popcon information.

The file obeys the following format:

```[ number of packages ] [ name 0 ] [ space ] [ votes 0 ] [ space ] [ size 0 ] [ newline ] [ name 1 ] [ space ] [ votes 1 ] [ space ] [ size 1 ] [ newline ] [ name 2 ] [ space ] [ votes 2 ] [ space ] [ size 2 ] [ newline ] ... ```

The packages are arranged within the file in sorted order, from the largest number of votes to the smallest.

==== 4. FLOW OF CONTROL ====

A test harness program is created which executes the above function and measures the elapsed time of the code corresponding to the algorithm in question. The test program will perform the following steps:

```1. Load the "knapsack_packages.txt" file into memory. 2. Input size "n" and weight "W" as variables. 3. Execute the exhaustive search knapsack algorithm. The output of this algorithm should be the best combination sequence of packages. 4. Print the solution. A solution is the best sequence of packages, displaying the name of the package, the votes, and the size of each package, as well as the total votes and total size of the entire solution. ```

A knapsack problem instances is created of varying input sizes “n” by using the first “n” entries in the file knapsack_packages.txt. In other words, to create a problem instance with `n = 100`, only use the first 100 packages listed in the file as input.

==== 5. TEST HARNESS ====

Note: This program uses a custom Timer class (Timer.h). To obtain code for that class, click here.

“Project2.h” is listed below.

``` Exhaustive Knapsack Test Harness C++ // ============================================================================= // Author: K Perkins // Date: Nov 2, 2013 // Taken From: http://programmingnotes.org/ // File: main.cpp // Description: This is the test harness program which runs the code and // measures the elapsed time of the code corresponding to the algorithm // in question. This test program will reduce the problem of selecting // which Debian Linux packages to include on installation media, using the // knapsack problem. An exhaustive search algorithm is used, and a measure // of its performance is recorded. // ============================================================================= #include <iostream> #include <cstdlib> #include <cassert> #include <fstream> #include <string> #include <vector> #include "Timer.h" #include "Project2.h" using namespace std; // the maximum weight const int MAX_WEIGHT = 65536; // the number of packages to examine const int NUM_PACKAGES = 24; int main() { // declare variables Timer timer; Project2 proj2; ifstream infile; PackageStats access; vector<PackageStats> packages; vector<PackageStats> bestCombo; int totPackages = 0; // open file & make sure it exists infile.open("knapsack_packages.txt"); if(infile.fail()) { cerr<<"nCant find file!n"; exit(1); } // get the total number of packages from the file infile >> totPackages; // make sure there are enough packages in the file assert(NUM_PACKAGES <= totPackages); // display stats cerr<<"n = "<<NUM_PACKAGES<<", W = "<<MAX_WEIGHT <<"nn-- Exhaustive Search Solution --nn"; // get the remaining info from the file // std::string int int for(int x=0; (infile >> access.name >> access.votes >> access.size) && x < NUM_PACKAGES; ++x) { packages.push_back(access); } infile.close(); // start the timer timer.Start(); // find the best knapsack subset solution bestCombo = proj2.ExhaustiveKnapsack(packages, MAX_WEIGHT); // stop the timer timer.Stop(); // display the best solution packages proj2.Display(bestCombo, bestCombo.size()); // display the size and total votes cout<<"nTotal Size = "<<proj2.TotalSize(bestCombo)<<" -- Total Votes = " <<proj2.TotalVotes(bestCombo)<<endl; // display the elapsed time cout<<"nIt took "<<timer.Elapsed()*1000 <<" clicks ("<<timer.Elapsed()<<" seconds)"<<endl; return 0; }// http://programmingnotes.org/ 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 // =============================================================================//     Author: K Perkins//     Date:   Nov 2, 2013//     Taken From: http://programmingnotes.org///     File:  main.cpp//     Description: This is the test harness program which runs the code and //       measures the elapsed time of the code corresponding to the algorithm //       in question. This test program will reduce the problem of selecting //       which Debian Linux packages to include on installation media, using the //       knapsack problem. An exhaustive search algorithm is used, and a measure //       of its performance is recorded.// =============================================================================#include <iostream>#include <cstdlib>#include <cassert>#include <fstream>#include <string>#include <vector>#include "Timer.h"#include "Project2.h"using namespace std; // the maximum weightconst int MAX_WEIGHT = 65536; // the number of packages to examineconst int NUM_PACKAGES = 24; int main(){ // declare variables Timer timer; Project2 proj2; ifstream infile; PackageStats access; vector<PackageStats> packages; vector<PackageStats> bestCombo; int totPackages = 0;  // open file & make sure it exists infile.open("knapsack_packages.txt"); if(infile.fail()) { cerr<<"nCant find file!n"; exit(1); }  // get the total number of packages from the file infile >> totPackages; // make sure there are enough packages in the file assert(NUM_PACKAGES <= totPackages); // display stats cerr<<"n = "<<NUM_PACKAGES<<", W = "<<MAX_WEIGHT <<"nn-- Exhaustive Search Solution --nn"; // get the remaining info from the file //                      std::string        int             int for(int x=0; (infile >> access.name >> access.votes >> access.size) && x < NUM_PACKAGES; ++x) { packages.push_back(access); } infile.close();  // start the timer timer.Start(); // find the best knapsack subset solution bestCombo = proj2.ExhaustiveKnapsack(packages, MAX_WEIGHT); // stop the timer timer.Stop();  // display the best solution packages proj2.Display(bestCombo, bestCombo.size());  // display the size and total votes cout<<"nTotal Size = "<<proj2.TotalSize(bestCombo)<<" -- Total Votes = " <<proj2.TotalVotes(bestCombo)<<endl;  // display the elapsed time cout<<"nIt took "<<timer.Elapsed()*1000 <<" clicks ("<<timer.Elapsed()<<" seconds)"<<endl;  return 0;}// http://programmingnotes.org/ ```

==== 6. THE ALGORITHMS – “include Project2.h” ====

``` Knapsack Problem Using Exhaustive Search C++ // ============================================================================= // Author: K Perkins // Date: Nov 2, 2013 // Taken From: http://programmingnotes.org/ // File: Project2.h // Description: This is a simple class which holds the functions for // project 2 // ============================================================================= #ifndef PROJECT2_H #define PROJECT2_H #include <iostream> #include <vector> #include <string> using std::vector; using std::string; // structure to hold the items contained in the file struct PackageStats { string name; int votes; int size; }; class Project2 { public: Project2(){} vector<PackageStats> ExhaustiveKnapsack(vector<PackageStats>& packages, int weight) { vector<PackageStats> best; int* subsets = new int[packages.size()]; int bestVote = 0; // generate subsets (2^n possibilities) for(unsigned x=0; x < (unsigned)(1 << (int)packages.size()); ++x) { int index = 0; int totalSize = 0; int totalVotes = 0; // generate subsets using binary digits for(unsigned y=0; y < packages.size(); ++y) { if(((x >> y) & 1) == 1) { subsets[index++] = y; totalSize += packages.at(y).size; totalVotes += packages.at(y).votes; } } // find the best combination of subsets if((totalSize <= weight) && (best.empty() || (totalVotes > bestVote))) { bestVote = totalVotes; best = ReturnBest(packages, subsets, index); } } delete[] subsets; return best; }// end of ExhaustiveKnapsack int TotalSize(vector<PackageStats>& packages, vector<int> s = vector<int>()) { int total = 0; // if theres 2 parameters if(!s.empty()) { for(unsigned x=0; x < s.size(); ++x) { total += packages.at(s.at(x)).size; } } else // if theres only 1 { for(unsigned x=0; x < packages.size(); ++x) { total += packages.at(x).size; } } return total; }// end of TotalSize int TotalVotes(vector<PackageStats>& packages, vector<int> s = vector<int>()) { int total = 0; // if theres 2 parameters if(!s.empty()) { for(unsigned x=0; x < s.size(); ++x) { total += packages.at(s.at(x)).votes; } } else // if theres only 1 { for(unsigned x=0; x < packages.size(); ++x) { total += packages.at(x).votes; } } return total; }// end of TotalVotes vector<PackageStats> ReturnBest(vector<PackageStats>& packages, int subsets[], int size) { vector<PackageStats> best; for(int x=0; x < size; ++x) { best.push_back(packages.at(subsets[x])); } return best; }// end of ReturnBest void Display(vector<PackageStats>& packages, unsigned size) { for(unsigned x=0; x < size && x < packages.size(); ++x) { std::cout<<packages.at(x).name<<" " <<packages.at(x).size<<" "<<packages.at(x).votes<<std::endl; } }// end of Display ~Project2(){} }; #endif // http://programmingnotes.org/ 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 // =============================================================================//     Author: K Perkins//     Date:   Nov 2, 2013//     Taken From: http://programmingnotes.org///     File:  Project2.h//     Description: This is a simple class which holds the functions for //       project 2// =============================================================================#ifndef PROJECT2_H#define PROJECT2_H #include <iostream>#include <vector>#include <string>using std::vector;using std::string; // structure to hold the items contained in the filestruct PackageStats{ string name; int votes; int size;}; class Project2{public: Project2(){} vector<PackageStats> ExhaustiveKnapsack(vector<PackageStats>& packages, int weight) { vector<PackageStats> best; int* subsets = new int[packages.size()]; int bestVote = 0;  // generate subsets (2^n possibilities) for(unsigned x=0; x < (unsigned)(1 << (int)packages.size()); ++x) { int index = 0; int totalSize = 0; int totalVotes = 0; // generate subsets using binary digits for(unsigned y=0; y < packages.size(); ++y) { if(((x >> y) & 1) == 1) { subsets[index++] = y; totalSize += packages.at(y).size; totalVotes += packages.at(y).votes; } } // find the best combination of subsets if((totalSize <= weight) && (best.empty() || (totalVotes > bestVote))) { bestVote = totalVotes; best = ReturnBest(packages, subsets, index); } } delete[] subsets; return best; }// end of ExhaustiveKnapsack  int TotalSize(vector<PackageStats>& packages, vector<int> s = vector<int>()) { int total = 0;  // if theres 2 parameters if(!s.empty()) { for(unsigned x=0; x < s.size(); ++x) { total += packages.at(s.at(x)).size; } } else // if theres only 1 { for(unsigned x=0; x < packages.size(); ++x) { total += packages.at(x).size; } } return total; }// end of TotalSize  int TotalVotes(vector<PackageStats>& packages, vector<int> s = vector<int>()) { int total = 0;  // if theres 2 parameters if(!s.empty()) { for(unsigned x=0; x < s.size(); ++x) { total += packages.at(s.at(x)).votes; } } else // if theres only 1 { for(unsigned x=0; x < packages.size(); ++x) { total += packages.at(x).votes; } } return total; }// end of TotalVotes  vector<PackageStats> ReturnBest(vector<PackageStats>& packages, int subsets[], int size) { vector<PackageStats> best; for(int x=0; x < size; ++x) { best.push_back(packages.at(subsets[x])); } return best; }// end of ReturnBest void Display(vector<PackageStats>& packages, unsigned size) { for(unsigned x=0; x < size && x < packages.size(); ++x) { std::cout<<packages.at(x).name<<" "      <<packages.at(x).size<<" "<<packages.at(x).votes<<std::endl; } }// end of Display ~Project2(){}};#endif // http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

NOTE: Looking for the Dynamic Programming version to this problem? Click here!

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Note: Dont forget to include the input file!

The following is sample output:

`n = 24, W = 65536`

``` -- Exhaustive Search Solution -- debianutils 90 128329 libgcc1 46 128327 dpkg 2672 128294 perl-base 1969 127369 debconf 168 121503 grep 595 121426 gzip 142 121346 login 980 121332 coreutils 6505 121240 bash 1673 121229 sed 261 121220 findutils 805 121173 lsb-base 26 121121 sysv-rc 80 121092 sysvinit-utils 116 121078 base-files 65 121072 initscripts 93 121037 util-linux 846 121022 mount 272 120961 libselinux1 109 120918 cron 120 120872 base-passwd 49 120856 apt 1356 120826 tzdata 488 120813 Total Size = 19526 -- Total Votes = 2934456 ```

```It took 19140 clicks (19.14 seconds) ```