|
| 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 | + |
| 87 | + |
| 88 | +- **Test Case 2 :** |
| 89 | +``` |
| 90 | +Input Given : |
| 91 | +arr = [181, 289, 390, 121, 145, 736, 514, 888, 122] |
| 92 | +``` |
| 93 | + |
| 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 | +[](https://www.python.org/) |
0 commit comments