# Radix Sort

## Introduction to Radix Sort

Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. Radix sort has been around since the days of punch cards and has been used to sort large amounts of data efficiently. The sorting process is done digit by digit from least significant digit to most significant digit, sometimes known as LSD radix sort, or from most significant digit to least significant digit, known as MSD radix sort.

In this lesson, we will cover:

- How Radix Sort works
- Real-world examples and scenarios of how Radix Sort is used
- A technical problem based on a real-world scenario
- Problem statement with a formal definition
- Solution to the problem
- Step by step implementation of Radix Sort
- Actual code solution with high-level comments
- Explanation of the code solution with intuitions and analogies
- Solving other real-world problems using Radix Sort

## How Radix Sort Works

Radix Sort works by sorting the input data based on each digit's value at a specific position. The algorithm processes the input data in multiple passes, where each pass sorts the data based on a specific digit's value. The number of passes is determined by the maximum number of digits in the input data. The sorting in each pass is done using a stable sorting algorithm, such as counting sort.

Here's a step-by-step breakdown of how LSD Radix Sort works:

- Determine the maximum number of digits in the input data.
- For each digit position starting from the least significant digit (LSD):
- Sort the input data using a stable sorting algorithm based on the digit's value at the current position.
- After all digit positions have been processed, the input data is sorted.

## Real-world Examples and Scenarios of Radix Sort

Radix Sort is used in various real-world scenarios, such as:

Sorting large datasets: Radix Sort is efficient in sorting large datasets, especially when the range of input data is small compared to the number of data points. For example, sorting a large dataset of phone numbers can be done efficiently using Radix Sort.

String sorting: Radix Sort can be used to sort strings by treating each character as a digit and sorting them based on their ASCII values.

Sorting records based on numeric attributes: In database systems, Radix Sort can be used to sort records based on numeric attributes, such as employee ID or salary.

## Real-world Scenario and Technical Problem

Let's consider a real-world scenario where a company wants to sort a large dataset of customer records based on their phone numbers. The phone numbers are 10-digit integers.

### Problem Statement and Formal Definition

Given a list of `n`

phone numbers, sort the phone numbers in non-decreasing order.

Input: An unsorted list of `n`

phone numbers, where `1 <= n <= 10^6`

and each phone number is a 10-digit integer.

Output: A sorted list of `n`

phone numbers in non-decreasing order.

## Tying the Problem Statement with the Real-world Scenario

In our real-world scenario, we have a large dataset of customer records which need to be sorted based on their phone numbers. This problem can be solved efficiently using Radix Sort, as the range of input data (10-digit integers) is small compared to the number of data points (customer records).

## Solution to the Problem

We will implement the LSD Radix Sort to solve this problem. The algorithm will sort the input data based on each digit's value, starting from the least significant digit (LSD) to the most significant digit (MSD).

Here's a step-by-step breakdown of our Radix Sort implementation:

- Determine the maximum number of digits in the input data (10 in this case).
- For each digit position starting from the least significant digit (LSD):
- Sort the input data using counting sort based on the digit's value at the current position.
- After all digit positions have been processed, the input data is sorted.

## Implementing Radix Sort with Code Solution

Here's the Python implementation of Radix Sort:

```
def counting_sort(arr, exp):
"""
Counting sort function to sort the input data based on the digit's value at position exp.
"""
n = len(arr)
output = [0] * n
count = [0] * 10
# Count the occurrences of each digit at position exp
for i in range(n):
index = arr[i] // exp
count[index % 10] += 1
# Calculate the cumulative count
for i in range(1, 10):
count[i] += count[i - 1]
# Build the output array
i = n - 1
while i >= 0:
index = arr[i] // exp
output[count[index % 10] - 1] = arr[i]
count[index % 10] -= 1
i -= 1
# Copy the output array to the input array
for i in range(n):
arr[i] = output[i]
def radix_sort(arr):
"""
Radix Sort function to sort the input data using LSD Radix Sort.
"""
# Find the maximum number in the input data to determine the number of digits
max_num = max(arr)
exp = 1
# Perform counting sort for each digit position
while max_num // exp > 0:
counting_sort(arr, exp)
exp *= 10
# Test the Radix Sort implementation
phone_numbers = [4382910234, 1293847560, 2938476591, 1029384756, 4839201029]
radix_sort(phone_numbers)
print(phone_numbers)
```

The output of the code should be:

```
[1029384756, 1293847560, 2938476591, 4382910234, 4839201029]
```

## Explaining the Code Solution with Intuitions and Analogies

In our solution, we first implement the `counting_sort`

function, which sorts the input data based on the digit's value at position `exp`

. This function uses counting sort, a stable sorting algorithm, to sort the input data. The main idea behind counting sort is to count the occurrences of each digit and then use the cumulative count to build the output array.

Next, we implement the `radix_sort`

function, which sorts the input data using LSD Radix Sort. The function first finds the maximum number in the input data to determine the number of digits. Then, it performs counting sort for each digit position, starting from the least significant digit (LSD) to the most significant digit (MSD).

Finally, we test our Radix Sort implementation on a list of phone numbers and print the sorted list.

## Solving Other Real-world Problems using Radix Sort

Radix Sort can be applied to solve other real-world problems, such as:

Sorting a large dataset of IP addresses: Radix Sort can be used to sort IP addresses by treating each octet as a digit and sorting them based on their values.

Sorting words in a dictionary: Radix Sort can be used to sort words in a dictionary by treating each character as a digit and sorting them based on their ASCII values.

Sorting a large dataset of dates: Radix Sort can be used to sort dates by treating each part of the date (day, month, and year) as a digit and sorting them based on their values.

In conclusion, Radix Sort is an efficient sorting algorithm that can be used to solve various real-world problems. By understanding the underlying principles and implementation details, you can apply Radix Sort to sort large datasets and improve the efficiency of your applications.