Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Java Program for Radix Sort
Next article icon

Radix Sort – Python

Last Updated : 03 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Radix Sort is a linear sorting algorithm that sorts elements by processing them digit by digit. It is an efficient sorting algorithm for integers or strings with fixed-size keys. 

Rather than comparing elements directly, Radix Sort distributes the elements into buckets based on each digit’s value. By repeatedly sorting the elements by their significant digits, from the least significant to the most significant, Radix Sort achieves the final sorted order.

Working of Radix Sort Algorithm

To perform radix sort on the array [170, 45, 75, 90, 802, 24, 2, 66], we follow these steps:

Step 1: Find the largest element in the array, which is 802. It has three digits, so we will iterate three times, once for each significant place.

Step 2: Sort the elements based on the unit place digits (X=0). We use a stable sorting technique, such as counting sort, to sort the digits at each significant place. It’s important to understand that the default implementation of counting sort is unstable i.e. same keys can be in a different order than the input array. To solve this problem, We can iterate the input array in reverse order to build the output array. This strategy helps us to keep the same keys in the same order as they appear in the input array.

Sorting based on the unit place:

  • Perform counting sort on the array based on the unit place digits.
  • The sorted array based on the unit place is [170, 90, 802, 2, 24, 45, 75, 66].

Step 3: Sort the elements based on the tens place digits.
Sorting based on the tens place:

  • Perform counting sort on the array based on the tens place digits.
  • The sorted array based on the tens place is [802, 2, 24, 45, 66, 170, 75, 90].

Step 4: Sort the elements based on the hundreds place digits.
Sorting based on the hundreds place:

  • Perform counting sort on the array based on the hundreds place digits.
  • The sorted array based on the hundreds place is [2, 24, 45, 66, 75, 90, 170, 802].

Step 5: The array is now sorted in ascending order.
The final sorted array using radix sort is [2, 24, 45, 66, 75, 90, 170, 802].

Below is the implementation for the above illustrations:

Python
def countingSort(arr, exp1):         n = len(arr)         # The output array elements that will have sorted arr      output = [0] * (n)         # initialize count array as 0      count = [0] * (10)         # Store count of occurrences in count[]      for i in range(0, n):          index = (arr[i]/exp1)          count[int((index)%10)] += 1        # Change count[i] so that count[i] now contains actual      #  position of this digit in output array      for i in range(1,10):          count[i] += count[i-1]         # Build the output array      i = n-1     while i>=0:          index = (arr[i]/exp1)          output[ count[ int((index)%10) ] - 1] = arr[i]          count[int((index)%10)] -= 1         i -= 1        # Copying the output array to arr[],      # so that arr now contains sorted numbers      i = 0     for i in range(0,len(arr)):          arr[i] = output[i]   # Method to do Radix Sort def radixSort(arr):      # Find the maximum number to know number of digits     max1 = max(arr)      # Do counting sort for every digit. Note that instead     # of passing digit number, exp is passed. exp is 10^i     # where i is current digit number     exp = 1     while max1 // exp > 0:         countingSort(arr,exp)         exp *= 10  # Driver code  arr = [ 170, 45, 75, 90, 802, 24, 2, 66] radixSort(arr)  for i in range(len(arr)):     print(arr[i],end=" ") 

Output
2 24 45 66 75 90 170 802 

Time Complexity: O(n*d). Here d=10
Auxiliary Space: O(n)

Please refer complete article on Radix Sort for more details!



Next Article
Java Program for Radix Sort
author
kartik
Improve
Article Tags :
  • Python Programs
  • python sorting-exercises

Similar Reads

  • Radix Sort - Data Structures and Algorithms Tutorials
    Radix Sort is a linear sorting algorithm that sorts elements by processing them digit by digit. It is an efficient sorting algorithm for integers or strings with fixed-size keys. Rather than comparing elements directly, Radix Sort distributes the elements into buckets based on each digit's value. By
    15+ min read
  • C Program For Radix Sort
    Radix Sort is a linear sorting algorithm that sorts elements by processing them digit by digit. It is an efficient sorting algorithm for integers or strings with fixed-size keys. Rather than comparing elements directly, Radix Sort distributes the elements into buckets based on each digit’s value. By
    3 min read
  • Radix Sort - Python
    Radix Sort is a linear sorting algorithm that sorts elements by processing them digit by digit. It is an efficient sorting algorithm for integers or strings with fixed-size keys. Rather than comparing elements directly, Radix Sort distributes the elements into buckets based on each digit’s value. By
    4 min read
  • Java Program for Radix Sort
    The Radix Sort Algorithm Do the following for each digit i where i varies from the least significant digit to the most significant digit.Sort input array using counting sort (or any stable sort) according to the ith digit. C/C++ Code // Radix sort Java implementation import java.io.*; import java.ut
    3 min read
  • 3-Way Radix Quicksort in Java
    Basically, as the name suggests that 3-Way Radix Quicksort is a combination of both radix and 3-way quicksort. It is a hybrid sort which is in between of both radix and 3-way quicksort. This algorithm is mainly used to sort strings. The main idea behind the radix sort is to use the digits (beginning
    9 min read
  • MSD( Most Significant Digit ) Radix Sort
    In this article, two types of Radix Sort are discussed: LSD Radix Sort: It starts sorting from the end of strings (the Least significant digit).MSD Radix Sort: It starts sorting from the beginning of strings (the Most significant digit).In this article, the task is to discuss the MSD Radix Sort and
    15+ min read
  • Sort n numbers in range from 0 to n^2 - 1 in linear time
    Given an array of numbers of size n. It is also given that the array elements are in the range from 0 to n2 - 1. Sort the given array in linear time. Examples: Since there are 5 elements, the elements can be from 0 to 24.Input: arr[] = {0, 23, 14, 12, 9}Output: arr[] = {0, 9, 12, 14, 23}Since there
    12 min read
  • How to efficiently sort a big list dates in 20's
    Given a big list of dates in '20s, how to efficiently sort the list. Example: Input: Date arr[] = {{20, 1, 2014}, {25, 3, 2010}, { 3, 12, 2000}, {18, 11, 2001}, {19, 4, 2015}, { 9, 7, 2005}} Output: Date arr[] = {{ 3, 12, 2000}, {18, 11, 2001}, { 9, 7, 2005}, {25, 3, 2010}, {20, 1, 2014}, {19, 4, 20
    11 min read
  • Radix Sort vs Bucket Sort
    We have two standard sorting algorithms, named bucket sort and radix sort. They both share differences and similarities. Let’s explore some similarities, differences, advantages, and disadvantages here in more detail. Bucket Sort: Bucket sort is a sorting algorithm in which the elements are separate
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences