-
Notifications
You must be signed in to change notification settings - Fork 45
/
10_Sort_0_1_2.cpp
61 lines (48 loc) · 1.18 KB
/
10_Sort_0_1_2.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
using namespace std;
void inputArray(int arr[], int size) {
for(int i=0; i<size; i++) {
cin>>arr[i];
}
}
void printArray(int arr[], int size) {
for(int i=0; i<size; i++) {
cout<<arr[i]<<" ";
}
cout<<endl;
}
// DNF (Dutch-National-Flag) Algorithm
// Red - White - Blue Ball Questions
// low - mid - high variables (3 pointers)
void sort012(int arr[], int size) {
int low = 0;
int mid = 0;
int high = size-1;
while(mid <= high) {
switch(arr[mid]) {
case 0:
swap(arr[mid++], arr[low++]);
break;
case 1:
mid++;
break;
case 2:
swap(arr[mid], arr[high--]);
break;
}
}
}
int main() {
int size;
int arr[100];
cout<<"Enter the size of array : ";
cin>>size;
cout<<"Enter the elements of array : ";
inputArray(arr, size);
cout<<"Array before sorting : ";
printArray(arr, size);
sort012(arr, size);
cout<<"Array after sorting : ";
printArray(arr, size);
return 0;
}