Sort an array of zeros, ones and twos in linear time complexity
Given an array consisting only 0's, 1's and 2's. Give an algorithm for sorting the array in O(n) time complexity ( in the sorted array, 0's will be at starting ,then the 1's & then the 2's).
Swap (array [low], array [mid]) since 0's should be at starting
Keep as it is since 1's should be at middle after being sorted
Swap (array [mid], array [high])
C++ program to sort an array of 0's, 1's and 2's in linear time complexity
Outputneed an explanation for this answer? contact us directly to get an explanation for this answer