mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Updated to be more standardized
This commit is contained in:
133
Algorithms.hpp
133
Algorithms.hpp
@@ -28,13 +28,6 @@
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
//This library is licensed under lgplv3
|
||||
//You can find more information at gmplib.org
|
||||
//In order to use this functionality you must use the -DNEEDGMP flag in your compiler
|
||||
//You must also link both libgmpxx and libgmp
|
||||
#ifdef NEEDGMP
|
||||
#include <gmpxx.h> //This is necessary for the getGmpFib function for numbers larger than a normal int can hold. It can be commented out if needed
|
||||
#endif //NEEDGMP
|
||||
|
||||
namespace mee{
|
||||
|
||||
@@ -42,54 +35,55 @@ namespace mee{
|
||||
//A list of functions in the file
|
||||
//Also works as prototypes with general information
|
||||
//This is a function that returns all the primes <= goalNumber and returns a vector with those prime numbers
|
||||
template<class T>
|
||||
template <class T>
|
||||
std::vector<T> getPrimes(T goalNumber);
|
||||
//This function returns a vector with a specific number of primes
|
||||
template<class T>
|
||||
template <class T>
|
||||
std::vector<T> getNumPrimes(T numberOfPrimes);
|
||||
//This function returns all prime factors of a number
|
||||
template<class T>
|
||||
template <class T>
|
||||
std::vector<T> getFactors(T goalNumber);
|
||||
//This is a function that gets all the divisors of num and returns a vector containing the divisors
|
||||
template<class T>
|
||||
template <class T>
|
||||
std::vector<T> getDivisors(T num);
|
||||
//This is a function that returns the sum of all elements in a vector
|
||||
template <class T>
|
||||
T getSum(std::vector<T> numbers);
|
||||
T getSum(const std::vector<T>& numbers);
|
||||
//This is a function that returns the product of all elements in a vector
|
||||
template <class T>
|
||||
T getProduct(std::vector<T> nums);
|
||||
//This is a function that searches a vecter for an element. Returns true if num is found in list
|
||||
template<class T>
|
||||
bool isFound(T num, std::vector<T> list);
|
||||
T getProduct(const std::vector<T>& nums);
|
||||
//This is a function that searches a vecter for an element. Returns true if the key is found in list
|
||||
template <class T>
|
||||
bool isFound(std::vector<T> ary, T key);
|
||||
//This is a function that creates all permutations of a string and returns a vector of those permutations.
|
||||
//It is meant to have only the string passed into it from the calling function. num is used for recursion purposes
|
||||
//It can however be used with num if you want the first num characters to be stationary
|
||||
std::vector<std::string> getPermutations(std::string master, int num = 0);
|
||||
//These functions return the numth Fibonacci number
|
||||
template <class T>
|
||||
T getFib(T num);
|
||||
T getFib(const T num);
|
||||
//This function returns a vector that includes all Fibonacci numbers <= num
|
||||
template<class T>
|
||||
template <class T>
|
||||
std::vector<T> getAllFib(const T num);
|
||||
//This is a function that performs a bubble sort on a vector
|
||||
template<class T>
|
||||
bool bubbleSort(std::vector<T> nums);
|
||||
template <class T>
|
||||
void bubbleSort(std::vector<T>& ary);
|
||||
//This is a function that perfomrs a quick sort on a vector
|
||||
template<class T>
|
||||
bool quickSort(std::vector<T> nums);
|
||||
template <class T>
|
||||
void quickSort(std::vector<T>& ary, int bottom = -1, int top = -1);
|
||||
//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>
|
||||
int64_t search(const std::vector<T>& ary, T num);
|
||||
//This function finds the smallest element in a vector
|
||||
template<class T>
|
||||
T findMin(std::vector<T> arr);
|
||||
template <class T>
|
||||
T findMin(const std::vector<T>& ary);
|
||||
//This function finds the largest element in a vector
|
||||
template<class T>
|
||||
T findMax(std::vector<T> arr);
|
||||
template <class T>
|
||||
T findMax(const std::vector<T>& ary);
|
||||
|
||||
|
||||
template<class T>
|
||||
//This is a function that returns all the primes <= goalNumber and returns a vector with those prime numbers
|
||||
template <class T>
|
||||
std::vector<T> getPrimes(T goalNumber){
|
||||
std::vector<T> primes;
|
||||
bool foundFactor = false;
|
||||
@@ -124,7 +118,8 @@ std::vector<T> getPrimes(T goalNumber){
|
||||
return primes;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
//This function returns a vector with a specific number of primes
|
||||
template <class T>
|
||||
std::vector<T> getNumPrimes(T numberOfPrimes){
|
||||
std::vector<T> primes;
|
||||
primes.reserve(numberOfPrimes); //Saves cycles later
|
||||
@@ -165,7 +160,8 @@ std::vector<T> getNumPrimes(T numberOfPrimes){
|
||||
return primes;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
//This function returns all prime factors of a number
|
||||
template <class T>
|
||||
std::vector<T> getFactors(T goalNumber){
|
||||
//Get all the prime numbers up to sqrt(number). If there is a prime < goalNumber it will have to be <= sqrt(goalNumber)
|
||||
std::vector<T> primes = getPrimes((T)ceil(sqrt(goalNumber))); //Make sure you are getting a vector of the correct type
|
||||
@@ -193,7 +189,8 @@ std::vector<T> getFactors(T goalNumber){
|
||||
return factors;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
//This is a function that gets all the divisors of num and returns a vector containing the divisors
|
||||
template <class T>
|
||||
std::vector<T> getDivisors(T num){
|
||||
std::vector<T> divisors; //Holds the number of divisors
|
||||
//Ensure the parameter is a valid number
|
||||
@@ -230,34 +227,36 @@ std::vector<T> getDivisors(T num){
|
||||
|
||||
//This is a function that returns the sum of all elements in a vector
|
||||
template <class T>
|
||||
T getSum(std::vector<T> numbers){
|
||||
T getSum(const std::vector<T>& ary){
|
||||
T sum = 0;
|
||||
for(unsigned int cnt = 0;cnt < numbers.size();++cnt){
|
||||
sum += numbers.at(cnt);
|
||||
for(unsigned int cnt = 0;cnt < ary.size();++cnt){
|
||||
sum += ary.at(cnt);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
//This is a function that returns the product of all elmements in a vector
|
||||
template <class T>
|
||||
T getProduct(std::vector<T> nums){
|
||||
T getProduct(const std::vector<T>& ary){
|
||||
T prod = 1;
|
||||
for(T cnt = 0;cnt < nums.size();++cnt){
|
||||
prod *= nums.at(cnt);
|
||||
for(T cnt = 0;cnt < ary.size();++cnt){
|
||||
prod *= ary.at(cnt);
|
||||
}
|
||||
return prod;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool isFound(T num, std::vector<T> list){
|
||||
for(int cnt = 0;cnt < list.size();++cnt){
|
||||
if(list.at(cnt) == num){
|
||||
//This is a function that searches a vecter for an element. Returns true if they key is found in list
|
||||
template <class T>
|
||||
bool isFound(std::vector<T> ary, T key){
|
||||
for(int cnt = 0;cnt < ary.size();++cnt){
|
||||
if(ary.at(cnt) == key){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//This is a function that creates all permutations of a string and returns a vector of those permutations.
|
||||
std::vector<std::string> getPermutations(std::string master, int num){
|
||||
std::vector<std::string> perms;
|
||||
//Check if the number is out of bounds
|
||||
@@ -289,8 +288,9 @@ std::vector<std::string> getPermutations(std::string master, int num){
|
||||
return perms;
|
||||
}
|
||||
|
||||
//These functions return the numth Fibonacci number
|
||||
template <class T>
|
||||
T getFib(T num){
|
||||
T getFib(const T num){
|
||||
//Make sure the number is within bounds
|
||||
if(num <= 2){
|
||||
return 1;
|
||||
@@ -301,7 +301,7 @@ T getFib(T num){
|
||||
tempNums[0] = tempNums[1] = 1;
|
||||
|
||||
//Do the calculation
|
||||
uint64_t cnt;
|
||||
unsigned int cnt;
|
||||
for(cnt = 2;(cnt < num) && (tempNums[(cnt - 1) % 3] >= tempNums[(cnt - 2) % 3]);++cnt){
|
||||
tempNums[cnt % 3] = tempNums[(cnt + 1) % 3] + tempNums[(cnt + 2) % 3];
|
||||
}
|
||||
@@ -341,14 +341,14 @@ std::vector<T> getAllFib(const T num){
|
||||
|
||||
//This is a function that performs a bubble sort on a vector
|
||||
template<class T>
|
||||
bool bubbleSort(std::vector<T> nums){
|
||||
void bubbleSort(std::vector<T>& ary){
|
||||
bool notFinished = true; //A flag to determine if the loop is finished
|
||||
for(int numLoops = 0;notFinished;++numLoops){ //Loop until you finish
|
||||
for(int numLoops = 0;numLoops < ary.size();++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));
|
||||
for(int cnt = 1;cnt < (ary.size() - numLoops);++cnt){ //use size - 1 to make sure you don't go out of bounds
|
||||
if(ary.at(cnt) < ary.at(cnt - 1)){
|
||||
std::swap(ary.at(cnt), ary.at(cnt - 1));
|
||||
notFinished = true;
|
||||
}
|
||||
}
|
||||
@@ -357,19 +357,22 @@ 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){
|
||||
void quickSort(std::vector<T>& nums, int bottom, int top){
|
||||
|
||||
}
|
||||
|
||||
//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 search(const std::vector<T>& ary, 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){
|
||||
while(subscript < ary.size()){
|
||||
if(ary.at(subscript) == num){
|
||||
return subscript;
|
||||
}
|
||||
else{
|
||||
++subscript;
|
||||
}
|
||||
}
|
||||
//If you cannot find the element return -1
|
||||
return -1;
|
||||
@@ -377,18 +380,18 @@ int64_t search(std::vector<T> nums, T num){
|
||||
|
||||
//This function finds the smallest element in a vector
|
||||
template<class T>
|
||||
T findMin(std::vector<T> arr){
|
||||
T findMin(const std::vector<T>& ary){
|
||||
T min; //For the smallest element
|
||||
|
||||
//Make sure the vector is not empty
|
||||
if(arr.size() > 0){
|
||||
if(ary.size() > 0){
|
||||
//Use the first element as the smallest element
|
||||
min = arr.at(0);
|
||||
min = ary.at(0);
|
||||
//Run through every element in the vector, checking it against the current minimum
|
||||
for(int cnt = 1;cnt < arr.size();++cnt){
|
||||
for(int cnt = 1;cnt < ary.size();++cnt){
|
||||
//If the current element is smaller than the minimum, make it the new minimum
|
||||
if(arr.at(cnt) < min){
|
||||
min = arr.at(cnt);
|
||||
if(ary.at(cnt) < min){
|
||||
min = ary.at(cnt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -399,18 +402,18 @@ T findMin(std::vector<T> arr){
|
||||
|
||||
//This function finds the largest element in a vector
|
||||
template<class T>
|
||||
T findMax(std::vector<T> arr){
|
||||
T findMax(const std::vector<T>& ary){
|
||||
T max; //For the largest element
|
||||
|
||||
//Make sure the vector is not empty
|
||||
if(arr.size() > 0){
|
||||
if(ary.size() > 0){
|
||||
//Use the first element as the largest element
|
||||
max = arr.at(0);
|
||||
max = ary.at(0);
|
||||
//Run through every element in the vector, checking it against the current minimum
|
||||
for(int cnt = 1;cnt < arr.size();++cnt){
|
||||
for(int cnt = 1;cnt < ary.size();++cnt){
|
||||
//If the current element is larger than the maximum, make it the new maximum
|
||||
if(arr.at(cnt) > max){
|
||||
max = arr.at(cnt);
|
||||
if(ary.at(cnt) > max){
|
||||
max = ary.at(cnt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user