std::ostream &
operator<<(std::ostream &out,
conststd::vector<T> &arr) {
26 for(
size_ti = 0; i < arr.size(); ++i) {
28 if(i < arr.size() - 1) {
56voidpartition3(std::vector<T> *arr, int32_t low, int32_t high, int32_t *i,
59 if(high - low <= 1) {
60 if((*arr)[high] < (*arr)[low]) {
61std::swap((*arr)[high], (*arr)[low]);
69T pivot = (*arr)[high];
71 if((*arr)[mid] < pivot) {
72std::swap((*arr)[low++], (*arr)[mid++]);
73}
else if((*arr)[mid] == pivot) {
75}
else if((*arr)[mid] > pivot) {
76std::swap((*arr)[mid], (*arr)[high--]);
94void quicksort(std::vector<T> *arr, int32_t low, int32_t high) {
102partition3(arr, low, high, &i, &j);
118template<
typenameT>
119std::vector<T>
quicksort(std::vector<T> arr, int32_t low, int32_t high) {
124int32_t i = 0, j = 0;
127partition3(&arr, low, high, &i, &j);
139std::cout <<
"\nTesting integer type arrays\n";
141 for(
intnum_tests = 1; num_tests < 21; num_tests++) {
142 size_tsize = std::rand() % 500;
143std::vector<int> arr(size);
144 for(
auto&a : arr) {
145a = std::rand() % 500 - 250;
148std::cout <<
"Test "<< num_tests <<
"\t Array size:"<< size <<
"\t ";
151std::cout <<
"\t Sorted Array is:\n\t";
152std::cout << sorted <<
"\n";
154assert(std::is_sorted(std::begin(sorted), std::end(sorted)));
155std::cout <<
"\t Passed\n";
161std::cout <<
"\nTesting Double type arrays\n";
162 for(
intnum_tests = 1; num_tests < 21; num_tests++) {
163 size_tsize = std::rand() % 500;
164std::vector<double> arr(size);
165 for(
auto&a : arr) {
166a = double(std::rand() % 500) -
171std::cout <<
"Test "<< num_tests <<
"\t Array size:"<< size <<
"\t ";
172std::vector<double> sorted =
175std::cout <<
"\t Sorted Array is:\n\t";
176std::cout << sorted <<
"\n";
178assert(std::is_sorted(std::begin(sorted), std::end(sorted)));
179std::cout <<
"\t Passed\n";
185std::srand(std::time(
nullptr));
static std::ostream & operator<<(std::ostream &out, matrix< T > const &v)
void quicksort(std::vector< T > *arr, int32_t low, int32_t high)
static void test_double()
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4