Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Function: bubbleSort
- *
- * Recursively sorts an array of strings using the bubble sort algorithm
- *
- * Parameters:
- * std::string strArray[]
- * std::size_t size
- */
- void bubbleSort(std::string strArray[], std::size_t size)
- {
- // base case
- if (size == 1)
- return;
- // to check if swap was made after each pass
- bool swapMade{ false };
- for (std::size_t i{ 0 }; i < size - 1; i++)
- {
- if (strArray[i] > strArray[i + 1])
- {
- swap(strArray[i], strArray[i + 1]);
- swapMade = true;
- }
- }
- // if no swap was made, array is sorted
- if (!swapMade)
- return;
- bubbleSort(strArray, size - 1);
- }
- /*
- * Function: swap
- *
- * Swaps the value stored at two string pointers
- *
- * Parameters:
- * std::string* strPtrOne
- * std::string* strPtrTwo
- */
- void swap(std::string* strPtrOne, std::string* strPtrTwo)
- {
- std::string temp;
- *strPtrOne = temp;
- *strPtrOne = *strPtrTwo;
- *strPtrTwo = temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement