25std::ostream &
operator<<(std::ostream &out,
const std::vector<T> &arr) {
26 for (
size_t i = 0; i < arr.size(); ++i) {
28 if (i < arr.size() - 1) {
56void partition3(std::vector<T> *arr, int32_t low, int32_t high, int32_t *i,
59 if (high - low <= 1) {
60 if ((*arr)[high] < (*arr)[low]) {
61 std::swap((*arr)[high], (*arr)[low]);
69 T pivot = (*arr)[high];
71 if ((*arr)[mid] < pivot) {
72 std::swap((*arr)[low++], (*arr)[mid++]);
73 }
else if ((*arr)[mid] == pivot) {
75 }
else if ((*arr)[mid] > pivot) {
76 std::swap((*arr)[mid], (*arr)[high--]);
94void quicksort(std::vector<T> *arr, int32_t low, int32_t high) {
102 partition3(arr, low, high, &i, &j);
119std::vector<T>
quicksort(std::vector<T> arr, int32_t low, int32_t high) {
124 int32_t i = 0, j = 0;
127 partition3(&arr, low, high, &i, &j);
139 std::cout <<
"\nTesting integer type arrays\n";
141 for (
int num_tests = 1; num_tests < 21; num_tests++) {
142 size_t size = std::rand() % 500;
143 std::vector<int> arr(size);
144 for (
auto &a : arr) {
145 a = std::rand() % 500 - 250;
148 std::cout <<
"Test " << num_tests <<
"\t Array size:" << size <<
"\t ";
151 std::cout <<
"\t Sorted Array is:\n\t";
152 std::cout << sorted <<
"\n";
154 assert(std::is_sorted(std::begin(sorted), std::end(sorted)));
155 std::cout <<
"\t Passed\n";
161 std::cout <<
"\nTesting Double type arrays\n";
162 for (
int num_tests = 1; num_tests < 21; num_tests++) {
163 size_t size = std::rand() % 500;
164 std::vector<double> arr(size);
165 for (
auto &a : arr) {
166 a = double(std::rand() % 500) -
171 std::cout <<
"Test " << num_tests <<
"\t Array size:" << size <<
"\t ";
172 std::vector<double> sorted =
175 std::cout <<
"\t Sorted Array is:\n\t";
176 std::cout << sorted <<
"\n";
178 assert(std::is_sorted(std::begin(sorted), std::end(sorted)));
179 std::cout <<
"\t Passed\n";
185 std::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()