mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Added bubblesort, quicksort, and search
This commit is contained in:
@@ -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<boolFn> functions {testGetPrimes, tetsGetDivisors, testGetSum, testIsFound, testGetPermutations, testGetFib};
|
||||
std::vector<std::string> names {"testGetPrimes", "testGetDivisors", "testGetSum", "testIsFound", "testGetPermutations", "testGetFib"};
|
||||
std::vector<boolFn> functions {testGetPrimes, tetsGetDivisors, testGetSum, testIsFound, testGetPermutations, testGetFib, testBubbleSort, testQuickSort, testSearch};
|
||||
std::vector<std::string> 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
|
||||
|
||||
@@ -37,6 +37,15 @@ std::vector<std::string> 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<class T>
|
||||
bool bubbleSort(std::vector<T> nums);
|
||||
//This is a function that perfomrs a quick sort on a vector
|
||||
template<class T>
|
||||
bool quickSort(std::vector<T> 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<class T>
|
||||
int64_t search(std::vector<T> nums, T num);
|
||||
|
||||
|
||||
template<class T>
|
||||
@@ -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<class T>
|
||||
bool bubbleSort(std::vector<T> 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<class T>
|
||||
bool quickSort(std::vector<T> nums){
|
||||
|
||||
}
|
||||
|
||||
//This is a function that performs a search on a vector and returns the subscript of the item being searched for
|
||||
template<class T>
|
||||
int64_t search(std::vector<T> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user