0% found this document useful (0 votes)
11 views

Python Algorithms Radix Sort and Bucket Sort 1717946949

Uploaded by

Steve McQueen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Python Algorithms Radix Sort and Bucket Sort 1717946949

Uploaded by

Steve McQueen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Algorithms 7 - Radix sort and Bucket sort

June 9, 2024

1 Day 39
Practicing python from basics

2 Radix sort
Radix Sort is a non-comparative sorting algorithm that sorts numbers digit by digit, starting from
the least significant digit to the most significant digit. It uses counting sort as a subroutine to sort.
• Complexity:
– Best, average, and worst-case: O(nk), where (k) is the number of digits in the largest
number.
• Best for: Sorting integers or strings of uniform length.
• Pros:
– Very efficient for sorting integers.
– Stable (maintains the relative order of equal elements).
• Cons:
– Limited to integers or fixed-length strings.
– Requires additional memory for the counting sort subroutine.

2.1 Implementation
[1]: def counting_sort(arr,exp):
n = len(arr)
output = [0]*n
count = [0]*10

for i in range(n):
index = arr[i]//exp
count[index%10] += 1

for i in range(1, 10):


count[i] += count[i-1]

1
i = n-1
while i > 0:
index = arr[i]//exp
output[count[index%10] - 1] = arr[i]
count[index%10] -= 1
i -= 1

for i in range(n):
arr[i] = output[i]

def radix_sort(arr):
max1 = max(arr)

exp = 1
while max1//exp>0:
counting_sort(arr,exp)
exp *= 10

return arr

2.2 Calling radix_sort to sort example list.


[2]: ex_list = [170, 45, 75, 90, 802, 24, 2, 66]
sorted_list = radix_sort(ex_list)
print("Sorted list:", sorted_list)

Sorted list: [0, 2, 24, 45, 66, 75, 90, 802]

2.3 using %%time to check how much time it takes


[3]: %%time
ex_list = [
53, 29, 77, 12, 34, 85, 42, 66, 23, 90, 55, 31, 48, 62, 75, 3, 9, 21, 39,␣
↪78,

49, 14, 28, 44, 50, 70, 82, 94, 18, 24, 67, 81, 2, 35, 61, 47, 8, 91, 40,␣
↪72,

99, 19, 38, 51, 63, 76, 88, 5, 13, 30, 59, 79, 93, 7, 26, 41, 69, 80, 54, 1,
32, 56, 74, 86, 92, 16, 36, 46, 68, 84, 95, 6, 27, 45, 52, 60, 73, 87, 98,␣
↪10,

20, 33, 64, 71, 83, 96, 15, 25, 37, 43, 57, 65, 89, 97, 4, 17, 22, 11, 58
]

sorted_list = radix_sort(ex_list)
print(F"sorted list : {sorted_list}")

sorted list : [1, 2, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,

2
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 0, 91, 92, 93, 94, 95, 96, 97, 98, 99]
CPU times: total: 0 ns
Wall time: 0 ns

3 Bucket sort
Bucket Sort distributes the elements of an array into a number of buckets. Each bucket is then
sorted individually, either using a different sorting algorithm or by recursively applying Bucket
Sort.
• Complexity:
– Best-case: O(n + k) when the elements are uniformly distributed across the buckets.
– Worst-case: O(n^2) if all elements are placed in a single bucket.
• Best for: Uniformly distributed floating-point numbers.
• Pros:
– Can be very efficient with uniformly distributed data.
– Simple and intuitive.
• Cons:
– Efficiency depends on the distribution of the input.
– Requires additional memory for buckets.

3.1 Implementation
[13]: def bucket_sort(arr):
n = len(arr)
min_val = min(arr)
max_val = max(arr)
bucket = []

for i in range(n):
bucket.append([])

for j in arr:
index_b = int( (j-min_val) / (max_val - min_val +1) * n )
bucket[index_b].append(j)

for i in range(n):
bucket[i] = sorted(bucket[i])

k = 0
for i in range(n):

3
for j in range(len(bucket[i])):
arr[k] = bucket[i][j]
k += 1

return arr

3.2 Calling bucket_sort to sort example list.


[14]: ex_list = [0.897, 0.565, 0.656, 0.123, 0.665, 0.343]
sorted_list = bucket_sort(ex_list)
print("Sorted list:", sorted_list)

Sorted list: [0.123, 0.343, 0.565, 0.656, 0.665, 0.897]

3.3 using %%time to check how much time it takes


[15]: %%time
ex_list = [
53, 29, 77, 12, 34, 85, 42, 66, 23, 90, 55, 31, 48, 62, 75, 3, 9, 21, 39,␣
↪78,

49, 14, 28, 44, 50, 70, 82, 94, 18, 24, 67, 81, 2, 35, 61, 47, 8, 91, 40,␣
↪72,

99, 19, 38, 51, 63, 76, 88, 5, 13, 30, 59, 79, 93, 7, 26, 41, 69, 80, 54, 1,
32, 56, 74, 86, 92, 16, 36, 46, 68, 84, 95, 6, 27, 45, 52, 60, 73, 87, 98,␣
↪10,

20, 33, 64, 71, 83, 96, 15, 25, 37, 43, 57, 65, 89, 97, 4, 17, 22, 11, 58
]

sorted_list = bucket_sort(ex_list)
print(F"sorted list : {sorted_list}")

sorted list : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
99]
CPU times: total: 0 ns
Wall time: 1.43 ms

You might also like