-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsegregate.cpp
More file actions
35 lines (32 loc) · 831 Bytes
/
segregate.cpp
File metadata and controls
35 lines (32 loc) · 831 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
//sort an array of three types of element 0's, 1's and 2's with linear time and const extra space
#include <iostream>
using namespace std;
void Segregate(int arr[], int n){
int low = 0, mid = 0, high = n-1;
while(mid<=high){
switch (arr[mid]){
case 0: swap(arr[low], arr[mid]);
low++;mid++;
break;
case 1: mid++;
break;
case 2: swap(arr[mid],arr[high]);
high--; mid++;
break;
}
}
}
int main(){
int T;
cin>>T;
while(T--){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
Segregate(arr,n);
for(int i=0;i<n;i++) cout<<arr[i]<<" ";
}
}