You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Suppose you have an array of N elements, containing three distinct keys, "true", "false", and "maybe". Given an O(N) algorithm to rearrange the list so that all "false" elements precede "maybe" elements, which in turn precede "true" elements. You may use only constant extra space.
3
+
*/
4
+
voidMySort( ElementTypeA[], intN ) {
5
+
intfalseCount=0, maybeCount=0, trueCount=0;
6
+
for (inti=0; i<N; i++) {
7
+
if (A[i] == false) falseCount++;
8
+
elseif (A[i] ==maybe) maybeCount++;
9
+
elsetrueCount++;
10
+
}
11
+
for (inti=0; i<falseCount; i++) A[i] = false;
12
+
for (inti=falseCount; i<falseCount+maybeCount; i++) A[i] =maybe;
13
+
for (inti=falseCount+maybeCount; i<N; i++) A[i] = true;
0 commit comments