Skip to content

Commit 40c858b

Browse files
authored
[feat/docs]: improve the quick_sort.cpp...
...algorithm implementation.
1 parent cfe1946 commit 40c858b

File tree

1 file changed

+168
-49
lines changed

1 file changed

+168
-49
lines changed

sorting/quick_sort.cpp

Lines changed: 168 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
/**
22
* @file
3-
* @brief Quick sort algorithm
4-
*
5-
* Implementation Details -
6-
* Quick Sort is a divide and conquer algorithm. It picks and element as
7-
* pivot and partition the given array around the picked pivot. There
8-
* are many different versions of quickSort that pick pivot in different
9-
* ways.
3+
* @brief [Quick sort implementation](https://en.wikipedia.org/wiki/Quicksort) in C++
4+
* @details
5+
* Quick Sort is a [divide and conquer algorithm](https://en.wikipedia.org/wiki/Category:Divide-and-conquer_algorithms).
6+
* It picks an element as pivot and partition the given array around the picked pivot. There
7+
* are many different versions of quickSort that pick pivot in different ways.
108
*
119
* 1. Always pick the first element as pivot
1210
* 2. Always pick the last element as pivot (implemented below)
@@ -19,83 +17,204 @@
1917
* than x) before x, and put all greater elements (greater than x) after
2018
* x. All this should be done in linear time
2119
*
20+
* @author [David Leal](https://github.com/Panquesito7)
21+
* @author Unknown author
2222
*/
2323

24-
#include <cstdlib>
25-
#include <iostream>
24+
#include <algorithm> /// for std::is_sorted
25+
#include <iostream> /// for IO operations
26+
#include <vector> /// for std::vector
27+
#include <cassert> /// for std::assert
28+
#include <ctime> /// for std::time
2629

30+
/**
31+
* @brief Sorting algorithms
32+
* @namespace sorting
33+
*/
2734
namespace sorting {
2835
/**
29-
* This function takes last element as pivot, places
30-
* the pivot element at its correct position in sorted
31-
* array, and places all smaller (smaller than pivot)
32-
* to left of pivot and all greater elements to right
33-
* of pivot
34-
*
36+
* @namespace quick_sort
37+
* @brief Functions for the [Quick sort implementation](https://en.wikipedia.org/wiki/Quicksort) in C++
3538
*/
36-
37-
int partition(int arr[], int low, int high) {
38-
int pivot = arr[high]; // taking the last element as pivot
39+
namespace quick_sort {
40+
/**
41+
* @brief Sorts the array taking the last element as pivot
42+
* @details
43+
* This function takes last element as pivot, places
44+
* the pivot element at its correct position in sorted
45+
* array, and places all smaller (smaller than pivot)
46+
* to left of pivot and all greater elements to right of pivot
47+
* @tparam T array type
48+
* @param arr the array with contents given by the user
49+
* @param low first point of the array (starting index)
50+
* @param high last point of the array (ending index)
51+
* @returns index of the smaller element
52+
*/
53+
template <typename T>
54+
int partition(std::vector<T> *arr, const int &low, const int &high) {
55+
T pivot = (*arr)[high]; // taking the last element as pivot
3956
int i = (low - 1); // Index of smaller element
4057

4158
for (int j = low; j < high; j++) {
4259
// If current element is smaller than or
4360
// equal to pivot
44-
if (arr[j] <= pivot) {
61+
if ((*arr)[j] <= pivot) {
4562
i++; // increment index of smaller element
46-
int temp = arr[i];
47-
arr[i] = arr[j];
48-
arr[j] = temp;
63+
std::swap((*arr)[i], (*arr)[j]);
4964
}
5065
}
51-
int temp = arr[i + 1];
52-
arr[i + 1] = arr[high];
53-
arr[high] = temp;
66+
67+
std::swap((*arr)[i + 1], (*arr)[high]);
5468
return (i + 1);
5569
}
5670

5771
/**
58-
* The main function that implements QuickSort
59-
* arr[] --> Array to be sorted,
60-
* low --> Starting index,
61-
* high --> Ending index
72+
* @brief the main function that implements Quick Sort.
73+
*
74+
* Void function used in T (array type) function, which then
75+
* can be used as self-tests or other functionalities.
76+
* @tparam T array type
77+
* @param arr array to be sorted
78+
* @param low starting index
79+
* @param high ending index
6280
*/
63-
void quickSort(int arr[], int low, int high) {
81+
template <typename T>
82+
void quick_sort(std::vector<T> *arr, const int &low, const int &high) {
6483
if (low < high) {
6584
int p = partition(arr, low, high);
66-
quickSort(arr, low, p - 1);
67-
quickSort(arr, p + 1, high);
85+
86+
quick_sort(arr, low, p - 1);
87+
quick_sort(arr, p + 1, high);
6888
}
6989
}
7090

71-
} // namespace sorting
91+
/**
92+
* @brief the main function that implements Quick Sort.
93+
*
94+
* T (array type) function which calls the void function. Can
95+
* be used for self-tests and other functionalities.
96+
* @tparam T array type
97+
* @param arr array to be sorted
98+
* @param low starting index
99+
* @param high ending index
100+
*/
101+
template <typename T>
102+
std::vector<T> quick_sort(std::vector<T> arr, const int &low, const int &high) {
103+
if (low < high) {
104+
int p = partition(&arr, low, high);
72105

73-
using sorting::quickSort;
106+
quick_sort(&arr, low, p - 1);
107+
quick_sort(&arr, p + 1, high);
108+
}
109+
return arr;
110+
}
74111

75-
// prints the array after sorting
76-
void show(int arr[], int size) {
112+
/**
113+
* @brief Utility function to print the array contents
114+
* @param arr the array to be printed
115+
* @param size size of the given array
116+
* @returns void
117+
*/
118+
template <typename T>
119+
void show(const std::vector<T> &arr, const int &size) {
77120
for (int i = 0; i < size; i++) std::cout << arr[i] << " ";
78121
std::cout << "\n";
79122
}
80123

81-
/** Driver program to test above functions */
124+
} // namespace quick_sort
125+
} // namespace sorting
126+
127+
/**
128+
* @brief Self-test implementations
129+
* @returns void
130+
*/
131+
static void tests() {
132+
// 1st test (normal numbers)
133+
std::vector<uint64_t> arr = {5, 3, 8, 12, 14, 16, 28, 96, 2, 5977};
134+
std::vector<uint64_t> arr_sorted = sorting::quick_sort::quick_sort(arr, 0, int(std::end(arr)-std::begin(arr)) - 1);
135+
136+
assert(std::is_sorted(std::begin(arr_sorted), std::end(arr_sorted)));
137+
std::cout << "\n1st test: passed!\n";
138+
139+
// 2nd test (normal and negative numbers)
140+
std::vector<int64_t> arr2 = {9, 15, 28, 96, 500, -4, -58, -977, -238, -800, -21, -53, -55};
141+
std::vector<int64_t> arr_sorted2 = sorting::quick_sort::quick_sort(arr2, 0, std::end(arr2)-std::begin(arr2));
142+
143+
assert(std::is_sorted(std::begin(arr_sorted2), std::end(arr_sorted2)));
144+
std::cout << "2nd test: passed!\n";
145+
146+
// 3rd test (decimal and normal numbers)
147+
std::vector<double> arr3 = {29, 36, 1100, 0, 77, 1, 6.7, 8.97, 1.74, 950.10, -329.65};
148+
std::vector<double> arr_sorted3 = sorting::quick_sort::quick_sort(arr3, 0, int(std::end(arr3)-std::begin(arr3)) - 1);
149+
150+
assert(std::is_sorted(std::begin(arr_sorted3), std::end(arr_sorted3)));
151+
std::cout << "3rd test: passed!\n";
152+
153+
// 4th test (random decimal and negative numbers)
154+
size_t size = std::rand() % 750 + 100;
155+
156+
std::vector<float> arr4(size);
157+
for (uint64_t i = 0; i < size; i++) {
158+
arr4[i] = static_cast <float> (std::rand()) / static_cast <float> (RAND_MAX / 999.99 - 0.99) - 250;
159+
}
160+
161+
std::vector<float> arr4_sorted = sorting::quick_sort::quick_sort(arr4, 0, int(std::end(arr4) - std::begin(arr4)) - 1);
162+
assert(std::is_sorted(std::begin(arr4_sorted), std::end(arr4_sorted)));
163+
164+
std::cout << "4th test: passed!\n";
165+
166+
// Printing all sorted arrays
167+
std::cout << "\n\tPrinting all sorted arrays:\t\n";
168+
169+
std::cout << "1st array:\n";
170+
sorting::quick_sort::show(arr_sorted, std::end(arr)-std::begin(arr)); std::cout << std::endl;
171+
std::cout << "2nd array:\n";
172+
sorting::quick_sort::show(arr_sorted2, std::end(arr2)-std::begin(arr2)); std::cout << std::endl;
173+
std::cout << "3rd array:\n";
174+
sorting::quick_sort::show(arr_sorted3, int(std::end(arr3)-std::begin(arr3)) - 1); std::cout << std::endl;
175+
std::cout << "Start: 4th array:\n\n";
176+
sorting::quick_sort::show(arr4_sorted, int(std::end(arr4_sorted) - std::begin(arr4_sorted)) - 1);
177+
std::cout << "\nEnd: 4th array.\n";
178+
}
179+
180+
/**
181+
* @brief Main function
182+
* @returns 0 on exit
183+
*/
82184
int main() {
83-
int size;
84-
std::cout << "\nEnter the number of elements : ";
185+
int choice = 0;
85186

86-
std::cin >> size;
187+
std::cout << "\tAvailable modes\t\n\n";
188+
std::cout << "1. Self-tests mode\n2. Interactive mode";
87189

88-
int *arr = new int[size];
190+
std::cout << "\nChoose a mode: ";
191+
std::cin >> choice; std::cout << "\n";
89192

90-
std::cout << "\nEnter the unsorted elements : ";
193+
while ((choice != 1) && (choice != 2)) {
194+
std::cout << "Invalid option. Choose between the valid modes: ";
195+
std::cin >> choice;
196+
}
91197

92-
for (int i = 0; i < size; ++i) {
93-
std::cout << "\n";
94-
std::cin >> arr[i];
198+
if (choice == 1) {
199+
std::srand(std::time(nullptr));
200+
tests(); // run self-test implementations
201+
}
202+
else if (choice == 2) {
203+
int size = 0;
204+
std::cout << "\nEnter the number of elements: ";
205+
206+
std::cin >> size;
207+
std::vector<float> arr(size);
208+
209+
std::cout << "\nEnter the unsorted elements (can be negative/decimal): ";
210+
211+
for (int i = 0; i < size; ++i) {
212+
std::cout << "\n";
213+
std::cin >> arr[i];
214+
}
215+
sorting::quick_sort::quick_sort(&arr, 0, size - 1);
216+
std::cout << "\nSorted array: \n";
217+
sorting::quick_sort::show(arr, size);
95218
}
96-
quickSort(arr, 0, size);
97-
std::cout << "Sorted array\n";
98-
show(arr, size);
99-
delete[] arr;
100219
return 0;
101220
}

0 commit comments

Comments
 (0)