mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
added random values test
This commit is contained in:
parent
d9c87d59e0
commit
97161cf894
@ -61,12 +61,12 @@ template <typename T, size_t size>
|
|||||||
std::array<T, size> gnomeSort(std::array<T, size> arr) {
|
std::array<T, size> gnomeSort(std::array<T, size> arr) {
|
||||||
// few easy cases
|
// few easy cases
|
||||||
if (size <= 1)
|
if (size <= 1)
|
||||||
return;
|
return arr;
|
||||||
|
|
||||||
int index = 1; // initialize loop index
|
int index = 1; // initialize loop index
|
||||||
while (index < size) {
|
while (index < size) {
|
||||||
// check for swap
|
// check for swap
|
||||||
if (arr[index] >= arr[index - 1]) {
|
if ((index == 0) || (arr[index] >= arr[index - 1])) {
|
||||||
index++;
|
index++;
|
||||||
} else {
|
} else {
|
||||||
std::swap(arr[index], arr[index - 1]); // swap
|
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
|
size); // pass array data as a C-style array pointer
|
||||||
assert(std::is_sorted(std::begin(arr), std::end(arr)));
|
assert(std::is_sorted(std::begin(arr), std::end(arr)));
|
||||||
std::cout << " Passed\n";
|
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;
|
std::cout << std::endl;
|
||||||
|
|
||||||
// Example 2. Creating array of doubles.
|
// 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, size> double_arr = {-100.2, 10.2, 20.0, 9.0, 7.5, 7.2};
|
std::array<double, size> double_arr = {-100.2, 10.2, 20.0, 9.0, 7.5, 7.2};
|
||||||
std::array<double, size> sorted_arr = sorting::gnomeSort(double_arr);
|
std::array<double, size> sorted_arr = sorting::gnomeSort(double_arr);
|
||||||
assert(std::is_sorted(std::begin(sorted_arr), std::end(sorted_arr)));
|
assert(std::is_sorted(std::begin(sorted_arr), std::end(sorted_arr)));
|
||||||
std::cout << " Passed\n";
|
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<float, size2> 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, size2> 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;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user