-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort012.cpp
More file actions
41 lines (37 loc) · 734 Bytes
/
sort012.cpp
File metadata and controls
41 lines (37 loc) · 734 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
// Similar Logic of Dutch National Flag problem
#include<iostream>
using namespace std;
void sortArray(int arr[], int n)
{
int low=0;
int mid=0;
int high=n-1;
while(mid<=high)
{
switch (arr[mid])
{
case 0:
swap(arr[low++],arr[mid++]);
break;
case 1:
mid++;
break;
case 2:
swap(arr[mid], arr[high--]);
break;
}
}
}
void printArray(int arr[], int arr_size)
{
for (int i = 0; i < arr_size; i++)
cout << arr[i] << " ";
}
int main()
{
int arr[8]={0,1,2,2,0,1,0,2};
int n=8;
sortArray(arr, n);
cout<<"After sorting : ";
printArray(arr, n);
}