DEV Community

Saranya R
Saranya R

Posted on

Sort 0s, 1s, and 2s

Approach Explanation (o゜▽゜)o☆

  1. I used three pointers: low=0, mid=0, and high=len(arr)-1.
  2. I placed 0's at the beginning by swapping with low.
  3. I left 1's in the middle.
  4. I placed 2's at the end by swapping with high.
  5. I repeated until mid-crossed high, and the array was sorted.

Methods Used ( ノ ^-^)ノ

  • Dutch National Flag algorithm (single pass)
  • Three-pointer technique (low, mid, high)
  • Constant space (no extra array used)

Top comments (0)