Skip to main content

Radix Sort


Radix sort is a sorting algorithm that operates by sorting the elements of an array by examining their digits. It is based on the idea of distributing elements into buckets according to the value of a specific digit. This process is repeated for each digit, starting from the least significant to the most significant digit.

The algorithm starts by selecting a digit position and grouping the elements based on their value at that position. For example, if the selected digit position is the ones place, all elements with a 0 in the ones place are grouped together, followed by all elements with a 1 in the ones place, and so on. After grouping the elements, the algorithm concatenates the buckets in the order they were created, resulting in a partially sorted array. This process is repeated for each digit position until the entire array is sorted.

Radix sort has a linear time complexity of O(kn), where k is the maximum number of digits in any element and n is the number of elements in the array. This makes it efficient for large datasets, as it does not depend on the magnitude of the elements being sorted. Additionally, radix sort is stable, meaning that it preserves the relative order of equal elements.

One disadvantage of radix sort is that it requires additional memory to store the buckets. The size of the buckets is determined by the range of possible values for the digit being examined. For example, if the digits are represented in base 10 (0-9), there would be 10 buckets. This can result in high memory usage for large datasets or when working with elements with a large range of possible values.

Another consideration when using radix sort is the choice of digit representation. Different digit representations can affect the speed and memory usage of the algorithm. For example, using binary representation can reduce memory usage but may require additional operations to convert the elements to and from binary.

In conclusion, radix sort is a sorting algorithm that operates by sorting the elements of an array by examining their digits. It has a linear time complexity of O(kn) and is efficient for large datasets. However, it requires additional memory to store the buckets and the choice of digit representation can affect the algorithm's performance.


ALGORITHM:

The steps of radix sort algorithm are as follows:

    1. Determine the maximum number of digits (k) in the input array.
    2. For each digit position (i), starting from the least significant digit to the most significant digit:
        a. Create 10 buckets, numbered 0 to 9.
        b. Iterate through the input array and distribute each element into the appropriate bucket based on the value of its digit at position i.
        c. Concatenate the buckets in the order they were created to form a partially sorted array.
    3. Repeat step 2 for each digit position until the entire array is sorted.

It's important to note that for the bucket distribution step in step 2b, it's common to use a stable sorting algorithm, such as counting sort or insertion sort, to sort the elements within each bucket. This ensures that the relative order of equal elements is preserved in the sorted array.

At the end of the algorithm, the input array will be sorted in non-decreasing order. The time complexity of radix sort is O(kn), where k is the maximum number of digits in any element and n is the number of elements in the array. Radix sort is efficient for large datasets as it does not depend on the magnitude of the elements being sorted, but it does require additional memory to store the buckets.

PSEUDO CODE:

Here's an example of pseudocode for radix sort algorithm:



In this pseudocode, get_max_digits(arr) is a function that returns the maximum number of digits in the input array. get_digit(num, pos) is a function that returns the digit at the specified position pos in the number num. concatenate_buckets(buckets) is a function that concatenates the buckets in the order they were created to form a partially sorted array.

Note that the above pseudocode assumes that all elements in the input array have the same number of digits. If the input array contains elements of varying lengths, some modifications may be necessary to handle this case.

PYTHON CODE:

Here's an implementation of radix sort in Python:



In this Python code, max(arr) returns the maximum value in the input array, and len(str(max(arr))) returns the number of digits in the maximum value. buckets is a list of 10 empty lists to hold the elements in each bucket. The // operator is used for integer division and the ** operator is used for exponentiation. The concatenated buckets are added back into the input array in sorted order.

JAVA CODE:

Here's an implementation of radix sort in Java:



In this Java code, Arrays.stream(arr).max().getAsInt() returns the maximum value in the input array, and Integer.toString() converts it to a string to determine the number of digits. List<Integer>[] buckets is an array of 10 ArrayLists to hold the elements in each bucket. The Math.pow(10, i) expression is used to isolate each digit in the input numbers. The concatenated buckets are added back into the input array in sorted order.

C CODE:

Here's an implementation of radix sort in C:



In this C code, max_digits is determined by iterating over the input array and counting the number of digits in each element. buckets is a 2D array of 10 rows (one for each digit) and n columns (one for each element in the input array) to hold the elements in each bucket. counts is an array of 10 integers to keep track of the number of elements in each bucket. The pow() function from math.h is used to isolate each digit in the input numbers. The concatenated buckets are added back into the input array in sorted order.

C++ CODE:

Here's an implementation of radix sort in C++:



In this C++ code, max_digits is determined by iterating over the input array and computing the number of digits in each element using the log10 function. buckets is a vector of 10 vectors to hold the elements in each bucket. The pow function from cmath is used to isolate each digit in the input numbers. The concatenated buckets are added back into the input vector in sorted order using the insert function.

JAVASCRIPT CODE:

Here's an implementation of radix sort in JavaScript:



In this JavaScript code, maxDigits is determined by computing the maximum number of digits in the input array. buckets is an array of 10 empty arrays to hold the elements in each bucket. The floor and pow functions are used to isolate each digit in the input numbers. The concatenated buckets are added back into the input array in sorted order using the flat function.

Note that in this implementation, the input array is not modified in place, and the sorted array is returned as the result of the function.

Comments

Post a Comment

Popular posts from this blog

Selection Sort

Selection sort is a simple sorting algorithm that sorts an array by repeatedly finding the minimum element from the unsorted portion of the array and swapping it with the first element of the unsorted portion. It is called selection sort because it repeatedly selects the smallest element of the unsorted portion of the array and moves it to its correct position in the sorted portion of the array. The time complexity of selection sort is O(n^2), where n is the number of elements in the array. This makes it inefficient for large arrays and it is not a good choice for sorting large datasets. However, selection sort is easy to understand and implement, and it is useful for educational purposes and for small arrays. One of the advantages of selection sort is that it makes fewer swaps than other sorting algorithms such as bubble sort. This makes it more efficient in terms of memory writes, which can be important in some situations. Another advantage of selection sort is that it is a stable so

Counting Sort

Counting sort is a sorting algorithm that operates by counting the frequency of each element in the input array, and then using these frequencies to compute the final sorted output. The algorithm works by first creating an array of counters, with each counter corresponding to a distinct element in the input array. The counters are initialized to zero, and then the input array is scanned once to count the number of occurrences of each element. This count information is stored in the corresponding counter in the counter array. Once the count information is obtained, the algorithm uses it to determine the final position of each element in the sorted output array. This is done by iterating over the counter array, and for each counter value, writing the corresponding element to the sorted output array the appropriate number of times. The time complexity of counting sort is O(n+k), where n is the length of the input array and k is the range of the elements. The space complexity is also O(n+k

Pigeonhole Sort

Pigeonhole sort is a sorting algorithm that works by distributing elements of an input sequence into a set of pigeonholes or buckets. It is an efficient sorting algorithm for small ranges of values where the number of elements to be sorted is roughly the same as the range of values. Pigeonhole sort is an example of a bucket sort algorithm. The pigeonhole sort algorithm works by first determining the range of values in the input sequence. This range is used to create a set of pigeonholes or buckets, each of which can hold one or more elements from the input sequence. Each element in the input sequence is then placed into its corresponding pigeonhole based on its value. If two or more elements have the same value, they can be placed in the same pigeonhole. Once all the elements have been placed into their corresponding pigeonholes, the elements are sorted within each pigeonhole. Finally, the sorted elements are combined into a single output sequence. Pigeonhole sort has a time complexity