{"id":24974399,"url":"https://github.com/shaldonbarnes10/sorting_0_1_2s","last_synced_at":"2025-03-29T06:44:58.015Z","repository":{"id":275016171,"uuid":"924791488","full_name":"Shaldonbarnes10/Sorting_0_1_2s","owner":"Shaldonbarnes10","description":"Sort an Array of 0s, 1s, and 2s in a Single Scan. This project provides an efficient solution to sort an array consisting only of 0s, 1s, and 2s using a single-pass algorithm (Dutch National Flag Algorithm) Three pointer. Given multiple test cases, the algorithm ensures optimal performance with a time complexity of O(N) and space complexity of O(1)","archived":false,"fork":false,"pushed_at":"2025-02-02T17:31:31.000Z","size":46,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-29T06:44:55.703Z","etag":null,"topics":["algorithms-and-data-structures","codestudio","cpp","dsa-algorithm","dutch-nationalflag-problem"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Shaldonbarnes10.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-30T16:55:54.000Z","updated_at":"2025-02-02T17:31:35.000Z","dependencies_parsed_at":"2025-01-30T18:41:55.385Z","dependency_job_id":null,"html_url":"https://github.com/Shaldonbarnes10/Sorting_0_1_2s","commit_stats":null,"previous_names":["shaldonbarnes10/sorting_0_1_2s"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shaldonbarnes10%2FSorting_0_1_2s","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shaldonbarnes10%2FSorting_0_1_2s/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shaldonbarnes10%2FSorting_0_1_2s/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shaldonbarnes10%2FSorting_0_1_2s/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Shaldonbarnes10","download_url":"https://codeload.github.com/Shaldonbarnes10/Sorting_0_1_2s/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246150417,"owners_count":20731419,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithms-and-data-structures","codestudio","cpp","dsa-algorithm","dutch-nationalflag-problem"],"created_at":"2025-02-03T20:03:58.280Z","updated_at":"2025-03-29T06:44:57.986Z","avatar_url":"https://github.com/Shaldonbarnes10.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sorting an Array of 0s, 1s, and 2s using the Three-Pointer Approach (Dutch National Flag Algorithm)\n\n## Overview\nThis repository implements an efficient algorithm to sort an array consisting of only `0`s, `1`s, and `2`s. The algorithm follows the **Dutch National Flag Algorithm**, which uses three pointers to partition the array in a single pass, ensuring an optimal time complexity of **O(n)**.\n\n## Problem Statement\nGiven an array consisting only of the numbers `0`, `1`, and `2`, sort it in ascending order **in-place** without using any extra space.\n\n### Example\n#### **Input:**\n```\narr[] = {0, 1, 2, 1, 2, 1, 2}\n```\n\n#### **Output:**\n```\n0 1 1 1 2 2 2\n```\n\n## Approach: Three-Pointer Technique\nWe use three pointers:\n1. **Low Pointer (`low`)**: Marks the boundary where `0`s should be placed.\n2. **Mid Pointer (`mid`)**: Iterates through the array, processing elements.\n3. **High Pointer (`high`)**: Marks the boundary where `2`s should be placed.\n\n### **Algorithm Steps:**\n1. Initialize three pointers:\n   - `low = 0` (beginning of array)\n   - `mid = 0` (current element being processed)\n   - `high = n - 1` (end of array)\n\n2. Traverse the array while `mid \u003c= high`:\n   - **If `arr[mid] == 0`**:\n     - Swap `arr[mid]` and `arr[low]`\n     - Increment `low` and `mid`\n   \n   - **If `arr[mid] == 1`**:\n     - Leave as is, simply increment `mid`\n   \n   - **If `arr[mid] == 2`**:\n     - Swap `arr[mid]` and `arr[high]`\n     - Decrement `high` (do not increment `mid` immediately, as swapped value needs processing)\n\n### **Code Implementation (C++)**\n```cpp\n#include \u003ciostream\u003e\n#include \u003carray\u003e\nusing namespace std;\n\nvoid sort012(array\u003cint, 7\u003e\u0026 arr) {\n    int low = 0, mid = 0, high = arr.size() - 1;\n    \n    while (mid \u003c= high) {\n        if (arr[mid] == 0) {\n            swap(arr[low], arr[mid]);\n            low++;\n            mid++;\n        } else if (arr[mid] == 1) {\n            mid++;\n        } else { // arr[mid] == 2\n            swap(arr[mid], arr[high]);\n            high--;\n        }\n    }\n}\n\nint main() {\n    array\u003cint, 7\u003e arr = {0, 1, 2, 1, 2, 1, 2};\n    sort012(arr);\n    \n    cout \u003c\u003c \"Sorted array: \";\n    for (int i = 0; i \u003c arr.size(); i++) {\n        cout \u003c\u003c arr[i] \u003c\u003c \" \";\n    }\n    cout \u003c\u003c endl;\n    \n    return 0;\n}\n```\n\n### **Time Complexity Analysis**\n- **Best Case:** O(n)\n- **Worst Case:** O(n)\n- **Average Case:** O(n)\n\n### **Space Complexity**\n- **O(1)** (Sorting is performed in-place without extra space)\n\n## Why Use the Three-Pointer Approach?\n✅ **Single pass (O(n) complexity)** – Efficient for large datasets.  \n✅ **In-place sorting (O(1) space)** – No extra memory required.  \n✅ **Easy to implement and understand.**  \n\n## Conclusion\nThis algorithm is one of the most efficient ways to sort an array of `0`s, `1`s, and `2`s, ensuring optimal performance. The three-pointer approach minimizes swaps and avoids unnecessary operations, making it an excellent choice for real-world scenarios.\n\n---\n### **Contributions**\nFeel free to fork this repository, suggest improvements, or report any issues!\n\n---\n**Author:** Shaldon Barnes  \n**License:** MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshaldonbarnes10%2Fsorting_0_1_2s","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshaldonbarnes10%2Fsorting_0_1_2s","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshaldonbarnes10%2Fsorting_0_1_2s/lists"}