-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDutchFlag.cpp
60 lines (52 loc) · 977 Bytes
/
DutchFlag.cpp
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
#include <iostream>
#include <random>
using namespace std;
void exchange(int arr[], int i, int j){
if(i<0)
return;
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
void print(int arr[], int n){
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main() {
int n=2;
int ptr1=-1,ptr2=0,ptr3=n-1,current,flag=0;
default_random_engine gen;
uniform_int_distribution<int> dist(1,3);
int arr[n];
for(int i=0;i<n;i++){
arr[i]=dist(gen);
}
print(arr,n);
while(ptr2<ptr3){
current=arr[ptr2];
if(current==1){
exchange(arr,ptr1,ptr2);
if(ptr1>=0){
ptr1++;
}
ptr2++;
}
else if(current==3){
while(arr[ptr3]==3 and ptr3>ptr2){
ptr3--;
}
exchange(arr,ptr2,ptr3);
}
else{
if(!flag){
ptr1=ptr2;
flag=1;
}
ptr2++;
}
}
print(arr,n);
return 0;
}