mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Added quick sort algorithm
This commit is contained in:
@@ -68,9 +68,15 @@ std::vector<T> getAllFib(const T num);
|
|||||||
//This is a function that performs a bubble sort on a vector
|
//This is a function that performs a bubble sort on a vector
|
||||||
template <class T>
|
template <class T>
|
||||||
void bubbleSort(std::vector<T>& ary);
|
void bubbleSort(std::vector<T>& ary);
|
||||||
//This is a function that perfomrs a quick sort on a vector
|
//This is a function that makes quick sort easier to start
|
||||||
template <class T>
|
template <class T>
|
||||||
void quickSort(std::vector<T>& ary, int bottom = -1, int top = -1);
|
void quickSort(std::vector<T>& ary);
|
||||||
|
//This is the function that actually performs the quick sort on the vector
|
||||||
|
template <class T>
|
||||||
|
void quickSort(std::vector<T>& ary, int64_t bottom, int64_t top);
|
||||||
|
//This is a helper function for quickSort. It chooses a pivot element and sorts everything to larger or smaller than the pivot. Returns location of pivot
|
||||||
|
template <class T>
|
||||||
|
int64_t partition(std::vector<T>& ary, int64_t bottom, int64_t top);
|
||||||
//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)
|
//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>
|
template <class T>
|
||||||
int64_t search(const std::vector<T>& ary, T num);
|
int64_t search(const std::vector<T>& ary, T num);
|
||||||
@@ -311,7 +317,7 @@ T getFib(const T num){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//This function returns a vector that includes all Fibonacci numbers <= 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){
|
std::vector<T> getAllFib(const T num){
|
||||||
std::vector<T> fibList;
|
std::vector<T> fibList;
|
||||||
//Make sure the number is within bounds
|
//Make sure the number is within bounds
|
||||||
@@ -340,7 +346,7 @@ std::vector<T> getAllFib(const T num){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//This is a function that performs a bubble sort on a vector
|
//This is a function that performs a bubble sort on a vector
|
||||||
template<class T>
|
template <class T>
|
||||||
void bubbleSort(std::vector<T>& ary){
|
void bubbleSort(std::vector<T>& ary){
|
||||||
bool notFinished = true; //A flag to determine if the loop is finished
|
bool notFinished = true; //A flag to determine if the loop is finished
|
||||||
for(int numLoops = 0;numLoops < ary.size();++numLoops){ //Loop until you finish
|
for(int numLoops = 0;numLoops < ary.size();++numLoops){ //Loop until you finish
|
||||||
@@ -355,14 +361,55 @@ void bubbleSort(std::vector<T>& ary){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//This is a function that perfomrs a quick sort on a vector
|
//This is a function that makes quick sort easier to start
|
||||||
template<class T>
|
template <class T>
|
||||||
void quickSort(std::vector<T>& nums, int bottom, int top){
|
void quickSort(std::vector<T>& ary){
|
||||||
|
//Call the other quickSort function with all the necessary info
|
||||||
|
quickSort(ary, 0, ary.size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//This is the function that actually performs the quick sort on the vector
|
||||||
|
template <class T>
|
||||||
|
void quickSort(std::vector<T>& ary, int64_t bottom, int64_t top){
|
||||||
|
//Make sure you have a valid slice of the vector
|
||||||
|
if(bottom < top){
|
||||||
|
//Get the pivot location
|
||||||
|
int64_t pivot = partition(ary, bottom, top);
|
||||||
|
|
||||||
|
//Sort all element less than the pivot
|
||||||
|
quickSort(ary, bottom, pivot - 1);
|
||||||
|
//Sort all element greater than the pivot
|
||||||
|
quickSort(ary, pivot + 1, top);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//This is a helper function for quickSort. It chooses a pivot element and sorts everything to larger or smaller than the pivot. Returns location of pivot
|
||||||
|
template <class T>
|
||||||
|
int64_t partition(std::vector<T>& ary, int64_t bottom, int64_t top){
|
||||||
|
int64_t pivot = ary.at(top); //Pick a pivot element
|
||||||
|
int64_t smaller = bottom - 1; //Keep track of where all elements are smaller than the pivot
|
||||||
|
|
||||||
|
//Loop through every element in the vector testing if it is smaller than pivot
|
||||||
|
for(int64_t cnt = bottom;cnt < top;++cnt){
|
||||||
|
//If the element is smaller than pivot move it to the correct location
|
||||||
|
if(ary.at(cnt) < pivot){
|
||||||
|
//Increment the tracker for elements smaller than pivot
|
||||||
|
++smaller;
|
||||||
|
//Swap the current element to the correct location for being smaller than the pivot
|
||||||
|
std::swap(ary.at(smaller), ary.at(cnt));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Move the pivot element to the correct location
|
||||||
|
++smaller;
|
||||||
|
std::swap(ary.at(top), ary.at(smaller));
|
||||||
|
|
||||||
|
//Return the pivot element
|
||||||
|
return smaller;
|
||||||
}
|
}
|
||||||
|
|
||||||
//This is a function that performs a search on a vector and returns the subscript of the item being searched for
|
//This is a function that performs a search on a vector and returns the subscript of the item being searched for
|
||||||
template<class T>
|
template <class T>
|
||||||
int64_t search(const std::vector<T>& ary, T num){
|
int64_t search(const std::vector<T>& ary, T num){
|
||||||
int64_t subscript = 0; //Start with the subscript at 0
|
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
|
//Step through every element in the vector and return the subscript if you find the correct element
|
||||||
@@ -379,7 +426,7 @@ int64_t search(const std::vector<T>& ary, T num){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//This function finds the smallest element in a vector
|
//This function finds the smallest element in a vector
|
||||||
template<class T>
|
template <class T>
|
||||||
T findMin(const std::vector<T>& ary){
|
T findMin(const std::vector<T>& ary){
|
||||||
T min; //For the smallest element
|
T min; //For the smallest element
|
||||||
|
|
||||||
@@ -401,7 +448,7 @@ T findMin(const std::vector<T>& ary){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//This function finds the largest element in a vector
|
//This function finds the largest element in a vector
|
||||||
template<class T>
|
template <class T>
|
||||||
T findMax(const std::vector<T>& ary){
|
T findMax(const std::vector<T>& ary){
|
||||||
T max; //For the largest element
|
T max; //For the largest element
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user