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/d3/d22/quick__sort__iterative_8cpp_source.html below:

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

Go to the documentation of this file. 33int partition

(std::vector<int> &arr,

int

start,

int

end)

36 int

index = start - 1;

38 for

(

int

j = start; j < end; j++) {

39 if

(arr[j] <= pivot) {

40

std::swap(arr[++index], arr[j]);

44

std::swap(arr[index + 1], arr[end]);

60

std::stack<int>

stack

;

62 int

end = arr.size()-1;

66 while

(!

stack

.empty())

73 int

pivotIndex =

partition

(arr,start,end);

75 if

(pivotIndex -1 > start)

97

std::vector<int> case1={100,534,1000000,553,10,61,2000,238,2756,9,12,56,30};

98

std::cout<<

"TEST 1\n"

;

99

std::cout<<

"Before: \n"

;

100 for

(

auto

x : case1) std::cout<<x<<

","

;

103

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

104

std::cout<<

"Test 1 succesful!\n"

;

105

std::cout<<

"After: \n"

;

106 for

(

auto

x : case1) std::cout<<x<<

","

;

110

std::vector<int> case2={-10,-2,-5,-2,-3746,-785,-123, -452, -32456};

111

std::cout<<

"TEST 2\n"

;

112

std::cout<<

"Before: \n"

;

113 for

(

auto

x : case2) std::cout<<x<<

","

;

116

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

117

std::cout<<

"Test 2 succesful!\n"

;

118

std::cout<<

"After: \n"

;

119 for

(

auto

x : case2) std::cout<<x<<

","

;

for std::invalid_argument

void push(const value_type &item)

void iterativeQuickSort(std::vector< int > &arr)

The main sorting function.

int partition(std::vector< int > &arr, int start, int end)

The partition function sorts the array from start to end and uses the last element as the pivot.

void tests()

Self-test implementations.

int main()

Main function.


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