-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort_Iterative_Using_HoarePartitionScheme.cpp
More file actions
102 lines (86 loc) · 2.01 KB
/
Copy pathQuickSort_Iterative_Using_HoarePartitionScheme.cpp
File metadata and controls
102 lines (86 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<iostream>
#include "stack"
using namespace std;
int Partition(int a[], int low, int high)
{
int pivot = a[low];
int i = low - 1;
int j = high + 1;
while(1)
{
do {
i++;
} while (a[i] < pivot);
do {
j--;
} while (a[j] > pivot);
if(i >= j)
return j;
swap(a[i], a[j]);
}
}
void QuickSort(int arr[],int size){
stack<pair<int,int>> stack1;
stack1.push({0,size-1});
int start ,end;
while(!stack1.empty()){
start = stack1.top().first , end = stack1.top().second;
stack1.pop();
int pivot = Partition(arr,start,end);
if(pivot - 1 > start) stack1.push({start,pivot });
if(pivot + 1 < end) stack1.push({pivot + 1, end });
}
}
int main()
{
int arr[] = { 3, 8, 5, 4, 1, 9, -2 };
int size = (*(&arr + 1) - arr);
QuickSort(arr, size );
for (int k = 0; k < (*(&arr + 1) - arr); ++k) {
cout<<arr[k]<<" ";
}cout<<"\n";
return 0;
}
#include<iostream>
#include "stack"
using namespace std;
int Partition(int a[], int low, int high)
{
int pivot = a[low];
int i = low - 1;
int j = high + 1;
while(1)
{
do {
i++;
} while (a[i] < pivot);
do {
j--;
} while (a[j] > pivot);
if(i >= j)
return j;
swap(a[i], a[j]);
}
}
void QuickSort(int arr[],int size){
stack<pair<int,int>> stack1;
stack1.push({0,size-1});
int start ,end;
while(!stack1.empty()){
start = stack1.top().first , end = stack1.top().second;
stack1.pop();
int pivot = Partition(arr,start,end);
if(pivot - 1 > start) stack1.push({start,pivot });
if(pivot + 1 < end) stack1.push({pivot + 1, end });
}
}
int main()
{
int arr[] = { 3, 8, 5, 4, 1, 9, -2 };
int size = (*(&arr + 1) - arr);
QuickSort(arr, size );
for (int k = 0; k < (*(&arr + 1) - arr); ++k) {
cout<<arr[k]<<" ";
}cout<<"\n";
return 0;
}