A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d1/d21/quick__sort_8cpp_source.html below:

TheAlgorithms/C++: sorting/quick_sort.cpp Source File

Go to the documentation of this file. 70int partition

(std::vector<T> *arr,

const int

&low,

const int

&high) {

71

T pivot = (*arr)[high];

74 for

(

int

j = low; j < high; j++) {

77 if

((*arr)[j] <= pivot) {

79

std::swap((*arr)[i], (*arr)[j]);

83

std::swap((*arr)[i + 1], (*arr)[high]);

98void quick_sort

(std::vector<T> *arr,

const int

&low,

const int

&high) {

117template

<

typename

T>

118

std::vector<T>

quick_sort

(std::vector<T> arr,

const int

&low,

const int

&high) {

134template

<

typename

T>

135void show

(

const

std::vector<T> &arr,

const int

&size) {

136 for

(

int

i = 0; i < size; i++) std::cout << arr[i] <<

" "

;

149

std::vector<uint64_t> arr = {5, 3, 8, 12, 14, 16, 28, 96, 2, 5977};

151

arr, 0,

int

(std::end(arr) - std::begin(arr)) - 1);

153

assert(std::is_sorted(std::begin(arr_sorted), std::end(arr_sorted)));

154

std::cout <<

"\n1st test: passed!\n"

;

157

std::vector<int64_t> arr2 = {9, 15, 28, 96, 500, -4, -58,

158

-977, -238, -800, -21, -53, -55};

160

arr2, 0, std::end(arr2) - std::begin(arr2));

162

assert(std::is_sorted(std::begin(arr_sorted2), std::end(arr_sorted2)));

163

std::cout <<

"2nd test: passed!\n"

;

166

std::vector<double> arr3 = {29, 36, 1100, 0, 77, 1,

167

6.7, 8.97, 1.74, 950.10, -329.65};

169

arr3, 0,

int

(std::end(arr3) - std::begin(arr3)) - 1);

171

assert(std::is_sorted(std::begin(arr_sorted3), std::end(arr_sorted3)));

172

std::cout <<

"3rd test: passed!\n"

;

175 size_t

size = std::rand() % 750 + 100;

177

std::vector<float> arr4(size);

178 for

(uint64_t i = 0; i < size; i++) {

179

arr4[i] =

static_cast<float>

(std::rand()) /

180 static_cast<float>

(RAND_MAX / 999.99 - 0.99) -

185

arr4, 0,

int

(std::end(arr4) - std::begin(arr4)) - 1);

186

assert(std::is_sorted(std::begin(arr4_sorted), std::end(arr4_sorted)));

188

std::cout <<

"4th test: passed!\n"

;

191

std::cout <<

"\n\tPrinting all sorted arrays:\t\n"

;

193

std::cout <<

"1st array:\n"

;

195

std::cout << std::endl;

196

std::cout <<

"2nd array:\n"

;

198

std::cout << std::endl;

199

std::cout <<

"3rd array:\n"

;

201 int

(std::end(arr3) - std::begin(arr3)) - 1);

202

std::cout << std::endl;

203

std::cout <<

"Start: 4th array:\n\n"

;

205

arr4_sorted,

int

(std::end(arr4_sorted) - std::begin(arr4_sorted)) - 1);

206

std::cout <<

"\nEnd: 4th array.\n"

;

216

std::cout <<

"\tAvailable modes\t\n\n"

;

217

std::cout <<

"1. Self-tests mode\n2. Interactive mode"

;

219

std::cout <<

"\nChoose a mode: "

;

223 while

((choice != 1) && (choice != 2)) {

224

std::cout <<

"Invalid option. Choose between the valid modes: "

;

229

std::srand(std::time(

nullptr

));

231

}

else if

(choice == 2) {

233

std::cout <<

"\nEnter the number of elements: "

;

236

std::vector<float> arr(size);

239

<<

"\nEnter the unsorted elements (can be negative/decimal): "

;

241 for

(

int

i = 0; i < size; ++i) {

246

std::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