Question: Need help with C++ assignment. The directions are write an 'all hares' version of the bubble sort. Instead of always looping through the list

Need help with C++ assignment. The directions are " write an 'all hares' version of the bubble sort. Instead of always looping through the list from lowest to highest you will alternate. The first pass will go from lowest to highest and the next pass will go from highest to lowest. You will use a class called SortTester that will create a list and has built in compare and swap functions. You need to call the compare and swap functions provided for the code to work correctly. You are being tested on your algorithm. You are going to fill in the details for the function singleBubblePass(). The result of that function is to execute either a highest to lowest or lowest to highest single pass of a bubble sort (depending on if pass is odd or even). The test bench will test the correctness of the first 5 passes (8 pts for the first pass, 5pts for the second, 2 each for the next two), correctness of a fully sorted list at the end (8 pts), and there will be a bonus if you get all of those correct and are able to correctly identify a modification to do fewer comparison's than my basic version (5 pts)"

this is what I have so far:

main.cpp:

#include #include #include #include "SortTester.h" using namespace std; bool singleBubblePass(SortTester &tester, unsigned int size, unsigned int passNum) { bool sorted = true; // Insert your code here // // return sorted; } int main() { unsigned int size = 10; SortTester tester = SortTester(size); cout<<"Unsorted"<

SortTester.h:

#include #include class SortTester { private: std::vector testList; std::vector refList; unsigned int numCompares; public: SortTester(unsigned int numEntries); void swap(unsigned int a, unsigned int b); void swapRef(unsigned int a, unsigned int b); //returns positive number if a > b, 0 if a==b, and negative number if a < b int compare(unsigned int a, unsigned int b); int compareRef(unsigned int a, unsigned int b); void print(std::ofstream& oss); void printRef(std::ofstream& oss); void print(); void printRef(); unsigned int getNumCompares(); bool isSorted(std::ofstream& oss); bool doesPassMatch(std::ofstream& oss); void runPerformanceExtraCreditTest(unsigned numPasses); };

SortTester.cpp:

#include #include #include "SortTester.h" using namespace std; SortTester::SortTester(unsigned int numEntries) { numCompares = 0; srand(time(NULL)); for (unsigned int i=0; i < numEntries; i++ ) { testList.push_back( rand() % 100); refList.push_back( testList[i] ); } } void SortTester::print(ofstream& oss) { for (auto & it : testList) { oss< testList.size()) or (b > testList.size()) or (a<0) or (b<0) ) { cout<<"Invalid swap request of "< refList.size()) or (b > refList.size()) or (a<0) or (b<0) ) { cout<<"Invalid swap request of "< testList[i+1] ) { sorted = false; } } if ( sorted ) { return true; } //implied else print(oss); return false; } bool SortTester::doesPassMatch(std::ofstream& oss) { bool listsMatch = true; for (unsigned int i=0; i< testList.size(); i++) { if (testList[i] != refList[i]) { listsMatch = false; } } //print Passed if match if (listsMatch) { return true; } //implied else oss<<"Match Test FAILED "; oss<<"Expected "; printRef(oss); oss<<"Actual "; print(oss); return false; } unsigned int SortTester::getNumCompares() { return numCompares; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!