Quick sort algorithm.
+
#include <cstdlib>
+
#include <algorithm>
+#include <cassert>
+#include <ctime>
#include <iostream>
+#include <vector>
|
-| int | sorting::partition (int arr[], int low, int high) |
-| |
-| void | sorting::quickSort (int arr[], int low, int high) |
-| |
-| void | show (int arr[], int size) |
-| |
+| template<typename T > |
+| int | sorting::quick_sort::partition (std::vector< T > *arr, const int &low, const int &high) |
+| | Sorts the array taking the last element as pivot. More...
|
+| |
+| template<typename T > |
+| void | sorting::quick_sort::quick_sort (std::vector< T > *arr, const int &low, const int &high) |
+| | the main function that implements Quick Sort. More...
|
+| |
+| template<typename T > |
+| std::vector< T > | sorting::quick_sort::quick_sort (std::vector< T > arr, const int &low, const int &high) |
+| | the main function that implements Quick Sort. More...
|
+| |
+| template<typename T > |
+| void | sorting::quick_sort::show (const std::vector< T > &arr, const int &size) |
+| | Utility function to print the array contents. More...
|
+| |
+| static void | tests () |
+| | Self-test implementations. More...
|
+| |
| int | main () |
+| | Main function. More...
|
| |
-
Quick sort algorithm.
-
Implementation Details - Quick Sort is a divide and conquer algorithm. It picks and element as pivot and partition the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.
-
-- Always pick the first element as pivot
-- Always pick the last element as pivot (implemented below)
-- Pick a random element as pivot
-- Pick median as pivot
-
-
The key process in quickSort is partition(). Target of partition is, given an array and an element x(say) of array as pivot, put x at it's correct position in sorted array and put all smaller elements (samller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time
+
Quick sort implementation in C++
+
Quick Sort is a divide and conquer algorithm. It picks an element as pivot and partition the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.
1. Always pick the first element as pivot
+ 2. Always pick the last element as pivot (implemented below)
+ 3. Pick a random element as pivot
+ 4. Pick median as pivot
+
+ The key process in quickSort is partition(). Target of partition is,
+ given an array and an element x(say) of array as pivot, put x at it's
+ correct position in sorted array and put all smaller elements (samller
+ than x) before x, and put all greater elements (greater than x) after
+ x. All this should be done in linear time
+
- Author
- David Leal
+-
+popoapp
◆ main()
@@ -158,48 +183,281 @@ Functions
-
Driver program to test above functions
-
82 {
-
83 int size;
-
84 std::cout <<
"\nEnter the number of elements : ";
-
85
-
-
87
-
88 int *arr = new int[size];
-
89
-
90 std::cout <<
"\nEnter the unsorted elements : ";
-
91
-
92 for (int i = 0; i < size; ++i) {
-
-
-
95 }
-
96 quickSort(arr, 0, size);
-
-
98 show(arr, size);
-
99 delete[] arr;
-
100 return 0;
-
101}
+
+
Main function.
+
- Returns
- 0 on exit
+
201 {
+
202 int choice = 0;
+
203
+
+
205 std::cout <<
"1. Self-tests mode\n2. Interactive mode";
+
206
+
+
+
+
210
+
211 while ((choice != 1) && (choice != 2)) {
+
212 std::cout <<
"Invalid option. Choose between the valid modes: ";
+
+
214 }
+
215
+
216 if (choice == 1) {
+
+
+
219 } else if (choice == 2) {
+
220 int size = 0;
+
221 std::cout <<
"\nEnter the number of elements: ";
+
222
+
+
+
225
+
+
227 << "\nEnter the unsorted elements (can be negative/decimal): ";
+
228
+
229 for (int i = 0; i < size; ++i) {
+
+
+
232 }
+
+
+
+
236 }
+
237 return 0;
+
238}
-
+
static void tests()
Self-test implementations.
Definition: quick_sort.cpp:135
+
void quick_sort(std::vector< T > *arr, const int &low, const int &high)
the main function that implements Quick Sort.
Definition: quick_sort.cpp:86
+
void show(const std::vector< T > &arr, const int &size)
Utility function to print the array contents.
Definition: quick_sort.cpp:123
+
+
+
+
+
-
-
◆ show()
+
+
+
+
+
◆ partition()
+
+template<typename T >
- | void show |
+ int sorting::quick_sort::partition |
( |
- int |
- arr[], |
+ std::vector< T > * |
+ arr, |
|
|
- int |
+ const int & |
+ low, |
+
+
+ |
+ |
+ const int & |
+ high |
+
+
+ |
+ ) |
+ | |
+
+
+
+
+
Sorts the array taking the last element as pivot.
+
This function takes last element as pivot, places the pivot element at its correct position in sorted array, and places all smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot
- Template Parameters
-
+
+
+
+
- Parameters
-
+
+ | arr | the array with contents given by the user |
+ | low | first point of the array (starting index) |
+ | high | last point of the array (ending index) |
+
+
+
+
- Returns
- index of the smaller element
+
58 {
+
59 T pivot = (*arr)[high];
+
60 int i = (low - 1);
+
61
+
62 for (int j = low; j < high; j++) {
+
63
+
64
+
65 if ((*arr)[j] <= pivot) {
+
66 i++;
+
+
68 }
+
69 }
+
70
+
+
72 return (i + 1);
+
73}
+
+
+
+
+
+
+
+
◆ quick_sort() [1/2]
+
+
+
+
+template<typename T >
+
+
+ | void sorting::quick_sort::quick_sort |
+ ( |
+ std::vector< T > * |
+ arr, |
+
+
+ |
+ |
+ const int & |
+ low, |
+
+
+ |
+ |
+ const int & |
+ high |
+
+
+ |
+ ) |
+ | |
+
+
+
+
+
the main function that implements Quick Sort.
+
Void function used in T (array type) function, which then can be used as self-tests or other functionalities.
- Template Parameters
-
+
+
+
+
- Parameters
-
+
+ | arr | array to be sorted |
+ | low | starting index |
+ | high | ending index |
+
+
+
+
86 {
+
87 if (low < high) {
+
+
89
+
+
+
92 }
+
93}
+
Functions for the Quick sort implementation in C++.
+
+
+
+
+
+
◆ quick_sort() [2/2]
+
+
+
+
+template<typename T >
+
+
+ | std::vector< T > sorting::quick_sort::quick_sort |
+ ( |
+ std::vector< T > |
+ arr, |
+
+
+ |
+ |
+ const int & |
+ low, |
+
+
+ |
+ |
+ const int & |
+ high |
+
+
+ |
+ ) |
+ | |
+
+
+
+
+
the main function that implements Quick Sort.
+
T (array type) function which calls the void function. Can be used for self-tests and other functionalities.
- Template Parameters
-
+
+
+
+
- Parameters
-
+
+ | arr | array to be sorted |
+ | low | starting index |
+ | high | ending index |
+
+
+
+
106 {
+
107 if (low < high) {
+
+
109
+
+
+
112 }
+
113 return arr;
+
114}
+
+
+
+
+
◆ show()
+
+
+
+
+template<typename T >
+
+
+ | void sorting::quick_sort::show |
+ ( |
+ const std::vector< T > & |
+ arr, |
+
+
+ |
+ |
+ const int & |
size |
@@ -209,11 +467,121 @@ Functions
-
76 {
-
77 for (
int i = 0; i < size; i++)
std::cout << arr[i] <<
" ";
-
-
79}
+
+
Utility function to print the array contents.
+
- Parameters
-
+
+ | arr | the array to be printed |
+ | size | size of the given array |
+
+
+
+
- Returns
- void
+
123 {
+
124 for (
int i = 0; i < size; i++)
std::cout << arr[i] <<
" ";
+
+
126}
+
+
+
+
◆ tests()
+
+
+
+
+
+
+
+
+ | static void tests |
+ ( |
+ | ) |
+ |
+
+
+ |
+
+static |
+
+
+
+
+
Self-test implementations.
+
- Returns
- void
+
135 {
+
136
+
+
+
+
140
+
+
+
143
+
144
+
+
146 -977, -238, -800, -21, -53, -55};
+
+
+
149
+
+
+
152
+
153
+
+
155 6.7, 8.97, 1.74, 950.10, -329.65};
+
+
+
158
+
+
+
161
+
162
+
+
164
+
+
166 for (uint64_t i = 0; i < size; i++) {
+
167 arr4[i] =
static_cast<float>(
std::rand()) /
+
168 static_cast<float>(RAND_MAX / 999.99 - 0.99) -
+
169 250;
+
170 }
+
171
+
+
+
+
175
+
+
177
+
178
+
179 std::cout <<
"\n\tPrinting all sorted arrays:\t\n";
+
180
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
195}
+
+
+
+
+
+
+
+
diff --git a/d1/d21/quick__sort_8cpp.js b/d1/d21/quick__sort_8cpp.js
index a97ca95d4..6430ec504 100644
--- a/d1/d21/quick__sort_8cpp.js
+++ b/d1/d21/quick__sort_8cpp.js
@@ -1,6 +1,9 @@
var quick__sort_8cpp =
[
[ "main", "d1/d21/quick__sort_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4", null ],
- [ "partition", "d1/d21/quick__sort_8cpp.html#a7e7f25f31c50523990437abf2ac3907e", null ],
- [ "quickSort", "d1/d21/quick__sort_8cpp.html#a50b66a1c652291b9a346ec7342967178", null ]
+ [ "partition", "d1/d21/quick__sort_8cpp.html#a1b2a2cc2d319240f3b65c5b2f479ed82", null ],
+ [ "quick_sort", "d1/d21/quick__sort_8cpp.html#a5e6213e8008356ac6eda9427f3f4b394", null ],
+ [ "quick_sort", "d1/d21/quick__sort_8cpp.html#a53adad7e4d83e1495df25fe8dbb4cc05", null ],
+ [ "show", "d1/d21/quick__sort_8cpp.html#aeccefcf6fcca62c54939c5ec9a93109b", null ],
+ [ "tests", "d1/d21/quick__sort_8cpp.html#a483bb8ccf42aaf7375a83e91490eda1e", null ]
];
\ No newline at end of file
diff --git a/d1/d21/quick__sort_8cpp_a1b2a2cc2d319240f3b65c5b2f479ed82_cgraph.map b/d1/d21/quick__sort_8cpp_a1b2a2cc2d319240f3b65c5b2f479ed82_cgraph.map
new file mode 100644
index 000000000..9e5e618eb
--- /dev/null
+++ b/d1/d21/quick__sort_8cpp_a1b2a2cc2d319240f3b65c5b2f479ed82_cgraph.map
@@ -0,0 +1,4 @@
+
diff --git a/d1/d21/quick__sort_8cpp_a1b2a2cc2d319240f3b65c5b2f479ed82_cgraph.md5 b/d1/d21/quick__sort_8cpp_a1b2a2cc2d319240f3b65c5b2f479ed82_cgraph.md5
new file mode 100644
index 000000000..41d2ce219
--- /dev/null
+++ b/d1/d21/quick__sort_8cpp_a1b2a2cc2d319240f3b65c5b2f479ed82_cgraph.md5
@@ -0,0 +1 @@
+4efa02a74b8a1aba2ef95d0cb31e2c58
\ No newline at end of file
diff --git a/d1/d21/quick__sort_8cpp_a1b2a2cc2d319240f3b65c5b2f479ed82_cgraph.svg b/d1/d21/quick__sort_8cpp_a1b2a2cc2d319240f3b65c5b2f479ed82_cgraph.svg
new file mode 100644
index 000000000..5439b8bf9
--- /dev/null
+++ b/d1/d21/quick__sort_8cpp_a1b2a2cc2d319240f3b65c5b2f479ed82_cgraph.svg
@@ -0,0 +1,37 @@
+
+
+
+
+
diff --git a/d1/d21/quick__sort_8cpp_a483bb8ccf42aaf7375a83e91490eda1e_cgraph.map b/d1/d21/quick__sort_8cpp_a483bb8ccf42aaf7375a83e91490eda1e_cgraph.map
new file mode 100644
index 000000000..f8ab19fc0
--- /dev/null
+++ b/d1/d21/quick__sort_8cpp_a483bb8ccf42aaf7375a83e91490eda1e_cgraph.map
@@ -0,0 +1,8 @@
+
diff --git a/d1/d21/quick__sort_8cpp_a483bb8ccf42aaf7375a83e91490eda1e_cgraph.md5 b/d1/d21/quick__sort_8cpp_a483bb8ccf42aaf7375a83e91490eda1e_cgraph.md5
new file mode 100644
index 000000000..a71402d1b
--- /dev/null
+++ b/d1/d21/quick__sort_8cpp_a483bb8ccf42aaf7375a83e91490eda1e_cgraph.md5
@@ -0,0 +1 @@
+4f2453e1f366e9e6ffadb66202e72dfb
\ No newline at end of file
diff --git a/d1/d21/quick__sort_8cpp_a483bb8ccf42aaf7375a83e91490eda1e_cgraph.svg b/d1/d21/quick__sort_8cpp_a483bb8ccf42aaf7375a83e91490eda1e_cgraph.svg
new file mode 100644
index 000000000..9aee0af84
--- /dev/null
+++ b/d1/d21/quick__sort_8cpp_a483bb8ccf42aaf7375a83e91490eda1e_cgraph.svg
@@ -0,0 +1,96 @@
+
+
+
+
+
diff --git a/d1/d21/quick__sort_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.map b/d1/d21/quick__sort_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.map
new file mode 100644
index 000000000..55e789b45
--- /dev/null
+++ b/d1/d21/quick__sort_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.map
@@ -0,0 +1,11 @@
+
diff --git a/d1/d21/quick__sort_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.md5 b/d1/d21/quick__sort_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.md5
new file mode 100644
index 000000000..36eda0f60
--- /dev/null
+++ b/d1/d21/quick__sort_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.md5
@@ -0,0 +1 @@
+3aab3bbf5657ef0510822b502193deee
\ No newline at end of file
diff --git a/d1/d21/quick__sort_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg b/d1/d21/quick__sort_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg
new file mode 100644
index 000000000..b7697124a
--- /dev/null
+++ b/d1/d21/quick__sort_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg
@@ -0,0 +1,141 @@
+
+
+
+
+
diff --git a/d1/d2a/knight__tour_8cpp_aaa47356d98676cf5315d978f741e29c9_cgraph.map b/d1/d2a/knight__tour_8cpp_aaa47356d98676cf5315d978f741e29c9_cgraph.map
index 4922c1144..91f3d44ef 100644
--- a/d1/d2a/knight__tour_8cpp_aaa47356d98676cf5315d978f741e29c9_cgraph.map
+++ b/d1/d2a/knight__tour_8cpp_aaa47356d98676cf5315d978f741e29c9_cgraph.map
@@ -1,4 +1,4 @@
diff --git a/d1/d2a/knight__tour_8cpp_aaa47356d98676cf5315d978f741e29c9_cgraph.svg b/d1/d2a/knight__tour_8cpp_aaa47356d98676cf5315d978f741e29c9_cgraph.svg
index d83b10167..df52a2ee0 100644
--- a/d1/d2a/knight__tour_8cpp_aaa47356d98676cf5315d978f741e29c9_cgraph.svg
+++ b/d1/d2a/knight__tour_8cpp_aaa47356d98676cf5315d978f741e29c9_cgraph.svg
@@ -1,7 +1,7 @@
-