{"id":25185,"date":"2017-10-15T14:47:18","date_gmt":"2017-10-15T09:17:18","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25185"},"modified":"2017-10-15T14:47:18","modified_gmt":"2017-10-15T09:17:18","slug":"radix-sort","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/radix-sort\/","title":{"rendered":"Radix Sort"},"content":{"rendered":"<p>The <a href=\"http:\/\/www.geeksforgeeks.org\/lower-bound-on-comparison-based-sorting-algorithms\/\" target=\"_blank\" rel=\"noopener noreferrer\">lower bound for Comparison based sorting algorithm<\/a> (Merge Sort, Heap Sort, Quick-Sort .. etc) is \u03a9(nLogn), i.e., they cannot do better than nLogn.<span id=\"more-122061\"><\/span><\/p>\n<p><a href=\"http:\/\/www.geeksforgeeks.org\/counting-sort\/\" target=\"_blank\" rel=\"noopener noreferrer\">Counting sort<\/a> is a linear time sorting algorithm that sort in O(n+k) time when elements are in range from 1 to k.<\/p>\n<p><strong><em>What if the elements are in range from 1 to n<sup>2<\/sup>? <\/em><\/strong><br \/>\nWe can\u2019t use counting sort because counting sort will take O(n<sup>2<\/sup>) which is worse than comparison based sorting algorithms. Can we sort such an array in linear time?<br \/>\n<a href=\"http:\/\/en.wikipedia.org\/wiki\/Radix_sort\" target=\"_blank\" rel=\"noopener noreferrer\">Radix Sort<\/a> is the answer. The idea of Radix Sort is to do digit by digit sort starting from least significant digit to most significant digit. Radix sort uses counting sort as a subroutine to sort.<\/p>\n<p><em><strong>The Radix Sort Algorithm<\/strong><\/em><br \/>\n<strong>1)<\/strong> Do following for each digit i where i varies from least significant digit to the most significant<\/p>\n<p><b>2)<\/b> Sort input array using counting sort (or any stable sort) according to the i\u2019th digit.<\/p>\n<p><strong>Example:<\/strong><br \/>\nOriginal, unsorted list:<\/p>\n<dl>\n<dd>170, 45, 75, 90, 802, 24, 2, 66<\/dd>\n<\/dl>\n<p>Sorting by least significant digit (1s place) gives: [*Notice that we keep 802 before 2, because 802 occurred before 2 in the original list, and similarly for pairs 170 &amp; 90 and 45 &amp; 75.]\n<dl>\n<dd>170, 90, 802,\u00a02, 24, 45, 75, 66<\/dd>\n<\/dl>\n<p>Sorting by next digit (10s place) gives: [*Notice that 802 again comes before 2 as 802 comes before 2 in the previous list.]\n<dl>\n<dd>802, 2,\u00a024,\u00a045,\u00a066, 170,\u00a075,\u00a090<\/dd>\n<\/dl>\n<p>Sorting by most significant digit (100s place) gives:<\/p>\n<dl>\n<dd>2, 24, 45, 66, 75, 90,\u00a0170,\u00a0802<\/dd>\n<\/dl>\n[ad type=&#8221;banner&#8221;]\n<p><em><strong>What is the running time of Radix Sort?<\/strong><\/em><br \/>\nLet there be d digits in input integers. Radix Sort takes O(d*(n+b)) time where b is the base for representing numbers, for example, for decimal system, b is 10. What is the value of d? If k is the maximum possible value, then d would be O(log<sub>b<\/sub>(k)). So overall time complexity is O((n+b) * log<sub>b<\/sub>(k)). Which looks more than the time complexity of comparison based sorting algorithms for a large k. Let us first limit k. Let k &lt;= n<sup>c<\/sup> where c is a constant. In that case, the complexity becomes O(nLog<sub>b<\/sub>(n)). But it still doesn\u2019t beat comparison based sorting algorithms.<br \/>\nWhat if we make value of b larger?. What should be the value of b to make the time complexity linear? If we set b as n, we get the time complexity as O(n). In other words, we can sort an array of integers with range from 1 to n<sup>c<\/sup> if the numbers are represented in base n (or every digit takes log<sub>2<\/sub>(n) bits).<\/p>\n<p><em><strong>Is Radix Sort preferable to Comparison based sorting algorithms like Quick-Sort?<\/strong><\/em><br \/>\nIf we have log<sub>2<\/sub>n bits for every digit, the running time of Radix appears to be better than Quick Sort for a wide range of input numbers. The constant factors hidden in asymptotic notation are higher for Radix Sort and Quick-Sort uses hardware caches more effectively. Also, Radix sort uses counting sort as a subroutine and counting sort takes extra space to sort numbers.<\/p>\n<p><strong>Implementation of Radix Sort<\/strong><br \/>\nFollowing is a simple C++ implementation of Radix Sort. For simplicity, the value of d is assumed to be 10. We recommend you to see <a href=\"http:\/\/www.geeksforgeeks.org\/counting-sort\/\" target=\"_blank\" rel=\"noopener noreferrer\">Counting Sort<\/a> for details of countSort() function in below code.<\/p>\n<p><strong>C<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ C++ implementation of Radix Sort<br\/>#include&lt;iostream&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ A utility function to get maximum value in arr[]<br\/>int getMax(int arr[], int n)<br\/>{<br\/>    int mx = arr[0];<br\/>    for (int i = 1; i &lt; n; i++)<br\/>        if (arr[i] &gt; mx)<br\/>            mx = arr[i];<br\/>    return mx;<br\/>}<br\/> <br\/>\/\/ A function to do counting sort of arr[] according to<br\/>\/\/ the digit represented by exp.<br\/>void countSort(int arr[], int n, int exp)<br\/>{<br\/>    int output[n]; \/\/ output array<br\/>    int i, count[10] = {0};<br\/> <br\/>    \/\/ Store count of occurrences in count[]<br\/>    for (i = 0; i &lt; n; i++)<br\/>        count[ (arr[i]\/exp)%10 ]++;<br\/> <br\/>    \/\/ Change count[i] so that count[i] now contains actual<br\/>    \/\/  position of this digit in output[]<br\/>    for (i = 1; i &lt; 10; i++)<br\/>        count[i] += count[i - 1];<br\/> <br\/>    \/\/ Build the output array<br\/>    for (i = n - 1; i &gt;= 0; i--)<br\/>    {<br\/>        output[count[ (arr[i]\/exp)%10 ] - 1] = arr[i];<br\/>        count[ (arr[i]\/exp)%10 ]--;<br\/>    }<br\/> <br\/>    \/\/ Copy the output array to arr[], so that arr[] now<br\/>    \/\/ contains sorted numbers according to current digit<br\/>    for (i = 0; i &lt; n; i++)<br\/>        arr[i] = output[i];<br\/>}<br\/> <br\/>\/\/ The main function to that sorts arr[] of size n using <br\/>\/\/ Radix Sort<br\/>void radixsort(int arr[], int n)<br\/>{<br\/>    \/\/ Find the maximum number to know number of digits<br\/>    int m = getMax(arr, n);<br\/> <br\/>    \/\/ Do counting sort for every digit. Note that instead<br\/>    \/\/ of passing digit number, exp is passed. exp is 10^i<br\/>    \/\/ where i is current digit number<br\/>    for (int exp = 1; m\/exp &gt; 0; exp *= 10)<br\/>        countSort(arr, n, exp);<br\/>}<br\/> <br\/>\/\/ A utility function to print an array<br\/>void print(int arr[], int n)<br\/>{<br\/>    for (int i = 0; i &lt; n; i++)<br\/>        cout &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};<br\/>    int n = sizeof(arr)\/sizeof(arr[0]);<br\/>    radixsort(arr, n);<br\/>    print(arr, n);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>JAVA<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Radix sort Java implementation<br\/>import java.io.*;<br\/>import java.util.*;<br\/> <br\/>class Radix {<br\/> <br\/>    \/\/ A utility function to get maximum value in arr[]<br\/>    static int getMax(int arr[], int n)<br\/>    {<br\/>        int mx = arr[0];<br\/>        for (int i = 1; i &lt; n; i++)<br\/>            if (arr[i] &gt; mx)<br\/>                mx = arr[i];<br\/>        return mx;<br\/>    }<br\/> <br\/>    \/\/ A function to do counting sort of arr[] according to<br\/>    \/\/ the digit represented by exp.<br\/>    static void countSort(int arr[], int n, int exp)<br\/>    {<br\/>        int output[] = new int[n]; \/\/ output array<br\/>        int i;<br\/>        int count[] = new int[10];<br\/>        Arrays.fill(count,0);<br\/> <br\/>        \/\/ Store count of occurrences in count[]<br\/>        for (i = 0; i &lt; n; i++)<br\/>            count[ (arr[i]\/exp)%10 ]++;<br\/> <br\/>        \/\/ Change count[i] so that count[i] now contains<br\/>        \/\/ actual position of this digit in output[]<br\/>        for (i = 1; i &lt; 10; i++)<br\/>            count[i] += count[i - 1];<br\/> <br\/>        \/\/ Build the output array<br\/>        for (i = n - 1; i &gt;= 0; i--)<br\/>        {<br\/>            output[count[ (arr[i]\/exp)%10 ] - 1] = arr[i];<br\/>            count[ (arr[i]\/exp)%10 ]--;<br\/>        }<br\/> <br\/>        \/\/ Copy the output array to arr[], so that arr[] now<br\/>        \/\/ contains sorted numbers according to curent digit<br\/>        for (i = 0; i &lt; n; i++)<br\/>            arr[i] = output[i];<br\/>    }<br\/> <br\/>    \/\/ The main function to that sorts arr[] of size n using<br\/>    \/\/ Radix Sort<br\/>    static void radixsort(int arr[], int n)<br\/>    {<br\/>        \/\/ Find the maximum number to know number of digits<br\/>        int m = getMax(arr, n);<br\/> <br\/>        \/\/ Do counting sort for every digit. Note that instead<br\/>        \/\/ of passing digit number, exp is passed. exp is 10^i<br\/>        \/\/ where i is current digit number<br\/>        for (int exp = 1; m\/exp &gt; 0; exp *= 10)<br\/>            countSort(arr, n, exp);<br\/>    }<br\/> <br\/>    \/\/ A utility function to print an array<br\/>    static void print(int arr[], int n)<br\/>    {<br\/>        for (int i=0; i&lt;n; i++)<br\/>            System.out.print(arr[i]+&quot; &quot;);<br\/>    }<br\/> <br\/> <br\/>    \/*Driver function to check for above function*\/<br\/>    public static void main (String[] args)<br\/>    {<br\/>        int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};<br\/>        int n = arr.length;<br\/>        radixsort(arr, n);<br\/>        print(arr, n);<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n<p><strong>PYTHON<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">PYTHON<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program for implementation of Radix Sort<br\/> <br\/># A function to do counting sort of arr[] according to<br\/># the digit represented by exp.<br\/>def countingSort(arr, exp1):<br\/> <br\/>    n = len(arr)<br\/> <br\/>    # The output array elements that will have sorted arr<br\/>    output = [0] * (n)<br\/> <br\/>    # initialize count array as 0<br\/>    count = [0] * (10)<br\/> <br\/>    # Store count of occurrences in count[]<br\/>    for i in range(0, n):<br\/>        index = (arr[i]\/exp1)<br\/>        count[ (index)%10 ] += 1<br\/> <br\/>    # Change count[i] so that count[i] now contains actual<br\/>    #  position of this digit in output array<br\/>    for i in range(1,10):<br\/>        count[i] += count[i-1]<br\/> <br\/>    # Build the output array<br\/>    i = n-1<br\/>    while i&gt;=0:<br\/>        index = (arr[i]\/exp1)<br\/>        output[ count[ (index)%10 ] - 1] = arr[i]<br\/>        count[ (index)%10 ] -= 1<br\/>        i -= 1<br\/> <br\/>    # Copying the output array to arr[],<br\/>    # so that arr now contains sorted numbers<br\/>    i = 0<br\/>    for i in range(0,len(arr)):<br\/>        arr[i] = output[i]<br\/> <br\/># Method to do Radix Sort<br\/>def radixSort(arr):<br\/> <br\/>    # Find the maximum number to know number of digits<br\/>    max1 = max(arr)<br\/> <br\/>    # Do counting sort for every digit. Note that instead<br\/>    # of passing digit number, exp is passed. exp is 10^i<br\/>    # where i is current digit number<br\/>    exp = 1<br\/>    while max1\/exp &gt; 0:<br\/>        countingSort(arr,exp)<br\/>        exp *= 10<br\/> <br\/># Driver code to test above<br\/>arr = [ 170, 45, 75, 90, 802, 24, 2, 66]<br\/>radixSort(arr)<br\/> <br\/>for i in range(len(arr)):<br\/>    print(arr[i]),<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>2 24 45 66 75 90 170 802<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Radix Sort &#8211; Searching and Sorting -The lower bound for Comparison based sorting algorithm (Merge Sort, Heap Sort, Quick-Sort .. etc is \u03a9(nLogn). i.e., they cannot do better than nLogn.<\/p>\n","protected":false},"author":1,"featured_media":25187,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,83503,71670],"tags":[71852,71847,71832,71840,71853,70461,71860,71855,71865,71864,71834,71850,70047,71859,71833,71868,71849,70048,71836,71838,70492,70928,70017,71839,71866,70754,71841,71262,71070,71267,71837,71266,71862,71835,71844,71863,71857,71858,71846,71856,71861,71843,71854,71845,71867,71848,71842,70016,71851],"class_list":["post-25185","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-algorithm","category-radix-sort","category-searching-and-sorting","tag-algorithm-for-radix-sort","tag-algorithm-for-radix-sort-in-data-structure","tag-algorithm-of-bucket-sort","tag-algorithm-of-radix-sort","tag-algorithm-of-radix-sort-in-data-structure","tag-bubble-sort","tag-bucket-sort-algorithm-example","tag-bucket-sort-algorithm-with-example","tag-bucket-sort-pdf","tag-bucket-sort-ppt","tag-c-language","tag-c-program-for-radix-sort","tag-c-programming","tag-cocktail-sort","tag-comb-sort","tag-counting-sort-algorithm","tag-data-structure-tutorial","tag-data-structures","tag-dotfuscator","tag-ember-js","tag-hash-function","tag-heap-sort-algorithm","tag-insertion-sort-algorithm","tag-java-windows-7","tag-jquery-datatable","tag-kruskals-algorithm","tag-mcreator","tag-merge-sort-algorithm","tag-programming","tag-quick-sort-algorithm","tag-quicksort-","tag-radix-sort-algorithm","tag-radix-sort-algorithm-in-c","tag-radix-sort-algorithm-in-data-structure-ppt","tag-radix-sort-algorithm-in-java","tag-radix-sort-algorithm-ppt","tag-radix-sort-algorithm-pseudocode","tag-radix-sort-analysis","tag-radix-sort-applications","tag-radix-sort-c-code","tag-radix-sort-c-program","tag-radix-sort-code-java","tag-radix-sort-implementation","tag-radix-sort-in-c-program","tag-radix-sort-program-in-c","tag-radix-sort-program-in-c-with-output","tag-radix-sort-python","tag-selection-sort-algorithm","tag-thread-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25185"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25185\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/25187"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}