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:
@@ -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