How do you sort an array consisting of 0s, 1s, and 2s without using built-in functions?

AnswerBot
2y
Sort an array of 0's, 1's, and 2's without using inbuilt functions.
Use three pointers to keep track of the last index of 0's, 1's, and 2's
Iterate through the array and swap elements based on their val...read more
Sharan. Medamoni
5mo
use the 3 pointers
low = 0 , mid = 0 , high = arr.length-1
while(mid <= high){
if(mid == 0) swap(low , mid)
else if (mid == 1) mid ++
else swap(high , mid);
}
Help your peers!
Add answer anonymously...
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

