×

Radix Sort

Radix Sort: The radix sort is a non-comparative integer sorting algorithm that sorts the elements by grouping the individual digits of the same location. It shares the same significant position and value. Positional notation is required in this sorting. An excellent example of the radix sort is a dictionary because, in the dictionary, all the alphabets are arranged in alphabetical order. Radix sort was developed by the Harold H. Seward in 1954.

Complexity table of radix sort

ComplexityBest caseAverage caseWorst case
Time?(n + k)?(nk)O(nk)
Space  O(n + k)

Algorithm of radix sort

Step 1: Find the largest number in ARR as LARGE 
Step 2: [INITIALIZE] SET NOP = Number of digits in LARGE 
Step 3: SET PASS = 0 
Step 4: Repeat Step 5 while PASS <= NOP-1 
Step 5: SET I = 0 and INITIALIZE buckets 
Step 6: Repeat Steps 7 to 9 while I<n-1< li=""></n-1<> 
Step 7: SET DIGIT = digit at (PASS)th place in A[I] 
Step 8: Add A[I] to the bucket numbered DIGIT 
Step 9: INCREMENT bucket count for bucket numbered DIGIT
             // end of loop 
Step 10: Collect the numbers in the bucket
             // end of loop 
Step 11: end  

Example 1: Suppose we have the following array, which we have to sort.

542254864650875781453211152968
Step 1: Sort the array according to the last digit of the value in the array element.   
650 781 211 542 152 453 254 864 875 968 
Step 2: Sort the array according to the middle digit of the value in the array element.   
211 542 650 152 453 254 864 968 875 781   
Step 3: Sort the array according to the first digit of the value in the array element. 
152 211 254 453 542 650 781 864 875 968

Example 2: Suppose we have the following array, which we have to sort.

65374321010632576972119918
Step 1: 0065 0374 3210 0010 0006 3257 0697 0211 0099 0018 
Step 2: Sort the array according to the last digit of the value in the array element.  
3210 0010 0211 0374 0065 0006 3257 0697 0018 0099 
Step 3: Sort the array according to the second last digit of the value in the array element.   
0006 3210 0010 0211 0018 3257 0065 0374 0697 0099 
Step 4: Sort the array according to the second digit of the value in the array element.   
0006 0010 0018 0065 0099 3210 0211 3257 0374 0697 
Step 5: Sort the array according to the first digit of the value in the array element.   
0006 0010 0018 0065 0099 0211 0374 0697 3210 3257 
6 10 18 65 99 211 374 697 3210 3257

Radix sort program in C language:

#include <stdio.h> 
 int largest(int a[]);  
void radix_sort(int a[]); 
 void main()  
{    
  int i;     
 int a[10]={21, 14, 65, 785, 365, 652, 75, 35, 3214, 52};   
        radix_sort(a);   
     printf("\n Radix sorted is: \n");   
   for(i=0;i<10;i++)    
      printf(" %d\t", a[i]);
  }   
 int largest(int a[]) 
 {        
 int larger=a[0], i;     
  for(i=1;i<10;i++)   
   {       
   if(a[i]>larger)  
        larger = a[i]; 
     }     
 return larger; 
}  
void radix_sort(int a[])  
{  
    int bucket[10][10], bucket_count[10];  
    int i, j, k, remainder, NOP=0, divisor=1, larger, pass;  
    larger = largest(a);  
    while(larger>0)    
  {        
  NOP++;          larger/=10;     
 }      
for(pass=0;pass<NOP;pass++) // Initialize the buckets 
     {     
     for(i=0;i<10;i++)  
        bucket_count[i]=0; 
         for(i=0;i<10;i++) 
         {    // sort the numbers according to the digit at passth place                        remainder = (a[i]/divisor)%10;             
 bucket[remainder][bucket_count[remainder]] = a[i];   
           bucket_count[remainder] += 1;         
 }        
  // collect the numbers after PASS pass          i=0;          for(k=0;k<10;k++)  
        {    
          for(j=0;j<bucket_count[k];j++)    
          {           
       a[i] = bucket[k][j];                 i++;           
   }      
    }       
   divisor *= 10;    
    } 
 } 

Output:

Radix sort is: 
14 
21 
35 
52 
65 
75 
365 
652 
785 
3214

Related Topics

Extended Binary Tree

A form of binary tree known as an extended binary tree replaces all of the original tree's null subtrees with special nodes known as external nodes, while the remaining nodes...

4 minutes read.

Threaded Binary Tree

The linked form of binary trees wastes storage capacity because more than half of the connection variables have a Missing value. A binary tree has several nodes. Hence n+1 link fields...

8 minutes read.

Function to Delete a Leaf Node from a Binary Tree

Implementation // We are writing a C++ code to eliminate all the leaves from the given value.  #include <bits/stdc++.h> using namespace std; // creating a new binary tree node struct __nod { int record; struct __nod *Lft,...

4 minutes read.

Hashing and its Applications

Hashing Hashing refers to transforming plain text data in such a way that even if it is leaked for some reason, no one would be able to make sense of it....

6 minutes read.

Given a Binary Tree, find its Minimum Depth

Implementation // Creating a C++ program or implementation to search and explore the minimum depth of a given binary tree.  #include<bits/stdc++.h> using namespace std; // Creating a new binary tree node struct __nod { int record; struct __nod*...

5 minutes read.

Blowfish algorithm

The Blowfish algorithm is the very first encryption algorithm which is symmetric. It was firstly used as an alternate algorithm for the DES algorithm. It was designed by Bruce Steiner...

3 minutes read.

Understanding Data Processing

Introduction Data In our everyday lives, any task that we perform online is related to data. Millions of pieces of data are produced every second across the globe. Data production is largely...

4 minutes read.

Given a Binary Tree, Check if it's balanced

Implementation /*Creating a C++ program that will help us identify whether the given tree is height-balanced or not.  */ #include <bits/stdc++.h> using namespace std; /* A particular binary tree node consists of data with some...

4 minutes read.

Graph Data Structure

A graph is a non-primitive and non-linear data structure. It is a group of (V, E) where V is a set of vertexes, and E is a set of edge....

3 minutes read.

Object-Oriented Analysis and Design

While designing a system, one should know all the requirements or needs of the plan beforehand, and to do so, we should use a systematic approach to analyze the goal...

3 minutes read.

What is the difference between DFS and BFS?

What is BFS? BFS is generally known as the low level traversal. As we already know that it stands for breadth first search and is mainly used in the queue data...

4 minutes read.

B Tree in Data Structure

Data management is called database management. A data model is a system that stores, manages, and optimizes computer resources. Data processing is not just about data storage. Almost every app...

9 minutes read.

Data Structures Tutorial

The data structure is a way of storing and organizing data in a computer system. So that we can use the data quickly, which means the information is stored and...

7 minutes read.

Polish Notation in Data Structures

Arithmetic Expression: An arithmetic expression is defined as several operands or data items combined using several operators. For example; a+b*(c-d) is an expression. Operands: Operands represent the data in an expression...

2 minutes read.

Insertion Sort vs Selection Sort

In this article, we will discuss insertion sort, Selection sort and the basic differences between these two sorting techniques in detail: What is Insertion Sort? Insertion Sort – The insertion sort is...

5 minutes read.

Buffer overflow attack with examples

You have undoubtedly faced the term buffer overflow in your programming journey. Many times it occurs when we try to run a piece of code with user input, but it...

4 minutes read.

Delete the Middle element of the Linked List in C

Delete the Middle element of the Linked List in C This article has given a singly linked list and will delete the middle element of the given linked list. Example:  The given...

3 minutes read.

Linear Queue VS Circular Queue

What is Queue? A queue is one of the important linear data structures extensively used in various computer applications. It is based on the FIFO (First In First Out) principle. It...

9 minutes read.

Sorting Algorithms

Sorting: In the data structure, sorting is the process by which you arrange the data in a logical order. This logical order can also be an ascending order or a...

7 minutes read.

Left View of Binary Tree

Implementation // creating a C++ program to print the Left view of the binary tree. #include <bits/stdc++.h> using namespace std; struct Nod { int record; struct Nod *Lft, *Rt; }; // creating a utility function that will eventually help...

4 minutes read.