From 97161cf894dc547ad4b97bd1bc3af4e068a2c137 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Fri, 17 Jul 2020 21:26:06 -0400 Subject: [PATCH] added random values test --- sorting/gnome_sort.cpp | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/sorting/gnome_sort.cpp b/sorting/gnome_sort.cpp index 5d34aa9ee..b77bf9c3f 100644 --- a/sorting/gnome_sort.cpp +++ b/sorting/gnome_sort.cpp @@ -61,12 +61,12 @@ template std::array gnomeSort(std::array arr) { // few easy cases if (size <= 1) - return; + return arr; int index = 1; // initialize loop index while (index < size) { // check for swap - if (arr[index] >= arr[index - 1]) { + if ((index == 0) || (arr[index] >= arr[index - 1])) { index++; } else { std::swap(arr[index], arr[index - 1]); // swap @@ -89,16 +89,31 @@ static void test() { size); // pass array data as a C-style array pointer assert(std::is_sorted(std::begin(arr), std::end(arr))); std::cout << " Passed\n"; - // for (int i = 0; i < size; i++) std::cout << arr[i] << " "; + for (int i = 0; i < size; i++) std::cout << arr[i] << ", "; std::cout << std::endl; // Example 2. Creating array of doubles. - std::cout << "Test 2 - as a std::array..."; + std::cout << "\nTest 2 - as a std::array..."; std::array double_arr = {-100.2, 10.2, 20.0, 9.0, 7.5, 7.2}; std::array sorted_arr = sorting::gnomeSort(double_arr); assert(std::is_sorted(std::begin(sorted_arr), std::end(sorted_arr))); std::cout << " Passed\n"; - for (int i = 0; i < size; i++) std::cout << double_arr[i] << " "; + for (int i = 0; i < size; i++) std::cout << double_arr[i] << ", "; + std::cout << std::endl; + + // Example 3. Creating random array of float. + std::cout << "\nTest 3 - 200 random numbers as a std::array..."; + const int size2 = 200; + std::array rand_arr; + + for (auto &a : rand_arr) + // generate random numbers between -5.0 and 4.99 + a = float(std::rand() % 1000 - 500) / 100.f; + + std::array float_arr = sorting::gnomeSort(rand_arr); + assert(std::is_sorted(std::begin(float_arr), std::end(float_arr))); + std::cout << " Passed\n"; + // for (int i = 0; i < size; i++) std::cout << double_arr[i] << ", "; std::cout << std::endl; }