-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.cpp
More file actions
39 lines (36 loc) · 675 Bytes
/
4.cpp
File metadata and controls
39 lines (36 loc) · 675 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
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
void sort012(int arr[], int n){
int s = 0, e = n-1, i = 0;
while(i<=e){
if(arr[i]==1){
i++;
continue;
}
if(arr[i]==0){
swap(&arr[s++], &arr[i++]);
continue;
}
if(arr[i]==2){
swap(&arr[e--], &arr[i]);
continue;
}
}
}
int main(){
//program to sort 0, 1, 2 in On time.
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
sort012(arr, n);
return 0;
}