diff --git a/Algorithms.cpp b/Algorithms.cpp index a350b70..b2197a4 100644 --- a/Algorithms.cpp +++ b/Algorithms.cpp @@ -24,13 +24,16 @@ bool testGetSum(); bool testIsFound(); bool testGetPermutations(); bool testGetFib(); +bool testBubbleSort(); +bool testQuickSort(); +bool testSearch(); int main(){ mee::Stopwatch timer; bool passedTest = false; - std::vector functions {testGetPrimes, tetsGetDivisors, testGetSum, testIsFound, testGetPermutations, testGetFib}; - std::vector names {"testGetPrimes", "testGetDivisors", "testGetSum", "testIsFound", "testGetPermutations", "testGetFib"}; + std::vector functions {testGetPrimes, tetsGetDivisors, testGetSum, testIsFound, testGetPermutations, testGetFib, testBubbleSort, testQuickSort, testSearch}; + std::vector names {"testGetPrimes", "testGetDivisors", "testGetSum", "testIsFound", "testGetPermutations", "testGetFib", "testBubbleSort", "testQuickSort", "testSearch"}; //Start doing tests and print out the results of each for(int cnt = 0;cnt < functions.size();++cnt){ @@ -167,6 +170,18 @@ bool testGetFib(){ return true; } +bool testBubbleSort(){ + +} + +bool testQuickSort(){ + +} + +bool testSearch(){ + +} + /* Results: Function testGetPrimes() passed the test The test took 0.013203 milliseconds diff --git a/Algorithms.hpp b/Algorithms.hpp index 129aa19..078f423 100644 --- a/Algorithms.hpp +++ b/Algorithms.hpp @@ -37,6 +37,15 @@ std::vector getPermutations(std::string master, int num = 0); //These functions return the numth Fibonacci number uint64_t getFib(uint64_t num); void getFib(mpz_t fibNum, uint64_t num); +//This is a function that performs a bubble sort on a vector +template +bool bubbleSort(std::vector nums); +//This is a function that perfomrs a quick sort on a vector +template +bool quickSort(std::vector nums); +//This is a function that performs a search on a vector and returns the subscript of the item being searched for (-1 if not found) +template +int64_t search(std::vector nums, T num); template @@ -185,6 +194,42 @@ mpz_class getMpzFib(uint64_t num){ return tempNums[(num - 1) % 3]; //Return the answer } +//This is a function that performs a bubble sort on a vector +template +bool bubbleSort(std::vector nums){ + bool notFinished = true; //A flag to determine if the loop is finished + for(int numLoops = 0;notFinished;++numLoops){ //Loop until you finish + notFinished = false; //Assume you are finished until you find an element out of order + //Loop through every element in the vector, moving the largest one to the end + for(int cnt = 0;cnt < ((nums.size() - 1) - numLoops);++cnt){ //use size - 1 to make sure you don't go out of bounds + if(nums.at(cnt) > nums.at(cnt + 1)){ + std::swap(nums.at(cnt), nums.at(cnt + 1)); + notFinished = true; + } + } + } +} + +//This is a function that perfomrs a quick sort on a vector +template +bool quickSort(std::vector nums){ + +} + +//This is a function that performs a search on a vector and returns the subscript of the item being searched for +template +int64_t search(std::vector nums, T num){ + int64_t subscript = 0; //Start with the subscript at 0 + //Step through every element in the vector and return the subscript if you find the correct element + while(subscript < nums.size()){ + if(nums.at(subscript) == num){ + return subscript; + } + } + //If you cannot find the element return -1 + return -1; +} + }