Skip to content

Commit b975982

Browse files
Merge pull request #219 from abhisheks008/main
Radix Sort [Folder: Sorting]
2 parents bd39339 + 99cf2fb commit b975982

File tree

4 files changed

+204
-0
lines changed

4 files changed

+204
-0
lines changed
13.9 KB
Loading
13 KB
Loading

Sorting/Radix Sort/README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Radix Sort Algorithm
2+
🔴 Language used : **Python 3**
3+
4+
## 🎯 Aim
5+
The aim of this script is to perform the sorting process using the Radix Sort algortihm.
6+
7+
## 👉 Purpose
8+
The main purpose of this script is to show the implementation of Radix Sort algorithm on an unsorted array of elements to make it a sorted one, with less time complexity than the merge sort, quick sort.
9+
10+
## 📄 Description
11+
Radix sort is the linear sorting algorithm that is used for integers. In Radix sort, there is digit by digit sorting is performed that is started from the least significant digit to the most significant digit.
12+
13+
The process of radix sort works similar to the sorting of students names, according to the alphabetical order. In this case, there are 26 radix formed due to the 26 alphabets in English. In the first pass, the names of students are grouped according to the ascending order of the first letter of their names. After that, in the second pass, their names are grouped according to the ascending order of the second letter of their name. And the process continues until we find the sorted list.
14+
15+
🔴 Examples:
16+
17+
```
18+
Constraints:
19+
arr[] -> array of unsorted elements.
20+
countingSort(arr, exp1) -> implementation of Counting Sort before Radix Sort.
21+
radixSort(arr) -> implementation of Radix sort.
22+
23+
Input:
24+
Array before implementing the Radix Sort...
25+
170 45 75 90 802 24 2 66
26+
27+
Radix Sort going on...
28+
29+
Output:
30+
After implementing Radix Sort algorithm...
31+
2 24 45 66 75 90 170 802
32+
```
33+
34+
## 📊 Flowchart
35+
```
36+
radixSort(arr)
37+
max = largest element in the given array
38+
d = number of digits in the largest element (or, max)
39+
Now, create d buckets of size 0 - 9
40+
for i -> 0 to d
41+
sort the array elements using counting sort (or any stable sort) according to the digits at
42+
the ith place
43+
```
44+
45+
## 🧮 Algorithm
46+
The steps used in the sorting of radix sort are listed as follows -
47+
- First, we have to find the largest element (suppose max) from the given array. Suppose `x` be the number of digits in max. The `x` is calculated because we need to go through the significant places of all elements.
48+
- After that, go through one by one each significant place. Here, we have to use any stable sorting algorithm to sort the digits of each significant place.
49+
Now let's see the working of radix sort in detail by using an example.
50+
51+
To understand it more clearly, let's take an unsorted array and try to sort it using radix sort. It will make the explanation clearer and easier.
52+
53+
For the understanding of the algorithm let's take the above mentioned example,
54+
The Input array is given as,
55+
```
56+
[170, 45, 75, 90, 802, 24, 2, 66]
57+
```
58+
In the given array, the largest element is `802` that have `3` digits in it. So, the loop will run up to three times (i.e., to the hundreds place). That means three passes are required to sort the array.
59+
60+
Now, first sort the elements on the basis of unit place digits (i.e., `x = 0`). Here, we are using the counting sort algorithm to sort the elements.
61+
62+
- **Pass 1**: In the first pass, the list is sorted on the basis of the digits at 0's place.
63+
```
64+
[170, 90, 802, 2, 24, 45, 75, 66]
65+
```
66+
- **Pass 2**: In this pass, the list is sorted on the basis of the next significant digits (i.e., digits at 10th place).
67+
```
68+
[2, 802, 24, 45, 66, 75, 170, 90]
69+
```
70+
- **Pass 3**: In this pass, the list is sorted on the basis of the next significant digits (i.e., digits at 100th place).
71+
```
72+
[2, 24, 45, 66, 75, 90, 170, 802]
73+
```
74+
Now, the array is sorted in ascending order. The final result is,
75+
```
76+
[2, 24, 45, 66, 75, 90, 170, 802]
77+
```
78+
79+
## 💻 Input and Output
80+
- **Test Case 1 :**
81+
```
82+
Input Given :
83+
arr = [170, 45, 75, 90, 802, 24, 2, 66]
84+
```
85+
86+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Sorting/Radix%20Sort/Images/radix-sort-2.png)
87+
88+
- **Test Case 2 :**
89+
```
90+
Input Given :
91+
arr = [181, 289, 390, 121, 145, 736, 514, 888, 122]
92+
```
93+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Sorting/Radix%20Sort/Images/radix-sort-1.png)
94+
95+
## ⏰ Time and Space complexity
96+
- **Time Complexity :** `O(n+k)`.
97+
- **Space Complexity :** `O(n+k)`.
98+
99+
---------------------------------------------------------------
100+
## 🖋️ Author
101+
**Code contributed by, _Abhishek Sharma_, 2022 [@abhisheks008](github.com/abhisheks008)**
102+
103+
[![forthebadge made-with-python](http://ForTheBadge.com/images/badges/made-with-python.svg)](https://www.python.org/)

Sorting/Radix Sort/radix_sort.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Problem Name: Radix Sort Algorithm
2+
# Problem Statement: Given an unsorted array of elements (here integers),
3+
# the task is to perform the Radix Sort algorithm to
4+
# sort the elements in the ascending order.
5+
6+
# ------------------------------------------------------------------------------
7+
8+
# Constraints:
9+
# arr[] -> array of unsorted elements.
10+
# countingSort(arr, exp1) -> implementation of Counting Sort before Radix Sort.
11+
# radixSort(arr) -> implementation of Radix sort.
12+
13+
# ------------------------------------------------------------------------------
14+
15+
# Python program for implementation of Radix Sort
16+
# A function to do counting sort of arr[] according to
17+
# the digit represented by exp.
18+
19+
def countingSort(arr, exp1):
20+
21+
n = len(arr)
22+
23+
# The output array elements that will have sorted arr
24+
output = [0] * (n)
25+
26+
# initialize count array as 0
27+
count = [0] * (10)
28+
29+
# Store count of occurrences in count[]
30+
for i in range(0, n):
31+
index = arr[i] // exp1
32+
count[index % 10] += 1
33+
34+
# Change count[i] so that count[i] now contains actual
35+
# position of this digit in output array
36+
for i in range(1, 10):
37+
count[i] += count[i - 1]
38+
39+
# Build the output array
40+
i = n - 1
41+
while i >= 0:
42+
index = arr[i] // exp1
43+
output[count[index % 10] - 1] = arr[i]
44+
count[index % 10] -= 1
45+
i -= 1
46+
47+
# Copying the output array to arr[],
48+
# so that arr now contains sorted numbers
49+
i = 0
50+
for i in range(0, len(arr)):
51+
arr[i] = output[i]
52+
53+
# Method to do Radix Sort
54+
def radixSort(arr):
55+
56+
# Find the maximum number to know number of digits
57+
max1 = max(arr)
58+
59+
# Do counting sort for every digit. Note that instead
60+
# of passing digit number, exp is passed. exp is 10^i
61+
# where i is current digit number
62+
exp = 1
63+
while max1 / exp >= 1:
64+
countingSort(arr, exp)
65+
exp *= 10
66+
67+
68+
# Driver code
69+
arr = [170, 45, 75, 90, 802, 24, 2, 66]
70+
71+
print ("-- Implementation of Radix Sort Algorithm --")
72+
print ()
73+
print ("Array before implementing the Radix Sort...")
74+
print (" ".join(str(k) for k in arr))
75+
print ()
76+
print ("Radix Sort going on...")
77+
print ()
78+
print ("After implementing Radix Sort algorithm...")
79+
80+
# Function Call
81+
radixSort(arr)
82+
print (" ".join(str(k) for k in arr))
83+
84+
# ------------------------------------------------------------------------------
85+
86+
# Output:
87+
# -- Implementation of Radix Sort Algorithm --
88+
89+
# Array before implementing the Radix Sort...
90+
# 170 45 75 90 802 24 2 66
91+
92+
# Radix Sort going on...
93+
94+
# After implementing Radix Sort algorithm...
95+
# 2 24 45 66 75 90 170 802
96+
97+
# ------------------------------------------------------------------------------
98+
99+
# Code contributed by, Abhishek Sharma, 2022
100+
101+
# ------------------------------------------------------------------------------

0 commit comments

Comments
 (0)