diff --git a/Algorithms.hpp b/Algorithms.hpp index c2956dd..d6c2656 100644 --- a/Algorithms.hpp +++ b/Algorithms.hpp @@ -68,9 +68,15 @@ std::vector getAllFib(const T num); //This is a function that performs a bubble sort on a vector template void bubbleSort(std::vector& 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 -void quickSort(std::vector& ary, int bottom = -1, int top = -1); +void quickSort(std::vector& ary); +//This is the function that actually performs the quick sort on the vector +template +void quickSort(std::vector& 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 +int64_t partition(std::vector& 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) template int64_t search(const std::vector& ary, T num); @@ -311,7 +317,7 @@ T getFib(const T num){ } //This function returns a vector that includes all Fibonacci numbers <= num -template +template std::vector getAllFib(const T num){ std::vector fibList; //Make sure the number is within bounds @@ -340,7 +346,7 @@ std::vector getAllFib(const T num){ } //This is a function that performs a bubble sort on a vector -template +template void bubbleSort(std::vector& ary){ bool notFinished = true; //A flag to determine if the loop is finished for(int numLoops = 0;numLoops < ary.size();++numLoops){ //Loop until you finish @@ -355,14 +361,55 @@ void bubbleSort(std::vector& ary){ } } -//This is a function that perfomrs a quick sort on a vector -template -void quickSort(std::vector& nums, int bottom, int top){ - +//This is a function that makes quick sort easier to start +template +void quickSort(std::vector& 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 +void quickSort(std::vector& 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 +int64_t partition(std::vector& 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 -template +template int64_t search(const std::vector& 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 @@ -379,7 +426,7 @@ int64_t search(const std::vector& ary, T num){ } //This function finds the smallest element in a vector -template +template T findMin(const std::vector& ary){ T min; //For the smallest element @@ -401,7 +448,7 @@ T findMin(const std::vector& ary){ } //This function finds the largest element in a vector -template +template T findMax(const std::vector& ary){ T max; //For the largest element