CA 08 - Sort 0s 1s and 2s
Problem Statement Given an array arr[] containing only 0s, 1s, and 2s. Sort the array in ascending order. Note: You need to solve this problem without utilizing the built-in sort function. Examples...

Source: DEV Community
Problem Statement Given an array arr[] containing only 0s, 1s, and 2s. Sort the array in ascending order. Note: You need to solve this problem without utilizing the built-in sort function. Examples: Input: arr[] = [0, 1, 2, 0, 1, 2] Output: [0, 0, 1, 1, 2, 2] Explanation: 0s, 1s and 2s are segregated into ascending order. Input: arr[] = [0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1] Output: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] Explanation: 0s, 1s and 2s are segregated into ascending order. My Goal For this problem, my goal was to: Avoid using built-in sorting methods Understand how to rearrange elements efficiently Solve the problem in a single pass Use constant extra space Solution I used the Dutch National Flag Algorithm, which is perfect for this type of problem. Idea: We maintain three pointers: l → position for next 0 m → current element h → position for next 2 Steps: If element is 0 → swap with l, move both l and m If element is 1 → just move m If element is 2 → swap with h, move h This en