{"id":11478,"date":"2026-02-06T21:58:12","date_gmt":"2026-02-06T16:28:12","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=11478"},"modified":"2026-02-07T21:18:16","modified_gmt":"2026-02-07T15:48:16","slug":"radix-sort","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/radix-sort\/","title":{"rendered":"Radix Sort"},"content":{"rendered":"\n<!-- PrismJS for Syntax Highlighting -->\n<link\n    href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.min.css\"\n    rel=\"stylesheet\"\n\/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.min.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n.wp_blog_theme {\n  --primary: #E58C32;\n  --secondary: #030302;\n  --light-bg: #fef9f4;\n  --text-dark: #2d2d2d;\n  --tab-radius: 12px;\n  --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);\n  --code-bg: #001f3f;\n  --code-text: #d4f1ff;\n}\n\n.wp_blog_container {\n  font-family: 'Segoe UI', sans-serif;\n  background: var(--light-bg);\n  margin: 0;\n  padding: 0;\n  color: var(--text-dark);\n}\n\n\/* Heading *\/\n.wp_blog_main-heading {\n  text-align: center;\n  font-size: 2.4rem;\n  color: var(--primary);\n  margin-top: 2.5rem;\n  font-weight: bold;\n}\n\n\/* Explanation Card *\/\n.wp_blog_explanation,\n.wp_blog_code-tabs-container {\n  max-width: 940px;\n  margin: 2rem auto;\n  padding: 2rem;\n  background: white;\n  border-radius: var(--tab-radius);\n  box-shadow: var(--shadow);\n}\n\n\/* Text and Visuals *\/\n.wp_blog_explanation h2 {\n  font-size: 1.4rem;\n  color: var(--primary);\n  margin-bottom: 0.5rem;\n}\n\n.wp_blog_explanation p,\n.wp_blog_explanation li {\n  font-size: 1.05rem;\n  line-height: 1.7;\n  margin: 0.5rem 0;\n}\n\n.wp_blog_explanation code {\n  background: #fef9f4;   \/* light bg instead of dark blue *\/\n  color: #E58C32;        \/* brand orange *\/\n  padding: 3px 6px;\n  border-radius: 4px;\n  font-family: 'Courier New', monospace;\n  font-weight: 600;      \/* optional, makes it pop *\/\n}\n\n.wp_blog_explanation img {\n  max-width: 100%;\n  border-radius: var(--tab-radius);\n  margin-top: 1rem;\n  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);\n}\n\n\/* Tab Buttons *\/\n.wp_blog_code-tabs-header {\n  display: flex;\n  flex-wrap: wrap;\n  gap: 0.5rem;\n  margin-bottom: 1rem;\n}\n\n.wp_blog_code-tab-button {\n  padding: 0.6rem 1.2rem;\n  border: 1px solid var(--primary);\n  background: white;\n  color: var(--primary);\n  border-radius: 50px;\n  font-weight: 600;\n  cursor: pointer;\n  transition: all 0.3s ease;\n}\n\n.wp_blog_code-tab-button:hover {\n  background: var(--secondary);\n}\n\n.wp_blog_code-tab-button.active {\n  background: var(--primary);\n  color: white;\n}\n\n\/* Code Content *\/\n.wp_blog_code-tab-content {\n  display: none;\n  background: var(--code-bg);\n  border-radius: var(--tab-radius);\n}\n\n.wp_blog_code-tab-content.active {\n  display: block;\n}\n\n.wp_blog_code-tab-content pre {\n  margin: 0;\n  padding: 1.5rem;\n  font-size: 1rem;\n  overflow-x: auto;\n  background: var(--code-bg);\n  border-radius: var(--tab-radius);\n  color: var(--code-text);\n}\n\n\/* Dark mode variables *\/\n.wp_blog_theme.dark-mode {\n  --light-bg: #121212;\n  --text-dark: #f5f5f5;\n  --shadow: 0 4px 12px rgba(255, 255, 255, 0.08);\n  --code-bg: #1e1e1e;\n  --code-text: #c5f0ff;\n}\n\n.wp_blog_theme.dark-mode .wp_blog_explanation {\n  background: #1e1e1e;\n}\n\n\/* Dark mode code highlight *\/\n.wp_blog_theme.dark-mode .wp_blog_explanation code {\n  background: #333;\n  color: #ffd27f;\n}\n\n.wp_blog_theme {\n  position: relative; \/* makes it the reference for absolute children *\/\n}\n\n.wp_blog_toggle-btn {\n  position: absolute;\n  top: 1rem;\n  right: 1rem;\n  z-index: 9999;\n  padding: 0.5rem 0.8rem;\n  border-radius: 10%;\n  background: var(--primary);\n  color: white;\n  font-weight: bold;\n  cursor: pointer;\n  border: none;\n  box-shadow: var(--shadow);\n  transition: background 0.3s, transform 0.2s;\n}\n\n.wp_blog_toggle-btn:hover {\n  background: #cc772e;\n}\n\n.wp_blog_theme.dark-mode .wp_blog_code-tabs-container {\n  background: #1e1e1e;\n}\n<\/style>\n\n<div class=\"wp_blog_container wp_blog_theme\">\n            <button id=\"blogNotesThemeToggle\" class=\"wp_blog_toggle-btn\">\ud83c\udf19<\/button>\n\n<h1 class=\"wp_blog_main-heading\"><\/h1>\n\n<div class=\"wp_blog_explanation\">\n    <h2>Radix Sort<\/h2>\n    <p>\n        <strong>\n            Radix sort is a <code>non-comparison sorting algorithm<\/code>. Instead of comparing elements directly, it sorts numbers digit by digit (or character by character), starting from either the least significant digit (LSD).\n        <\/strong>\n    <\/p>\n\n    <h2>Example<\/h2>\n    <p><strong>[170, 45, 75, 90, 802, 24, 2, 66]<\/strong><\/p>\n    <p><code>Sort First LSD: <\/code><strong>[170, 90, 802, 2, 24, 45, 75, 66]<\/strong><\/p>\n    <p><code>Sort Second LSD: <\/code><strong>[802, 2, 24, 45, 66, 170, 75, 90]<\/strong><\/p>\n    <p><code>Sort Third LSD: <\/code><strong>[2, 24, 45, 66, 75, 90, 170, 802]<\/strong><\/p>\n\n    <h2>Time Complexity<\/h2>\n    <p><strong>O(d * (n + k))<\/strong><\/p>\n    <p>where k: range, d = digits<\/p>\n    <p>Effectively o(n) for integers.<\/p>\n\n    <h2>Space Complexity<\/h2>\n    <p><strong>O(n + k)<\/strong><\/p>\n\n    <h2>When to use Radix Sort<\/h2>\n    <ul>\n        <li>Large number of integers.<\/li>\n        <li>Fix digit length.<\/li>\n        <li>Non-negative numbers.<\/li>\n    <\/ul>\n\n    <h2>When Not to use Radix Sort<\/h2>\n    <ul>\n        <li>Floating point values.<\/li>\n        <li>Very large digits.<\/li>\n        <li>When memory is extremely tight.<\/li>\n    <\/ul>\n<\/div>\n\n<div class=\"wp_blog_code-tabs-container\">\n    <div class=\"wp_blog_code-tabs-header\">\n        <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">\n            JavaScript\n        <\/button>\n        <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n        <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n        <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n        <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n        <button class=\"wp_blog_code-tab-button\" data-lang=\"cs\">C#<\/button>\n    <\/div>\n\n    <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n       \n<pre><code class=\"language-javascript\">\nfunction countingSortStable(arr, e) {\n    let count = new Array(10).fill(0);\n    \n    for(let x of arr){\n        let digit = Math.floor(x \/ e) % 10;\n        count[digit]++;\n    }\n    \n    for(let i = 1; i < count.length; i++) {\n        count[i] = count[i] + count[i - 1];\n    }\n    \n    let sortedArr = new Array(arr.length);\n    for(let i = arr.length - 1; i >= 0; i--) {\n        let curr = Math.floor(arr[i] \/ e) % 10;\n        let x = count[curr];\n        sortedArr[x-1] = arr[i];\n        count[curr]--\n    }\n    \/\/ modify the original array\n    for(let i = 0; i < arr.length; i++) {\n        arr[i] = sortedArr[i]; \n    }\n}\n\nfunction radixSort(arr){\n    let max = Math.max(...arr);\n    \n    for(let e = 1; Math.floor(max \/ e) > 0; e = e * 10){\n        countingSortStable(arr, e);\n    }\n    return arr;\n}\nlet nums = [170, 45, 75, 90, 802, 24, 2, 66];\nconsole.log(radixSort(nums));\n<\/code><\/pre>\n\n<\/div>\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n        <pre><code class=\"language-python\">\ndef countingSortStable(arr, e):\n    count = [0] * 10\n\n    for x in arr:\n        digit = (x \/\/ e) % 10\n        count[digit] += 1\n\n    for i in range(1, len(count)):\n        count[i] = count[i] + count[i - 1]\n\n    sortedArr = [0] * len(arr)\n    for i in range(len(arr) - 1, -1, -1):\n        curr = (arr[i] \/\/ e) % 10\n        x = count[curr]\n        sortedArr[x - 1] = arr[i]\n        count[curr] -= 1\n\n    for i in range(len(arr)):\n        arr[i] = sortedArr[i]\n\n\ndef radixSort(arr):\n    max_val = max(arr)\n\n    e = 1\n    while max_val \/\/ e > 0:\n        countingSortStable(arr, e)\n        e *= 10\n\n    return arr\n\n\nnums = [170, 45, 75, 90, 802, 24, 2, 66]\nprint(radixSort(nums))\n    <\/code><\/pre>\n    <\/div>\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n        <pre><code class=\"language-java\">\nimport java.util.*;\n\npublic class RadixSort {\n\n    static void countingSortStable(int[] arr, int e) {\n        int[] count = new int[10];\n\n        for (int x : arr) {\n            int digit = (x \/ e) % 10;\n            count[digit]++;\n        }\n\n        for (int i = 1; i < count.length; i++) {\n            count[i] = count[i] + count[i - 1];\n        }\n\n        int[] sortedArr = new int[arr.length];\n        for (int i = arr.length - 1; i >= 0; i--) {\n            int curr = (arr[i] \/ e) % 10;\n            int x = count[curr];\n            sortedArr[x - 1] = arr[i];\n            count[curr]--;\n        }\n\n        for (int i = 0; i < arr.length; i++) {\n            arr[i] = sortedArr[i];\n        }\n    }\n\n    static int[] radixSort(int[] arr) {\n        int max = Arrays.stream(arr).max().getAsInt();\n\n        for (int e = 1; max \/ e > 0; e = e * 10) {\n            countingSortStable(arr, e);\n        }\n        return arr;\n    }\n\n    public static void main(String[] args) {\n        int[] nums = {170, 45, 75, 90, 802, 24, 2, 66};\n        System.out.println(Arrays.toString(radixSort(nums)));\n    }\n}\n<\/code><\/pre>\n    <\/div>\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n        <pre><code class=\"language-cpp\">\n#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\n\nvoid countingSortStable(vector<int>& arr, int e) {\n    vector<int> count(10, 0);\n\n    for (int x : arr) {\n        int digit = (x \/ e) % 10;\n        count[digit]++;\n    }\n\n    for (int i = 1; i < count.size(); i++) {\n        count[i] = count[i] + count[i - 1];\n    }\n\n    vector<int> sortedArr(arr.size());\n    for (int i = arr.size() - 1; i >= 0; i--) {\n        int curr = (arr[i] \/ e) % 10;\n        int x = count[curr];\n        sortedArr[x - 1] = arr[i];\n        count[curr]--;\n    }\n\n    for (int i = 0; i < arr.size(); i++) {\n        arr[i] = sortedArr[i];\n    }\n}\n\nvector<int> radixSort(vector<int>& arr) {\n    int maxVal = *max_element(arr.begin(), arr.end());\n\n    for (int e = 1; maxVal \/ e > 0; e = e * 10) {\n        countingSortStable(arr, e);\n    }\n    return arr;\n}\n\nint main() {\n    vector<int> nums = {170, 45, 75, 90, 802, 24, 2, 66};\n    radixSort(nums);\n\n    for (int x : nums) cout << x << \" \";\n    return 0;\n}\n    <\/code><\/pre>\n    <\/div>\n\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n        <pre><code class=\"language-c\">\n#include &lt;stdio.h&gt;\n\nvoid countingSortStable(int arr[], int n, int e) {\n    int count[10] = {0};\n    int sortedArr[n];\n\n    for (int i = 0; i < n; i++) {\n        int digit = (arr[i] \/ e) % 10;\n        count[digit]++;\n    }\n\n    for (int i = 1; i < 10; i++) {\n        count[i] = count[i] + count[i - 1];\n    }\n\n    for (int i = n - 1; i >= 0; i--) {\n        int curr = (arr[i] \/ e) % 10;\n        int x = count[curr];\n        sortedArr[x - 1] = arr[i];\n        count[curr]--;\n    }\n\n    for (int i = 0; i < n; i++) {\n        arr[i] = sortedArr[i];\n    }\n}\n\nvoid radixSort(int arr[], int n) {\n    int max = arr[0];\n    for (int i = 1; i < n; i++)\n        if (arr[i] > max)\n            max = arr[i];\n\n    for (int e = 1; max \/ e > 0; e = e * 10) {\n        countingSortStable(arr, n, e);\n    }\n}\n\nint main() {\n    int nums[] = {170, 45, 75, 90, 802, 24, 2, 66};\n    int n = sizeof(nums) \/ sizeof(nums[0]);\n\n    radixSort(nums, n);\n\n    for (int i = 0; i < n; i++)\n        printf(\"%d \", nums[i]);\n\n    return 0;\n}\n    <\/code><\/pre>\n    <\/div>\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"cs\">\n        <pre><code class=\"language-csharp\">\nusing System;\nusing System.Linq;\n\nclass Program {\n\n    static void countingSortStable(int[] arr, int e) {\n        int[] count = new int[10];\n\n        foreach (int x in arr) {\n            int digit = (x \/ e) % 10;\n            count[digit]++;\n        }\n\n        for (int i = 1; i < count.Length; i++) {\n            count[i] = count[i] + count[i - 1];\n        }\n\n        int[] sortedArr = new int[arr.Length];\n        for (int i = arr.Length - 1; i >= 0; i--) {\n            int curr = (arr[i] \/ e) % 10;\n            int x = count[curr];\n            sortedArr[x - 1] = arr[i];\n            count[curr]--;\n        }\n\n        for (int i = 0; i < arr.Length; i++) {\n            arr[i] = sortedArr[i];\n        }\n    }\n\n    static int[] radixSort(int[] arr) {\n        int max = arr.Max();\n\n        for (int e = 1; max \/ e > 0; e = e * 10) {\n            countingSortStable(arr, e);\n        }\n        return arr;\n    }\n\n    static void Main() {\n        int[] nums = {170, 45, 75, 90, 802, 24, 2, 66};\n        Console.WriteLine(string.Join(\" \", radixSort(nums)));\n    }\n}\n  <\/code><\/pre>\n    <\/div>\n<\/div>\n<\/div>\n<script>\ndocument.addEventListener(\"DOMContentLoaded\", () => {\n  const buttons = document.querySelectorAll(\".wp_blog_code-tab-button\");\n  const contents = document.querySelectorAll(\".wp_blog_code-tab-content\");\n\n  buttons.forEach((button) => {\n    button.addEventListener(\"click\", () => {\n      const lang = button.getAttribute(\"data-lang\");\n\n      buttons.forEach((btn) => btn.classList.remove(\"active\"));\n      contents.forEach((content) => content.classList.remove(\"active\"));\n\n      button.classList.add(\"active\");\n      document\n        .querySelector(`.wp_blog_code-tab-content[data-lang=\"${lang}\"]`)\n        .classList.add(\"active\");\n    });\n  });\n\n  const themeToggle = document.getElementById(\"blogNotesThemeToggle\");\n  const themeContainer = document.querySelector(\".wp_blog_theme\");\n\n  themeToggle.addEventListener(\"click\", () => {\n    themeContainer.classList.toggle(\"dark-mode\");\n    themeToggle.textContent =\n      themeContainer.classList.contains(\"dark-mode\") ? \"\u2600\ufe0f\" : \"\ud83c\udf19\";\n  });\n});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udf19 Radix Sort Radix sort is a non-comparison sorting algorithm. Instead of comparing elements directly, it sorts numbers digit by digit (or character by character), starting from either the least significant digit (LSD). Example [170, 45, 75, 90, 802, 24, 2, 66] Sort First LSD: [170, 90, 802, 2, 24, 45, 75, 66] Sort Second<\/p>\n","protected":false},"author":108,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[210,260,176,175,211,174,172,173],"tags":[],"class_list":["post-11478","post","type-post","status-publish","format-standard","category-algorithms","category-c-c-plus-plus","category-csharp","category-cplusplus","category-data-structures","category-java","category-javascript","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/11478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/108"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=11478"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/11478\/revisions"}],"predecessor-version":[{"id":11479,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/11478\/revisions\/11479"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=11478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=11478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=11478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}