8. PSEUDOCODE :-
• radixSort()
• Find largest element (maxim) in the input array array[]
• For each place value in input (place=1;maxim/place>0;place*=10)
• Sort all elements on the basis of digits in that place value
• using counting sort
• For each array[] index (i=0;i<size;i++)
• Store all the sorted elements in the original array
• Output the sorted array[]
• end radixSort()
9. PASS 1 :- UNIT’S DIGIT
COUNT ARRAY :-
Count array after storing the count of each unit digit
in the original array:
Count array after storing the cumulative count so that
their values indicate their sorted positions:
Array after pass 1:
12. Bucket Sort
● Bucket sort assumes that the input is generated by a random process and drawn from
a uniform distribution. In other words the elements are distributed uniformly
and independently over the interval [0,1].
● Bucket sort divides the interval [0,1] into n equal sized subintervals or buckets. Then
distributes the n inputs into these buckets.
● After that the elements of each buckets are sorted using a sorting algorithm generally
using insertion or quick sort.
● Finally the buckets are concatenated together in order.
● Consider that the input is an n-element array A and each element A[i] in the array
satisfies the 0<=A[i]<1.
13. • define function bucketSort(arr[]) as
• step 1: create as many empty buckets (lists or arrays) as the length of the input
array
• step 2: store the array elements into the buckets based on their values
(described in step 3)
• step 3: for i from 0 to length of arr do
• (a) bucket_index = int((n * arr[i]) / 10) //here n is the length of the input
array
• (b) store the element arr[i] at bucket[bucket_index]
• step 4: for i from 0 to length of bucket do:
• (a) sort the elements stored inside bucket[i], either by applying recursion
or by
• some other sorting method
• step 5: push the values stored in each bucket starting from lowest index to
highest index to a
• new array/list
• step 6: return the sorted array/list
14. function bucket_sort (list, B) is // list is the name of the array
buckets <- new array of B empty lists
A <- the array's maximum key value
for i = 1 to length( list ) do
Insert array[i[ into buckets [ floor ( B * list[i] / A) ]
for i to B do
Sort buckets[i]
Return the gatttreation of buckets[1], buckets[2],...............buckets[B]
Pseudo code
15. ● Let's take an array A[]={0.78,0.26,0.72,0.17,0.39}. Total number of
elements are n=10. So we need to create 5 buckets.
maximum element(max)=0.78
● Using the formula: bi = (n)*arr[i]/(max+1), we find out bucket index
of each element of the array A[].
For eg: bucket index of 0th element is
bi= floor((5)(0.78)/(0.78+1))= 2
● Similarly,bucket index of 1st element is bi=0
bucket index of 2nd element is bi=1
bucket index of 3rd element is bi=0
bucket index of 4th element is bi=1
Now, since buckets have been created we need to concatenate all the
buckets in the order to get the sorted array.
19. Time Complexity of Bucket Sort
Worst-case complexity: In General, one bucket contains many elements when there are
elements of the normal range, But when there are elements of the very close range, they
are more likely to fall in the same bucket. So, in this case, more elements get placed in
the same bucket resulting in more number elements in some buckets than others. The
algorithm complexity depends on the number of elements in the bucket and, it becomes
even worse when the elements in the bucket are in reverse order. When insertion sort is
used to sort the elements then the time complexity will be O(n2)
20. ● Average case complexity: Average case complexity occurs when the elements are
distributed randomly in the array of buckets. In general, Bucket sort runs in linear time in
all cases until the sum of the squares of the bucket sizes is linear in the total number of
elements. Finally, this makes O(n) to be the average-case complexity of the bucket sort.
● Best case complexity: Best case complexity occurs when there are an equal number of
elements in the buckets and, it will be even better if the elements inside the buckets are
already sorted. Suppose insertion sort is used to sort the elements in the buckets. In that
case, the overall best case complexity will be O(n+k), where O(n) is the complexity for
making the buckets and O(k) is the complexity for sorting the elements in the buckets.