-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram22.cpp
More file actions
46 lines (38 loc) · 855 Bytes
/
program22.cpp
File metadata and controls
46 lines (38 loc) · 855 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
#include<iostream>
using namespace std;
void printarray(int arr[],int n){
for(int i = 0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
void sort012(int arr[],int n){
int low = 0;
int mid = 0;
int high = n-1;
while(mid <= high){
// int step = 0;
// cout<<"step "<<++step<<endl;
// printarray( arr, 9);
if(arr[mid]==0){
swap(arr[low],arr[mid]);
low++;
mid++;
}
if (arr[mid]==1){
mid++;
}
if(arr[mid]==2 && mid<= high){
swap(arr[mid],arr[high]);
high--;
}
}
}
int main(){
int arr[9] = {0,1,2,0,1,2,0,1,2};
printarray(arr,9);
cout<<"After sorting the array of elements 0,1 and 2 : "<<endl;
sort012(arr,9);
printarray(arr,9);
return 0;
}