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
  • DSA
  • Practice Sorting
  • MCQs on Sorting
  • Tutorial on Sorting
  • Bubble Sort
  • Quick Sort
  • Merge Sort
  • Insertion Sort
  • Selection Sort
  • Heap Sort
  • Sorting Complexities
  • Radix Sort
  • ShellSort
  • Counting Sort
  • Bucket Sort
  • TimSort
  • Bitonic Sort
  • Uses of Sorting Algorithm
Open In App

Radix Sort vs Bucket Sort

Last Updated : 18 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 separated into several groups that are called buckets. Each bucket is then sorted individually using any other algorithm or recursively using bucket sort itself. Then the sorted buckets are gathered together.

Bucket sort is mainly useful when input is uniformly distributed over a range.

Bucket Sort Algorithm:

The algorithm can be expressed as following:

  1. Take the array then find the maximum and minimum elements of the array. Find the range of each bucket.
    Bucket range:((maximum element – minimum element)/number of elements)
  2. Now insert the element into the bucket based on Bucket Index. 
    Bucket Index: floor(a[i]-minimum element)/range
  3. Once the elements are inserted into each bucket, sort the elements within each bucket using the insertion sort.

Illustration:

Consider an array arr[] = {22, 72, 62, 32, 82, 142}

Range = (maximum-minimum) / number of elements
So, here the range will be given as: Range = (142 – 22)/6 = 20

Thus, the range of each bucket in bucket sort will be: 20 So, the buckets will be as:
20-40; 40-60; 60-80; 80-100; 100-120; 120-140; 140-160

Bucket index = floor((a[i]-min)/range)
For 22, bucketindex = (22-22)/20 = 0.
For 72, bucketindex = (72-22)/20 = 2.5. 
For 62, bucketindex = (62-22)/20 = 2.
For 32, bucketindex = (32-22)/20 = 0.5.
For 82, bucketindex = (82-22)/20 = 3.
For 142, bucketindex = (142-22)/20 = 6.

Elements can be inserted into the bucket as:
0 -> 22 -> 32
1
2 -> 72 -> 62 (72 will be inserted before 62 as it appears first in the list).
3 -> 82
4
5 
6 -> 142

Now sort the elements in each bucket using the insertion sort.
0 -> 22 -> 32
1
2 -> 62 -> 72
3 -> 82
4 
5
6 -> 142

Now gather them together.
arr[] = {22, 32, 62, 72, 82, 142}

Radix Sort:

The idea of Radix Sort is to do digit-by-digit sorting starting from the least significant digit to the most significant digit. Radix sort uses counting sort as a subroutine to sort.

Radix Sort Algorithm:

The algorithm can be described as follows:

  1. Take the array. Check whether the number of digits in every array element is the same. If it is not the same, make it the same by using 0 before MSB.
  2. Find how many buckets are needed. Now, if you are given a decimal number, the digit will fall in the range of 0 to 9, so take 10 buckets. If you are given a string, then characters will fall in the range a-z (26 alphabets), so consider 0 – 25 buckets.
  3. Begin with the LSB (leftmost bit/character) and place the number based on the LSB in the appropriate bucket number. (Do not sort within the buckets). Just concatenate from the buckets and append the numbers in an empty array.
  4. Once it is done with one’s place (LSB), follow step 3 again for ten’s place, the hundred’s place, and so on until the MSB is reached.
  5. The last output will be the resultant sorted array.

Illustration:

Consider array arr[] = {22, 72, 62, 32, 82, 142}

We will sort based on LSB to MSB (keeping the number of digits the same in every number). The numbers will be: 
022, 072, 062, 032, 082, 142

We will have 10 buckets ranging from 0 to 9. Start from one place.

PASS 1
0
1
2 -> 022 -> 072 -> 062 -> 042 -> 032 -> 082 -> 142
3
4
5
6
7
8
9
Resulting list: {022, 072, 062, 042, 032, 082, 142}

PASS 2
0
1
2 -> 022
3 -> 032
4 -> 042 -> 142
5
6 -> 062
7 -> 072
8 -> 082
9
Resulting list: {022, 032, 042, 142, 062, 072, 082}

PASS 3
0 -> 022 -> 032 -> 042 -> 062 -> 072 -> 082
1 -> 142
2
3
4
5
6
7
8
9
Resulting list: {022, 032, 042, 062, 072, 082, 142}

Below are some major differences between Radix Sort and Bucket Sort: 

Feature

RADIX SORT

BUCKET SORT

Base of approach Buckets are based on the base of the number. If we are using decimal numbers, we will have 10 buckets. If we are using the same for alphabets, we will have 26 buckets. The buckets are based on range, and the range is dependent on maximum and minimum numbers within the array.
sorting  In radix sort, we don’t sort the elements after inserting them into the respective buckets. In bucket sort, we sort the elements after inserting them into the bucket.
Number of Passes  The number of passes one needs to perform for radix sort is the number of digits. If the 3-digit number, we need to perform 3 passes; if the 6-digit number, we need to perform 6 passes. The number of passes one needs to perform for bucket sorting is 2. (One for inserting each element into a bucket and the other for sorting the elements within the bucket).
Time Complexity O (d*(n+b)) d = number of digits, n = number of array elements, b = base In the worst-case scenario, O(n+k) [If we use a linked list to represent every element inside the bucket, then it will be O (n*n)].
uses When the array elements are sparse (scattered), radix sort is used. Bucket sort is used when the array elements are dense (nearby).

It was found that bucket sort is faster as compared to radix sort, but it uses more memory when compared to radix sort.

Advantages And Disadvantages of Radix Sort:

Advantages:

  • It is fast when the numbers are small If the numbers are small, the number of passes will also be small. So, it becomes more efficient.
  • It is a stable sorting algorithm, i.e., it maintains the relative order of elements with equal values.
  • It is used in many suffix array construction algorithms.

Disadvantages:

  • It will sometimes consume more memory than is required.
  • It is based on digits or letters, so it is less flexible as compared to other sorting algorithms as one needs to know the entire data priorly only.

Advantages And Disadvantages of Bucket Sort:

Advantages:

It allows the buckets to be processed independently (you need to sort them independently within the buckets). This plays a great role in the processing of files.

Disadvantages:

  • It works more efficiently when the data is either less or more evenly distributed.
  • This technique is not valid for all data types due to its bucketing technique.

 

RADIX SORT

BUCKET SORT

STABLE ALGORITHM

  Yes

Yes

IN PLACE ALGORITHM

No

No

When to use Radix Sort vs. Bucket Sort

  • Radix Sort is suitable for sorting elements with varying key sizes and for parallelization
  • Bucket Sort is suitable for sorting uniformly distributed numbers within a specific range and works well with distributed computing
  • Choosing the right algorithm depends on the size of the dataset, the distribution of the data, and the computing environment

Related Articles:

  • Radix Sort:
  • Bucket Sort


D

disha_km
Improve
Article Tags :
  • Algorithms
  • DSA
  • Sorting
Practice Tags :
  • Algorithms
  • Sorting

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