TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
quick_sort.cpp File Reference

Quick sort implementation in C++ More...

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <ctime>
#include <iostream>
#include <vector>
Include dependency graph for quick_sort.cpp:

Go to the source code of this file.

Namespaces

namespace  sorting
 for working with vectors
 
namespace  quick_sort
 Functions for the Quick sort implementation in C++.
 

Functions

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.
 
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.
 
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.
 
template<typename T >
void sorting::quick_sort::show (const std::vector< T > &arr, const int &size)
 Utility function to print the array contents.
 
static void tests ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

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

Definition in file quick_sort.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 202 of file quick_sort.cpp.

202 {
203 int choice = 0;
204
205 std::cout << "\tAvailable modes\t\n\n";
206 std::cout << "1. Self-tests mode\n2. Interactive mode";
207
208 std::cout << "\nChoose a mode: ";
209 std::cin >> choice;
210 std::cout << "\n";
211
212 while ((choice != 1) && (choice != 2)) {
213 std::cout << "Invalid option. Choose between the valid modes: ";
214 std::cin >> choice;
215 }
216
217 if (choice == 1) {
218 std::srand(std::time(nullptr));
219 tests(); // run self-test implementations
220 } else if (choice == 2) {
221 int size = 0;
222 std::cout << "\nEnter the number of elements: ";
223
224 std::cin >> size;
225 std::vector<float> arr(size);
226
227 std::cout
228 << "\nEnter the unsorted elements (can be negative/decimal): ";
229
230 for (int i = 0; i < size; ++i) {
231 std::cout << "\n";
232 std::cin >> arr[i];
233 }
234 sorting::quick_sort::quick_sort(&arr, 0, size - 1);
235 std::cout << "\nSorted array: \n";
236 sorting::quick_sort::show(arr, size);
237 }
238 return 0;
239}
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.
void show(const std::vector< T > &arr, const int &size)
Utility function to print the array contents.

◆ partition()

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.

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
Tarray type
Parameters
arrthe array with contents given by the user
lowfirst point of the array (starting index)
highlast point of the array (ending index)
Returns
index of the smaller element

Definition at line 59 of file quick_sort.cpp.

59 {
60 T pivot = (*arr)[high]; // taking the last element as pivot
61 int i = (low - 1); // Index of smaller element
62
63 for (int j = low; j < high; j++) {
64 // If current element is smaller than or
65 // equal to pivot
66 if ((*arr)[j] <= pivot) {
67 i++; // increment index of smaller element
68 std::swap((*arr)[i], (*arr)[j]);
69 }
70 }
71
72 std::swap((*arr)[i + 1], (*arr)[high]);
73 return (i + 1);
74}

◆ 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
Tarray type
Parameters
arrarray to be sorted
lowstarting index
highending index

Definition at line 87 of file quick_sort.cpp.

87 {
88 if (low < high) {
89 int p = partition(arr, low, high);
90
91 quick_sort(arr, low, p - 1);
92 quick_sort(arr, p + 1, high);
93 }
94}
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
Tarray type
Parameters
arrarray to be sorted
lowstarting index
highending index

Definition at line 107 of file quick_sort.cpp.

107 {
108 if (low < high) {
109 int p = partition(&arr, low, high);
110
111 quick_sort(&arr, low, p - 1);
112 quick_sort(&arr, p + 1, high);
113 }
114 return arr;
115}

◆ show()

template<typename T >
void sorting::quick_sort::show ( const std::vector< T > & arr,
const int & size )

Utility function to print the array contents.

Parameters
arrthe array to be printed
sizesize of the given array
Returns
void

Definition at line 124 of file quick_sort.cpp.

124 {
125 for (int i = 0; i < size; i++) std::cout << arr[i] << " ";
126 std::cout << "\n";
127}

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void

Definition at line 136 of file quick_sort.cpp.

136 {
137 // 1st test (normal numbers)
138 std::vector<uint64_t> arr = {5, 3, 8, 12, 14, 16, 28, 96, 2, 5977};
139 std::vector<uint64_t> arr_sorted = sorting::quick_sort::quick_sort(
140 arr, 0, int(std::end(arr) - std::begin(arr)) - 1);
141
142 assert(std::is_sorted(std::begin(arr_sorted), std::end(arr_sorted)));
143 std::cout << "\n1st test: passed!\n";
144
145 // 2nd test (normal and negative numbers)
146 std::vector<int64_t> arr2 = {9, 15, 28, 96, 500, -4, -58,
147 -977, -238, -800, -21, -53, -55};
148 std::vector<int64_t> arr_sorted2 = sorting::quick_sort::quick_sort(
149 arr2, 0, std::end(arr2) - std::begin(arr2));
150
151 assert(std::is_sorted(std::begin(arr_sorted2), std::end(arr_sorted2)));
152 std::cout << "2nd test: passed!\n";
153
154 // 3rd test (decimal and normal numbers)
155 std::vector<double> arr3 = {29, 36, 1100, 0, 77, 1,
156 6.7, 8.97, 1.74, 950.10, -329.65};
157 std::vector<double> arr_sorted3 = sorting::quick_sort::quick_sort(
158 arr3, 0, int(std::end(arr3) - std::begin(arr3)) - 1);
159
160 assert(std::is_sorted(std::begin(arr_sorted3), std::end(arr_sorted3)));
161 std::cout << "3rd test: passed!\n";
162
163 // 4th test (random decimal and negative numbers)
164 size_t size = std::rand() % 750 + 100;
165
166 std::vector<float> arr4(size);
167 for (uint64_t i = 0; i < size; i++) {
168 arr4[i] = static_cast<float>(std::rand()) /
169 static_cast<float>(RAND_MAX / 999.99 - 0.99) -
170 250;
171 }
172
173 std::vector<float> arr4_sorted = sorting::quick_sort::quick_sort(
174 arr4, 0, int(std::end(arr4) - std::begin(arr4)) - 1);
175 assert(std::is_sorted(std::begin(arr4_sorted), std::end(arr4_sorted)));
176
177 std::cout << "4th test: passed!\n";
178
179 // Printing all sorted arrays
180 std::cout << "\n\tPrinting all sorted arrays:\t\n";
181
182 std::cout << "1st array:\n";
183 sorting::quick_sort::show(arr_sorted, std::end(arr) - std::begin(arr));
184 std::cout << std::endl;
185 std::cout << "2nd array:\n";
186 sorting::quick_sort::show(arr_sorted2, std::end(arr2) - std::begin(arr2));
187 std::cout << std::endl;
188 std::cout << "3rd array:\n";
189 sorting::quick_sort::show(arr_sorted3,
190 int(std::end(arr3) - std::begin(arr3)) - 1);
191 std::cout << std::endl;
192 std::cout << "Start: 4th array:\n\n";
194 arr4_sorted, int(std::end(arr4_sorted) - std::begin(arr4_sorted)) - 1);
195 std::cout << "\nEnd: 4th array.\n";
196}