-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomized_QuickSort_Iterative.cpp
More file actions
53 lines (45 loc) · 1.22 KB
/
Copy pathRandomized_QuickSort_Iterative.cpp
File metadata and controls
53 lines (45 loc) · 1.22 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
#include<iostream>
#include "stack"
using namespace std;
int Partition(int a[], int start, int end)
{
int pivot = a[end];
int pIndex = start;
for (int i = start; i < end; i++)
{
if (a[i] <= pivot)
{
swap(a[i], a[pIndex]);
pIndex++;
}
}
swap (a[pIndex], a[end]);
return pIndex;
}
int RandomizedPartition(int arr[],int start ,int end ){
int pivotIndex = rand() % (end - start + 1) + start;
int temp = arr[pivotIndex]; arr[pivotIndex] = arr[end]; arr[end] = temp;
return Partition(arr,start,end);
}
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 = RandomizedPartition(arr,start,end);
if(pivot - 1 > start) stack1.push({start,pivot - 1});
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;
}