<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/day-11-two-pointers-sort-colors-dutch-national-flag--l4r1ilvs0" -->

---
title: Day 11: Two Pointers — Sort Colors (Dutch National Flag)
description: A walkthrough of the Dutch National Flag algorithm (LeetCode Sort Colors) using three pointers to sort an array of 0s, 1s, and 2s in-place in O(n) time and...
canonical: https://daily.dev/posts/day-11-two-pointers-sort-colors-dutch-national-flag--l4r1ilvs0
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Day 11: Two Pointers — Sort Colors (Dutch National Flag) | daily.dev
og:description: A walkthrough of the Dutch National Flag algorithm (LeetCode Sort Colors) using three pointers to sort an array of 0s, 1s, and 2s in-place in O(n) time and...
og:url: https://daily.dev/posts/day-11-two-pointers-sort-colors-dutch-national-flag--l4r1ilvs0
og:image: https://api.daily.dev/og/posts/L4R1iLvS0.png
og:image:alt: Day 11: Two Pointers — Sort Colors (Dutch National Flag)
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Day 11: Two Pointers — Sort Colors (Dutch National Flag)

**[Medium](https://daily.dev/sources/medium_js)** · 5 min read · 2 upvotes · 0 comments

## Summary

A walkthrough of the Dutch National Flag algorithm (LeetCode Sort Colors) using three pointers to sort an array of 0s, 1s, and 2s in-place in O(n) time and O(1) space. Covers the key insight of maintaining three regions (confirmed 0s, unknown, confirmed 2s), explains why mid must not advance after swapping with right, includes a C++ implementation, step-by-step trace, edge cases, and complexity analysis.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://medium.com/@aljubinagavit/day-11-two-pointers-sort-colors-dutch-national-flag-4a3414609c04>

## Questions this post answers

### How do you sort an array of 0s, 1s, and 2s in place in one pass without using a built-in sort?

Use the Dutch National Flag algorithm with three pointers: left, mid, and right. Swap 0s found at mid with the left pointer and advance both; leave 1s in place and advance mid; swap 2s found at mid with the right pointer and move right backward without advancing mid, since the swapped-in value hasn't been checked yet. This runs in O(n) time and O(1) space.

_daily.dev surfaces algorithm walkthroughs like this for developers sharpening interview-ready coding skills._

### Why shouldn't you advance the mid pointer after swapping nums[mid] with nums[right] in the Sort Colors problem?

Advancing mid after that swap would skip classifying the newly swapped-in value from the right side, since it hasn't been checked yet. Not moving mid ensures every element gets examined and correctly placed into the 0, 1, or 2 region, and it's the detail most people trip up on when implementing this partitioning approach.

_Developers debugging tricky pointer logic can find similar step-by-step breakdowns on daily.dev._

## Similar posts on daily.dev

- [Stop Memorizing the Dutch National Flag Algorithm - Learn to Derive It Instead \(LeetCode 75\)](https://daily.dev/posts/stop-memorizing-the-dutch-national-flag-algorithm---learn-to-derive-it-instead-leetcode-75--lbqc8fr6v) · Medium · 0 upvotes · 0 comments
- [Day 5: Two Pointers — Move Zeroes](https://daily.dev/posts/day-5-two-pointers-move-zeroes-mhhjdckfe) · Medium · 0 upvotes · 0 comments
- [3 Sum Problem in DSA \(Example and Optimized Solution\)](https://daily.dev/posts/3-sum-problem-in-dsa-example-and-optimized-solution--67wpded8f) · C\# Corner · 0 upvotes · 0 comments
- [January 2, 2026](https://daily.dev/posts/january-2-2026-60wwwmhvi) · DevBlogs · 0 upvotes · 0 comments
- [How to Solve the Sock Merchant Problem in Java](https://daily.dev/posts/how-to-solve-the-sock-merchant-problem-in-java-o8tbca3pb) · Baeldung · 1 upvotes · 0 comments

---

Tags: [#algorithms](https://daily.dev/tags/algorithms), [#c++](https://daily.dev/tags/c++), [#data-structures](https://daily.dev/tags/data-structures)

[View this post on daily.dev](https://daily.dev/posts/day-11-two-pointers-sort-colors-dutch-national-flag--l4r1ilvs0)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Day 11: Two Pointers — Sort Colors (Dutch National Flag)","url":"https://daily.dev/posts/day-11-two-pointers-sort-colors-dutch-national-flag--l4r1ilvs0","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/day-11-two-pointers-sort-colors-dutch-national-flag--l4r1ilvs0"},"datePublished":"2026-07-23T19:45:48.065Z","dateModified":"2026-09-14T06:02:16.486Z","description":"A walkthrough of the Dutch National Flag algorithm (LeetCode Sort Colors) using three pointers to sort an array of 0s, 1s, and 2s in-place in O(n) time and...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/68e30ad14fdddb38b4fbf39375f95e5b?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/68e30ad14fdddb38b4fbf39375f95e5b?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Medium","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Medium","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/medium","url":"https://daily.dev/sources/medium_js"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/day-11-two-pointers-sort-colors-dutch-national-flag--l4r1ilvs0","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"algorithms,c++,data-structures","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Medium","item":"https://daily.dev/sources/medium_js"},{"@type":"ListItem","position":3,"name":"Day 11: Two Pointers — Sort Colors (Dutch National Flag)"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/day-11-two-pointers-sort-colors-dutch-national-flag--l4r1ilvs0#faq","mainEntity":[{"@type":"Question","name":"How do you sort an array of 0s, 1s, and 2s in place in one pass without using a built-in sort?","acceptedAnswer":{"@type":"Answer","text":"Use the Dutch National Flag algorithm with three pointers: left, mid, and right. Swap 0s found at mid with the left pointer and advance both; leave 1s in place and advance mid; swap 2s found at mid with the right pointer and move right backward without advancing mid, since the swapped-in value hasn't been checked yet. This runs in O(n) time and O(1) space. daily.dev surfaces algorithm walkthroughs like this for developers sharpening interview-ready coding skills."}},{"@type":"Question","name":"Why shouldn't you advance the mid pointer after swapping nums[mid] with nums[right] in the Sort Colors problem?","acceptedAnswer":{"@type":"Answer","text":"Advancing mid after that swap would skip classifying the newly swapped-in value from the right side, since it hasn't been checked yet. Not moving mid ensures every element gets examined and correctly placed into the 0, 1, or 2 region, and it's the detail most people trip up on when implementing this partitioning approach. Developers debugging tricky pointer logic can find similar step-by-step breakdowns on daily.dev."}}]}
```

