-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort-0-1.cpp
More file actions
53 lines (45 loc) · 817 Bytes
/
Copy pathSort-0-1.cpp
File metadata and controls
53 lines (45 loc) · 817 Bytes
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
//By counting
void sortZeroesAndOne(int arr[], int size)
{
//Write your code here
int count0 = 0;
for(int i = 0; i < size; i++)
{
if(arr[i] == 0)
{
count0++;
}
}
for(int i = 0; i < count0; i++)
{
arr[i] = 0;
}
for(int i = count0; i < size; i++)
{
arr[i] = 1;
}
}
//By two-pointer approach
void sortZeroesAndOne2(int input[], int size)
{
//Write your code here
int i = 0;
int j = size - 1;
while(i < j)
{
while(input[i] == 0 && i < j)
{
i++;
}
while(input[j] == 1 && i < j)
{
j--;
}
int temp;
temp = input[i];
input[i] = input[j];
input[j] = temp;
i++;
j--;
}
}