โ— 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
๐Ÿ“… Sat, 21 Mar, 2026โœˆ๏ธ Telegram
AiFeed24

AI & Tech News

๐Ÿ”
โœˆ๏ธ Follow
๐Ÿ Home๐Ÿค–AI๐Ÿ’ปTech๐Ÿš€Startupsโ‚ฟCrypto๐Ÿ”’Security๐Ÿ‡ฎ๐Ÿ‡ณIndiaโ˜๏ธCloud๐Ÿ”ฅDeals
โœˆ๏ธ News Channel๐Ÿ›’ Deals Channel
Home/Cloud & DevOps/Sorting an Array of 0s, 1s and 2s - CA08
โ˜๏ธCloud & DevOps

Sorting an Array of 0s, 1s and 2s - CA08

Introduction Sorting is a fundamental concept in programming. In this problem, we are given an array that contains only three distinct values: 0s, 1s, and 2s. The goal is to sort the array in ascending order without using any built-in sorting functions. This problem is important because it introduce

โšกQuick SummaryAI generating...
C

Christina Sharon S

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

Original Source

Dev.to

https://dev.to/christina_sharons_2b3205/sorting-an-array-of-0s-1s-and-2s-ca08-5d58
Read Full โ†—

Introduction

Sorting is a fundamental concept in programming. In this problem, we are given an array that contains only three distinct values: 0s, 1s, and 2s. The goal is to sort the array in ascending order without using any built-in sorting functions.

This problem is important because it introduces an efficient technique known as the Dutch National Flag Algorithm.

Problem Statement

Given an array arr[] containing only 0s, 1s, and 2s, sort the array in ascending order.

Constraints:

  • Do not use built-in sorting functions
  • Aim for an efficient solution

Example 1

Input:

arr = [0, 1, 2, 0, 1, 2]

Output:

[0, 0, 1, 1, 2, 2]

Example 2

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]

Approach 1: Counting Method

Python Implementation (Counting)

def sort_array(arr):
    count0 = arr.count(0)
    count1 = arr.count(1)
    count2 = arr.count(2)

    i = 0

    # Fill 0s
    for _ in range(count0):
        arr[i] = 0
        i += 1

    # Fill 1s
    for _ in range(count1):
        arr[i] = 1
        i += 1

    # Fill 2s
    for _ in range(count2):
        arr[i] = 2
        i += 1

    return arr

# Example usage
arr = [0, 1, 2, 0, 1, 2]
print(sort_array(arr))

Conclusion

Sorting an array of 0s, 1s, and 2s is a classic problem that demonstrates how understanding patterns in data can lead to highly efficient solutions. The Dutch National Flag Algorithm is the most optimal approach and is widely used in coding interviews.

Mastering this problem helps in building strong problem-solving skills and understanding in-place algorithms.

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

Majority Element

about 2 hours ago

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

Building a SQL Tokenizer and Formatter From Scratch โ€” Supporting 6 Dialects

about 2 hours ago

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

Markdown Knowledge Graph for Humans and Agents

about 2 hours ago

Moving Beyond Disk: How Redis Supercharges Your App Performance
โ˜๏ธCloud & DevOps

Moving Beyond Disk: How Redis Supercharges Your App Performance

about 2 hours ago

๐Ÿ“ก Source Details

Dev.to

๐Ÿ“… Mar 21, 2026

๐Ÿ• about 4 hours 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