-
Notifications
You must be signed in to change notification settings - Fork 1
/
dutchflag.cpp
49 lines (40 loc) · 973 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
#include<iostream>
using namespace std;
void swapper(int array[],int size,int firstIndex,int secondIndex);
int main()
{
int array[] = {0,1,0,1,2,1,2,2,2,0,0,0};
int size = sizeof(array)/sizeof(array[0]);
int low,mid,high;
low = mid = 0;
high = size-1;
while(mid <= high)
{
switch(array[mid])
{
case 0:
swapper(array,size,low,mid);
low++;
mid++;
break;
case 1:
mid++;
break;
case 2:
swapper(array,size,mid,high);
high--;
break;
}
}
for(int i=0; i<size;i++)
{
cout<<array[i]<<" ";
}
}
void swapper(int array[],int size,int firstIndex,int secondIndex)
{
int temp;
temp = array[firstIndex];
array[firstIndex] = array[secondIndex];
array[secondIndex] = temp;
}