Skip to content

Commit 468ae47

Browse files
authored
Merge pull request #3742 from Ishitamukherjee2004/new-branch
Solution of Sort 0s, 1s and 2s from gfg is added
2 parents 020e383 + ce618f5 commit 468ae47

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
---
2+
id: sort-0s-1s-2s
3+
title: Sort 0s, 1s And 2s
4+
sidebar_label: Sort-0s-1s-and-2s
5+
tags:
6+
- Arrays
7+
- Sorting
8+
description: "This tutorial covers the solution to the SSort 0s, 1s and 2s problem from the GeeksforGeeks website."
9+
---
10+
## Problem Description
11+
Given an array of size `N` containing only `0s`, `1s`, and `2s`; sort the array in ascending order.
12+
13+
## Examples
14+
15+
**Example 1:**
16+
17+
```
18+
Input:
19+
N = 5
20+
arr[]= {0 2 1 2 0}
21+
Output:
22+
0 0 1 2 2
23+
Explanation:
24+
0s 1s and 2s are segregated
25+
into ascending order.
26+
```
27+
28+
**Example 2:**
29+
30+
```
31+
Input:
32+
N = 3
33+
arr[] = {0 1 0}
34+
Output:
35+
0 0 1
36+
Explanation:
37+
0s 1s and 2s are segregated
38+
into ascending order.
39+
```
40+
41+
## Your Task
42+
You don't need to read input or print anything. Your task is to complete the function sort012() that takes an array arr and N as input parameters and sorts the array in-place.
43+
44+
45+
46+
Expected Time Complexity: O(N)
47+
48+
Expected Auxiliary Space: O(1)
49+
50+
## Constraints
51+
52+
* `1 <= N <= 10^6`
53+
54+
## Problem Explanation
55+
56+
The task is to traverse the array and sort 0s, 1s, 2s.
57+
58+
## Code Implementation
59+
60+
### C++ Solution
61+
62+
```cpp
63+
void sortArray(vector<int>& arr) {
64+
int low = 0, mid = 0, high = arr.size() - 1;
65+
while (mid <= high) {
66+
if (arr[mid] == 0) {
67+
swap(arr[low], arr[mid]);
68+
low++;
69+
mid++;
70+
} else if (arr[mid] == 1) {
71+
mid++;
72+
} else {
73+
swap(arr[mid], arr[high]);
74+
high--;
75+
}
76+
}
77+
}
78+
```
79+
80+
```java
81+
public void sortArray(int[] arr) {
82+
int low = 0, mid = 0, high = arr.length - 1;
83+
while (mid <= high) {
84+
if (arr[mid] == 0) {
85+
swap(arr, low, mid);
86+
low++;
87+
mid++;
88+
} else if (arr[mid] == 1) {
89+
mid++;
90+
} else {
91+
swap(arr, mid, high);
92+
high--;
93+
}
94+
}
95+
}
96+
public void swap(int[] arr, int i, int j) {
97+
int temp = arr[i];
98+
arr[i] = arr[j];
99+
arr[j] = temp;
100+
}
101+
102+
103+
```
104+
105+
```python
106+
def sortArray(arr):
107+
low, mid, high = 0, 0, len(arr) - 1
108+
while mid <= high:
109+
if arr[mid] == 0:
110+
arr[low], arr[mid] = arr[mid], arr[low]
111+
low += 1
112+
mid += 1
113+
elif arr[mid] == 1:
114+
mid += 1
115+
else:
116+
arr[mid], arr[high] = arr[high], arr[mid]
117+
high -= 1
118+
119+
120+
121+
```
122+
123+
```javascript
124+
function sortArray(arr) {
125+
let low = 0, mid = 0, high = arr.length - 1;
126+
while (mid <= high) {
127+
if (arr[mid] === 0) {
128+
[arr[low], arr[mid]] = [arr[mid], arr[low]];
129+
low++;
130+
mid++;
131+
} else if (arr[mid] === 1) {
132+
mid++;
133+
} else {
134+
[arr[mid], arr[high]] = [arr[high], arr[mid]];
135+
high--;
136+
}
137+
}
138+
}
139+
140+
141+
```
142+
143+
```typescript
144+
function sortArray(arr) {
145+
let low = 0, mid = 0, high = arr.length - 1;
146+
while (mid <= high) {
147+
if (arr[mid] === 0) {
148+
[arr[low], arr[mid]] = [arr[mid], arr[low]];
149+
low++;
150+
mid++;
151+
} else if (arr[mid] === 1) {
152+
mid++;
153+
} else {
154+
[arr[mid], arr[high]] = [arr[high], arr[mid]];
155+
high--;
156+
}
157+
}
158+
}
159+
160+
```
161+
162+
## Solution Logic:
163+
The solution uses a variation of the Dutch National Flag algorithm, which is used to sort an array of 0s, 1s, and 2s. The algorithm uses three pointers: low, mid, and high. The low pointer is used to track the position of the next 0, the mid pointer is used to track the position of the next 1, and the high pointer is used to track the position of the next 2.
164+
165+
166+
167+
168+
## Time Complexity
169+
170+
* The time complexity is $O(N)$,where N is the length of the array. This is because we only need to iterate through the array once to sort it.
171+
172+
173+
174+
## Space Complexity
175+
176+
* The auxiliary space complexity is $O(1)$ due to the algorithm only uses a fixed amount of space to store the indices and the target element.

0 commit comments

Comments
 (0)