(std::vector<T> *arr,
const int&low,
const int&high) {
71T pivot = (*arr)[high];
74 for(
intj = low; j < high; j++) {
77 if((*arr)[j] <= pivot) {
79std::swap((*arr)[i], (*arr)[j]);
83std::swap((*arr)[i + 1], (*arr)[high]);
98void quick_sort(std::vector<T> *arr,
const int&low,
const int&high) {
117template<
typenameT>
118std::vector<T>
quick_sort(std::vector<T> arr,
const int&low,
const int&high) {
134template<
typenameT>
135void show(
conststd::vector<T> &arr,
const int&size) {
136 for(
inti = 0; i < size; i++) std::cout << arr[i] <<
" ";
149std::vector<uint64_t> arr = {5, 3, 8, 12, 14, 16, 28, 96, 2, 5977};
151arr, 0,
int(std::end(arr) - std::begin(arr)) - 1);
153assert(std::is_sorted(std::begin(arr_sorted), std::end(arr_sorted)));
154std::cout <<
"\n1st test: passed!\n";
157std::vector<int64_t> arr2 = {9, 15, 28, 96, 500, -4, -58,
158-977, -238, -800, -21, -53, -55};
160arr2, 0, std::end(arr2) - std::begin(arr2));
162assert(std::is_sorted(std::begin(arr_sorted2), std::end(arr_sorted2)));
163std::cout <<
"2nd test: passed!\n";
166std::vector<double> arr3 = {29, 36, 1100, 0, 77, 1,
1676.7, 8.97, 1.74, 950.10, -329.65};
169arr3, 0,
int(std::end(arr3) - std::begin(arr3)) - 1);
171assert(std::is_sorted(std::begin(arr_sorted3), std::end(arr_sorted3)));
172std::cout <<
"3rd test: passed!\n";
175 size_tsize = std::rand() % 750 + 100;
177std::vector<float> arr4(size);
178 for(uint64_t i = 0; i < size; i++) {
179arr4[i] =
static_cast<float>(std::rand()) /
180 static_cast<float>(RAND_MAX / 999.99 - 0.99) -
185arr4, 0,
int(std::end(arr4) - std::begin(arr4)) - 1);
186assert(std::is_sorted(std::begin(arr4_sorted), std::end(arr4_sorted)));
188std::cout <<
"4th test: passed!\n";
191std::cout <<
"\n\tPrinting all sorted arrays:\t\n";
193std::cout <<
"1st array:\n";
195std::cout << std::endl;
196std::cout <<
"2nd array:\n";
198std::cout << std::endl;
199std::cout <<
"3rd array:\n";
201 int(std::end(arr3) - std::begin(arr3)) - 1);
202std::cout << std::endl;
203std::cout <<
"Start: 4th array:\n\n";
205arr4_sorted,
int(std::end(arr4_sorted) - std::begin(arr4_sorted)) - 1);
206std::cout <<
"\nEnd: 4th array.\n";
216std::cout <<
"\tAvailable modes\t\n\n";
217std::cout <<
"1. Self-tests mode\n2. Interactive mode";
219std::cout <<
"\nChoose a mode: ";
223 while((choice != 1) && (choice != 2)) {
224std::cout <<
"Invalid option. Choose between the valid modes: ";
229std::srand(std::time(
nullptr));
231}
else if(choice == 2) {
233std::cout <<
"\nEnter the number of elements: ";
236std::vector<float> arr(size);
239<<
"\nEnter the unsorted elements (can be negative/decimal): ";
241 for(
inti = 0; i < size; ++i) {
246std::cout <<
"\nSorted array: \n";
Functions for the Quick sort implementation in C++.
int partition(std::vector< T > *arr, const int &low, const int &high)
Sorts the array taking the last element as pivot.
static void tests()
Self-test implementations.
void quick_sort(std::vector< T > *arr, const int &low, const int &high)
the main function that implements Quick Sort.
int main()
Main function.
void show(const std::vector< T > &arr, const int &size)
Utility function to print the array contents.
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