Algorithms_in_C++ 1.0.0
Set of 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:

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

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
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
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.
Definition quick_sort.cpp:136
void quick_sort(std::vector< T > *arr, const int &low, const int &high)
the main function that implements Quick Sort.
Definition quick_sort.cpp:87
void show(const std::vector< T > &arr, const int &size)
Utility function to print the array contents.
Definition quick_sort.cpp:124
T srand(T... args)
T time(T... args)
Here is the call graph for this function:

◆ 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
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}
T swap(T... args)
Here is the call graph for this function:

◆ 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
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++.
T partition(T... args)

◆ 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
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
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
136 {
137 // 1st test (normal numbers)
138 std::vector<uint64_t> arr = {5, 3, 8, 12, 14, 16, 28, 96, 2, 5977};
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};
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};
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
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));
185 std::cout << "2nd array:\n";
186 sorting::quick_sort::show(arr_sorted2, std::end(arr2) - std::begin(arr2));
188 std::cout << "3rd array:\n";
189 sorting::quick_sort::show(arr_sorted3,
190 int(std::end(arr3) - std::begin(arr3)) - 1);
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}
T begin(T... args)
T end(T... args)
T endl(T... args)
T is_sorted(T... args)
T rand(T... args)
Here is the call graph for this function: