×

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

Quick Sort vs Merge Sort

In this article, we will take an overview of Quick Sort and Merge Sort and then discuss the differences between them. What is Quick Sort? Quick Sort – The idea behind the...

7 minutes read.

Binary Search Tree vs AVL Tree: Data Structure

Difference Between Binary Search Tree and AVL Tree Binary Search Tree: The binary search tree is a kind of binary tree data structure and it follows the conditions of binary...

3 minutes read.

Union and Intersection of two Linked Lists

Union and Intersection of two Linked Lists This article explains how we can do the union and intersection of two linked lists. In this problem, we have given two linked lists...

3 minutes read.

B+ Tree Program in Q language

A B+ tree is just an improvised version of a self-balancing and well-maintained tree in which all the key values that hold valuable information is present at the bottom, which...

9 minutes read.

What are Forest Trees in Data Structure

Data structure A data model manages and optimizes computer resources, and a database stores and manages data. It's one of many uses for data structures to hold data. Data structures come...

5 minutes read.

Serialize and Deserialize Binary Trees

In order to save a tree in a file that can later be restored, serialisation is used. The tree's structure must be preserved. Deserialization involves reading a tree from a...

4 minutes read.

Applications of Different Linked Lists in Data Structure

What is a Linked list? A linked list is a data structure that consists of a sequence of elements, where each containing a reference or ("link") to the next element in...

5 minutes read.

Horizontal and Vertical Scaling

Being a software engineer, you would have designed a website or application and deployed it on any server. Imagine that the developed application starts getting popular, and many users engage...

6 minutes read.

Asynchronous advantage actor-critic (A3C) Algorithm

The Asynchronous advantage actor-critic (A3C) Algorithm is one of the latest algorithms developed by the Artificial Intelligence division, Deep Mind at Google. It is used for the Deep Reinforcement Learning...

3 minutes read.

Optimal binary search tree in DSA

Implementation // A simple way of the recursive implementation of the optimal search that we will perform on the binary tree.   #include <bits/stdc++.h> using namespace std; // we have to create a basic utility...

8 minutes read.

Huffman tree in Data Structures

The Huffman trees in the field of data structures are pretty impressive in their work. They are generally treated as the binary tree, which is linked with the least external...

6 minutes read.

Recursion - Factorial and Fibonacci

In this article, we will learn how to find the factorial of a number and the Fibonacci series up to n using the recursion method. What is recursion? Defining anything in terms...

7 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.

Application of Stack in Data Structures

In this article, we will discuss all the different applications of stack. What is meant by stack? The stack is a non-primitive linear data structure in which the insertion of the new...

11 minutes read.

Implementation of stack

Implementation of stack: The stack can be implemented in two ways: using array and using a linked list. The pop and push operations in the array are simpler than the...

3 minutes read.

Find the nth node from the end of a Linked List

Find the nth node from the end of a Linked List In this problem, we have given a singly linked list and a number 'n,' and we need to find the...

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.

What is an AVL Tree in Data Structure?

AVL tree stands for (Adelson, Velskii, & Landis Tree) Data structure Data management is called database management. A data model is a system used to store, manage, and optimize computer resources. Data...

4 minutes read.

What are the types of Trees in Data Structure

Data structures Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system used to store, manage,...

6 minutes read.

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.