-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsort_012.cpp
More file actions
72 lines (60 loc) · 1.5 KB
/
sort_012.cpp
File metadata and controls
72 lines (60 loc) · 1.5 KB
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
62
63
64
65
66
67
68
69
70
71
72
/*
You are given an integer array/list(ARR) of size N. It contains only 0s, 1s and 2s.
Write a solution to sort this array/list in a 'single scan'.
'Single Scan' refers to iterating over the array/list just once or to put it in other words,
you will be visiting each element in the array/list just once.
*/
#include <iostream>
using namespace std;
void sort012(int arr[], int n);
void printArray(int arr[], int n);
int main() {
int arr[6] = {0, 1, 2, 0, 1, 2};
sort012(arr, 6);
printArray(arr, 6);
return 0;
}
void shift(int arr[], int start, int end);
void sort(int arr[], int index);
void sort012(int arr[], int n) {
int low=0;
int mid=0;
int high=n-1;
for(int i=0;i<n;i++)
{
printArray(arr,n);
cout << low << " " << mid << " " << high << endl;
if(arr[mid]==0)
{
int temp=arr[low];
arr[low]=arr[mid];
arr[mid]=temp;
low++;
mid++;
}
else if(arr[mid]==1)
{
mid++;
}
else if(arr[mid]==2)
{
int temp=arr[high];
arr[high]=arr[mid];
arr[mid]=temp;
high--;
}
}
}
void shift(int arr[], int newPlace, int current) {
int temp = arr[current];
for (int i = current - 1; i >= newPlace; i--) {
arr[i+1] = arr[i];
}
arr[newPlace] = temp;
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}