Mar 7, 2022

Sort an array of 0s, 1s and 2s

#include using namespace std; void printArray(int a[], int len) { for(int i=0;i left && a[right] == val) right--; int t=a[left]; a[left] = a[right]; a[right] = t; left++; right--; } else left++; } if(a[right] == val) right--; return right; } int main() { // your code goes here int a[] = {1,0,2,0,1,2,1,2,0,1,1}; cout << "before arrangement- "; printArray(a, sizeof(a)/sizeof(int)); int twoIndex = groupZeroOneTwo(a, sizeof(a)/sizeof(int) - 1, 2); cout << "after arrangement1- "; printArray(a, sizeof(a)/sizeof(int)); groupZeroOneTwo(a, twoIndex, 1); cout << "after arrangement2- "; printArray(a, sizeof(a)/sizeof(int)); return 0; }

No comments: