You are on page 1of 4

1.

Radix Sort Algorithm:

Radix sort is a non-comparison based sorting algorithm that sorts the input array one digit
at a time, starting from the least significant digit to the most significant digit. It uses a
stable sorting algorithm to sort the digits at each position.

The algorithm for radix sort is as follows:

RADIX-SORT(A, d)

1 for i = 1 to d do

2 use a stable sorting algorithm to sort array A on digit i

3 return A

Here, A is the input array of n elements, and d is the maximum number of digits in the
elements of A. In step 2, we can use any stable sorting algorithm like counting sort or
bucket sort to sort the digits at each position.

The time complexity of radix sort is O(d(n+k)), where k is the range of the digits at each
position. If k = O(n), then the time complexity of radix sort reduces to O(dn).

Consider an array of integers to be sorted using radix sort - {170, 45, 75, 90, 802, 24, 2, 66}.
We need to sort the numbers based on their digits, starting from the least significant digit.
The maximum number of digits in this array is 3.

Step 1: We need to find the maximum number of digits in the input data, which is 3.

Step 2: Initialize a bucket for each digit (0-9).

Step 3: Starting from the least significant digit, sort the input data by placing it in the
corresponding bucket according to that digit. For example, first, we need to sort the
numbers based on the units place digits. Therefore, we place the numbers in their
respective buckets based on their units place digits -

Bucket 0: Bucket 1: 170

Bucket 2: 802, 2

Bucket 3: 24

Bucket 4: 45

Bucket 5: 75

Bucket 6: 66
Bucket 7:

Bucket 8: 90

Bucket 9:

After sorting based on the units place digits, we concatenate the buckets in order to get
the new array - {170, 802, 2, 24, 45, 75, 66, 90}.

Next, we repeat the same process for the tens place digits. The new buckets are:

Bucket 0: 002, 024

Bucket 1:

Bucket 2:

Bucket 3: 045

Bucket 4:

Bucket 5: 075

Bucket 6: 066

Bucket 7:

Bucket 8: 170

Bucket 9: 090, 802

After sorting based on the tens place digits, we concatenate the buckets to get the new
array - {2, 24, 45, 66, 75, 90, 170, 802}.

Finally, we sort based on the hundreds place digits, which does not change the order of
the numbers in this case.

Therefore, the final sorted array is - {2, 24, 45, 66, 75, 90, 170, 802}.

The time complexity of radix sort in this case is O(3*(8+10)) = O(54), which is linear and
efficient.
2. Bucket Sort Algorithm:

Bucket sort is a non-comparison based sorting algorithm that works by distributing the
input elements into a fixed number of buckets, sorting each bucket separately, and then
concatenating the sorted buckets.

The algorithm for bucket sort is as follows:

BUCKET-SORT(A)

1 n = length[A]

2 let B[0..n-1] be an array of n empty lists

3 for i = 1 to n do

4 insert A[i] into list B[⌊nA[i]⌋]

5 for i = 0 to n-1 do

6 sort list B[i] with insertion sort

7 concatenate the lists B[0], B[1], ..., B[n-1] together in order

8 return the concatenated list

Here, A is the input array of n elements, and the range of A is assumed to be between 0
and 1. In step 2, we create an array of n empty lists. In step 4, we insert each element of A
into the corresponding bucket based on its value. The index of the bucket is calculated as
⌊nA[i]⌋, which gives the floor of n times the value of A[i]. In step 6, we sort each bucket
using insertion sort. Finally, in step 7, we concatenate the sorted buckets to get the final
sorted array.

The time complexity of bucket sort depends on the sorting algorithm used to sort each
bucket. In the worst case, if all the elements are inserted into the same bucket, then the
time complexity of bucket sort is O(n^2). However, if the input data is uniformly
distributed across the buckets, then the time complexity of bucket sort is O(n+k), where k
is the number of buckets.

Consider an array of floating-point numbers to be sorted using bucket sort - {0.42, 0.32,
0.21, 0.34, 0.82, 0.67}. We need to sort the numbers into ascending order.

Step 1: Create a fixed number of buckets, say 5.

Step 2: Scan the input data and place each element in the corresponding bucket. For
example, to place the element 0.42, we multiply it by the number of buckets and take the
integer part, which gives us 2. We place 0.42 in bucket 2. The resulting buckets are:

Bucket 0:
Bucket 1: 0.21

Bucket 2: 0.32, 0.34

Bucket 3:

Bucket 4:

Bucket 5: 0.82, 0.67

Step 3: Sort each bucket using any sorting algorithm. In this case, we use insertion sort to
sort each bucket.

Bucket 0:

Bucket 1: 0.21

Bucket 2: 0.32, 0.34

Bucket 3:
Bucket 4:

Bucket 5: 0.67, 0.82

After sorting each bucket, we concatenate the buckets in order to get the final sorted array
- {0.21, 0.32, 0.34, 0.67, 0.82}.

The time complexity of bucket sort depends on the sorting algorithm used to sort each bucket. In
this case, we used insertion sort, which has a worst-case time complexity of O(n^2). However, if the
input data is uniformly distributed across the buckets, the time complexity of bucket sort is O(n).
Therefore, the time complexity of bucket sort can vary between O(n) and O(n^2) depending on the
input data and the sorting algorithm used.

You might also like