โ— LIVE
OpenAI releases GPT-5 APIIndia AI startup raises $120MBitcoin ETF hits record inflowsMeta Llama 4 benchmarks leakedOpenAI releases GPT-5 APIIndia AI startup raises $120MBitcoin ETF hits record inflowsMeta Llama 4 benchmarks leaked
๐Ÿ“… Mon, 23 Mar, 2026โœˆ๏ธ Telegram
AiFeed24

AI & Tech News

๐Ÿ”
โœˆ๏ธ Follow
๐Ÿ Home๐Ÿค–AI๐Ÿ’ปTech๐Ÿš€Startupsโ‚ฟCrypto๐Ÿ”’Security๐Ÿ‡ฎ๐Ÿ‡ณIndiaโ˜๏ธCloud๐Ÿ”ฅDeals
โœˆ๏ธ News Channel๐Ÿ›’ Deals Channel
Home/Cloud & DevOps/๐Ÿ”ต Sort 0s, 1s and 2s (Dutch National Flag Algorithm)
โ˜๏ธCloud & DevOps

๐Ÿ”ต Sort 0s, 1s and 2s (Dutch National Flag Algorithm)

Sorting an array containing only 0s, 1s, and 2s is a classic problem in Data Structures. Dutch National Flag Algorithm, which is highly efficient. Given an array arr[] containing only 0, 1, and 2, sort the array in ascending order without using built-in sort. Input: [0, 1, 2, 0, 1, 2] Output: [0, 0,

โšกQuick SummaryAI generating...
N

Navin S

๐Ÿ“… Mar 22, 2026ยทโฑ 3 min readยทDev.to โ†—
โœˆ๏ธ Telegram๐• TweetWhatsApp
๐Ÿ“ก

Original Source

Dev.to

https://dev.to/shnavin/sort-0s-1s-and-2s-dutch-national-flag-algorithm-3ooi
Read Full โ†—

Sorting an array containing only 0s, 1s, and 2s is a classic problem in Data Structures.
It is commonly solved using the Dutch National Flag Algorithm, which is highly efficient.

๐Ÿ“Œ Problem Statement

Given an array arr[] containing only 0, 1, and 2, sort the array in ascending order without using built-in sort.

๐Ÿ” Examples

Example 1:

Input:  [0, 1, 2, 0, 1, 2]
Output: [0, 0, 1, 1, 2, 2]

Example 2:

Input:  [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]

๐Ÿง  Concept

We divide the array into three regions:

  • Left โ†’ all 0s
  • Middle โ†’ all 1s
  • Right โ†’ all 2s

We use three pointers:

  • low โ†’ position for next 0
  • mid โ†’ current element
  • high โ†’ position for next 2

๐Ÿ”„ Approach: One-Pass (Optimal Solution)

Step-by-Step:

  1. Initialize:
  • low = 0
  • mid = 0
  • high = n - 1
  1. Traverse while mid <= high:
  • If arr[mid] == 0:

    • Swap arr[low] and arr[mid]
    • low++, mid++
  • If arr[mid] == 1:

    • Move mid++
  • If arr[mid] == 2:

    • Swap arr[mid] and arr[high]
    • high-- (do NOT increment mid here)

๐Ÿ’ป Python Code

```python id="c1i8rm"
def sort_012(arr):
low = 0
mid = 0
high = len(arr) - 1

while mid <= high:
    if arr[mid] == 0:
        arr[low], arr[mid] = arr[mid], arr[low]
        low += 1
        mid += 1

    elif arr[mid] == 1:
        mid += 1

    else:  # arr[mid] == 2
        arr[mid], arr[high] = arr[high], arr[mid]
        high -= 1

return arr

Example

print(sort_012([0, 1, 2, 0, 1, 2]))




---

## ๐Ÿงพ Dry Run (Step-by-Step)

For:



```id="m3z2fg"
arr = [0, 1, 2, 0, 1, 2]
Step low mid high Array
1 0 0 5 [0, 1, 2, 0, 1, 2]
2 1 1 5 [0, 1, 2, 0, 1, 2]
3 1 1 4 [0, 1, 1, 0, 1, 2]
4 1 2 4 [0, 1, 1, 0, 1, 2]
5 2 3 4 [0, 0, 1, 1, 1, 2]

Final โ†’ [0, 0, 1, 1, 2, 2]

โšก Time and Space Complexity

  • Time Complexity: O(n) (single traversal)
  • Space Complexity: O(1) (in-place)

๐Ÿ” Alternative Approach (Counting Method)

```python id="6qz4ki"
def sort_012(arr):
count0 = arr.count(0)
count1 = arr.count(1)
count2 = arr.count(2)

i = 0
for _ in range(count0):
    arr[i] = 0
    i += 1
for _ in range(count1):
    arr[i] = 1
    i += 1
for _ in range(count2):
    arr[i] = 2
    i += 1

return arr



---

## ๐Ÿงฉ Where Is This Used?

* Partitioning problems
* QuickSort optimizations
* Interview questions

---

## ๐Ÿ Conclusion

The **Dutch National Flag Algorithm** is the best solution for this problem because it:

* Works in **one pass**
* Uses **constant space**
* Is highly efficient

Tags:#cloud#dev.to

Found this useful? Share it!

โœˆ๏ธ Telegram๐• TweetWhatsApp

Read the Full Story

Continue reading on Dev.to

Visit Dev.to โ†—

Related Stories

โ˜๏ธ
โ˜๏ธCloud & DevOps

How to Make Claude, Codex, and Gemini Collaborate on Your Codebase

about 1 hour ago

How I Set Up Server-Side GTM Tracking with Stape.io, GA4 and Google Ads on a Drupal 11 Marketplace
โ˜๏ธCloud & DevOps

How I Set Up Server-Side GTM Tracking with Stape.io, GA4 and Google Ads on a Drupal 11 Marketplace

about 1 hour ago

โ˜๏ธ
โ˜๏ธCloud & DevOps

NPR Music: Kronos Quartet: Tiny Desk Concert

about 1 hour ago

โ˜๏ธ
โ˜๏ธCloud & DevOps

The Morning vs Evening Habit Tracker: What the Data Actually Shows

about 1 hour ago

๐Ÿ“ก Source Details

Dev.to

๐Ÿ“… Mar 22, 2026

๐Ÿ• 1 day ago

โฑ 3 min read

๐Ÿ—‚ Cloud & DevOps

Read Original โ†—

Web Hosting

๐ŸŒ Hostinger โ€” 80% Off Hosting

Start your website for โ‚น69/mo. Free domain + SSL included.

Claim Deal โ†’

๐Ÿ“ฌ AiFeed24 Daily

Top 5 AI & tech stories every morning. Join 40,000+ readers.

โœฆ 40,218 subscribers ยท No spam, ever

Cloud Hosting

โ˜๏ธ Vultr โ€” $100 Free Credit

Deploy cloud servers in 25+ locations. From $2.50/mo. No contract.

Claim $100 Credit โ†’
AiFeed24

India's AI-powered tech news hub. Daily coverage of AI, startups, crypto and emerging technology.

โœˆ๏ธ๐Ÿ›’

Topics

Artificial IntelligenceStartups & VCCryptocurrencyCybersecurityCloud & DevOpsIndia Tech

Company

About AiFeed24Write For UsContact

Daily Digest

Top 5 AI stories every morning. 40,000+ readers.

No spam, ever.

ยฉ 2026 AiFeed24 Media.Affiliate Disclosure โ€” We earn commission on qualifying purchases at no extra cost to you.
PrivacyTermsCookies