add test cases

This commit is contained in:
Krishna Vedala 2020-07-18 18:02:43 -04:00
parent ea1dab8f90
commit 30c9a199ad
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7

View File

@ -1,4 +1,4 @@
/* /**
* @file * @file
* @brief Implementation Details * @brief Implementation Details
* @details Quick sort 3 works on Dutch National Flag Algorithm * @details Quick sort 3 works on Dutch National Flag Algorithm
@ -56,7 +56,7 @@ void partition3(std::array<T, N> *arr, int low, int high, int *i, int *j) {
} }
int mid = low; int mid = low;
int pivot = arr[high]; T pivot = (*arr)[high];
while (mid <= high) { while (mid <= high) {
if ((*arr)[mid] < pivot) { if ((*arr)[mid] < pivot) {
std::swap((*arr)[low++], (*arr)[mid++]); std::swap((*arr)[low++], (*arr)[mid++]);
@ -92,7 +92,7 @@ void quicksort(std::array<T, N> *arr, int low, int high) {
template <typename T, size_t N> template <typename T, size_t N>
std::array<T, N> quicksort(std::array<T, N> arr, int low, int high) { std::array<T, N> quicksort(std::array<T, N> arr, int low, int high) {
if (low >= high) { // 1 or 0 elements if (low >= high) { // 1 or 0 elements
return; return arr;
} }
int i, j; int i, j;
@ -108,18 +108,24 @@ std::array<T, N> quicksort(std::array<T, N> arr, int low, int high) {
} }
} // namespace sorting } // namespace sorting
// Driver program for above functions /** Test function */
int main() { static void test() {
int size, i; constexpr int size = 8;
std::cout << "Enter Number of elements\n";
std::cin >> size;
std::array<int, size> arr{}; std::array<int, size> arr{};
for (auto &a : arr) { for (auto &a : arr) {
a = std::rand() % 100 - 50; // random numbers between -50, 49 a = std::rand() % 100 - 50; // random numbers between -50, 49
} }
std::cout << "Test 1.... ";
std::array<int, size> sorted = sorting::quicksort(arr, 0, size - 1); std::array<int, size> sorted = sorting::quicksort(arr, 0, size - 1);
std::cout << "Sorted Array is:\n\t"; std::cout << "Sorted Array is:\n\t";
std::cout << sorted << "\n"; std::cout << sorted << "\n";
assert(std::is_sorted(std::begin(sorted), std::end(sorted)));
std::cout << "passed\n";
}
/** Driver program for above functions */
int main() {
test();
return 0; return 0;
} }