-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortlikedlistof0s1s2s.cpp
More file actions
162 lines (131 loc) · 2.66 KB
/
Copy pathsortlikedlistof0s1s2s.cpp
File metadata and controls
162 lines (131 loc) · 2.66 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//{ Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
void sort012(int a[], int n) {
int low = 0;
int high = n - 1;
int mid = 0;
while (mid <= high) {
if (a[mid] == 0) {
swap(a[low], a[mid]);
low++;
mid++;
} else if (a[mid] == 1) {
mid++;
} else if (a[mid] == 2) {
swap(a[mid], a[high]);
high--;
}
}
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while(t--){
int n;
cin >>n;
int a[n];
for(int i=0;i<n;i++){
cin >> a[i];
}
Solution ob;
ob.sort012(a, n);
for(int i=0;i<n;i++){
cout << a[i] << " ";
}
cout << endl;
}
return 0;
}
// } Driver Code Ends
// C++ Program to sort a linked list 0s, 1s or 2s
#include <bits/stdc++.h>
using namespace std;
/* Link list node */
class Node
{
public:
int data;
Node* next;
};
// Function to sort a linked list of 0s, 1s and 2s
void sortList(Node *head)
{
int count[3] = {0, 0, 0}; // Initialize count of '0', '1' and '2' as 0
Node *ptr = head;
/* count total number of '0', '1' and '2'
* count[0] will store total number of '0's
* count[1] will store total number of '1's
* count[2] will store total number of '2's */
while (ptr != NULL)
{
count[ptr->data] += 1;
ptr = ptr->next;
}
int i = 0;
ptr = head;
/* Let say count[0] = n1, count[1] = n2 and count[2] = n3
* now start traversing list from head node,
* 1) fill the list with 0, till n1 > 0
* 2) fill the list with 1, till n2 > 0
* 3) fill the list with 2, till n3 > 0 */
while (ptr != NULL)
{
if (count[i] == 0)
++i;
else
{
ptr->data = i;
--count[i];
ptr = ptr->next;
}
}
}
/* Function to push a node */
void push (Node** head_ref, int new_data)
{
/* allocate node */
Node* new_node = new Node();
/* put in the data */
new_node->data = new_data;
/* link the old list of the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Function to print linked list */
void printList(Node *node)
{
while (node != NULL)
{
cout << node->data << " ";
node = node->next;
}
cout << endl;
}
/* Driver code*/
int main(void)
{
Node *head = NULL;
push(&head, 0);
push(&head, 1);
push(&head, 0);
push(&head, 2);
push(&head, 1);
push(&head, 1);
push(&head, 2);
push(&head, 1);
push(&head, 2);
cout << "Linked List before Sorting\n";
printList(head);
sortList(head);
cout << "Linked List after Sorting\n";
printList(head);
return 0;
}