Bucket Sort is a simple and interesting sorting algorithm that works best when numbers are spread evenly over a known range. Instead of comparing every number with every other number, Bucket Sort divides values into small groups called buckets, sorts each bucket, and then joins everything back together. This approach makes the algorithm easy to understand and very practical for certain types of data.
with hands-on learning.
get the skills and confidence to land your next move.
Bucket Sort matters because it can be very fast when used in the right situation. It is often used when sorting floating point numbers, percentages, or values that fall within a fixed range, such as exam scores or sensor readings. Learning how to implement Bucket Sort in Perl helps beginners understand how data distribution can improve performance and also builds confidence in working with arrays and logic.
Program 1: Basic Bucket Sort Using Loops
This first program shows a very basic Bucket Sort implementation in Perl. It uses loops and simple logic to divide numbers into buckets and then sort them.
#!/usr/bin/perl
use strict;
use warnings;
my @numbers = (0.42, 0.32, 0.23, 0.52, 0.25, 0.47);
sub bucket_sort {
my @arr = @_;
my $n = scalar @arr;
my @buckets;
for (my $i = 0; $i < $n; $i++) {
push @buckets, [];
}
for my $value (@arr) {
my $index = int($n * $value);
push @{$buckets[$index]}, $value;
}
my @sorted;
for my $bucket (@buckets) {
my @temp = sort { $a <=> $b } @$bucket;
push @sorted, @temp;
}
return @sorted;
}
my @sorted = bucket_sort(@numbers);
print "Sorted array: @sorted\n";This program assumes all numbers are between 0 and 1. Each number is placed into a bucket based on its value, and each bucket is sorted using Perl’s built-in sort. Beginners can clearly see how dividing data into buckets makes sorting easier.
Program 2: Bucket Sort with Integer Values
This version adapts Bucket Sort for integer values within a known range. It helps beginners understand how Bucket Sort can work beyond decimal numbers.
#!/usr/bin/perl
use strict;
use warnings;
my @numbers = (29, 25, 3, 49, 9, 37, 21, 43);
sub bucket_sort {
my @arr = @_;
my $max = 50;
my $bucket_count = 5;
my @buckets = map { [] } (1..$bucket_count);
for my $num (@arr) {
my $index = int($num / $max * $bucket_count);
$index = $bucket_count - 1 if $index >= $bucket_count;
push @{$buckets[$index]}, $num;
}
my @sorted;
for my $bucket (@buckets) {
push @sorted, sort { $a <=> $b } @$bucket;
}
return @sorted;
}
print "Sorted array: ", join(" ", bucket_sort(@numbers)), "\n";Here, numbers are grouped based on their relative size compared to the maximum value. This program shows beginners that Bucket Sort works well when the data range is known in advance. It also demonstrates how simple math helps decide bucket placement.
Program 3: Bucket Sort Using Nested Loops
This program focuses on clarity by using nested loops instead of shortcuts. It is written to be easy to read and understand.
#!/usr/bin/perl
use strict;
use warnings;
my @numbers = (15, 11, 27, 8, 12, 14);
my $bucket_size = 5;
my @buckets;
for my $num (@numbers) {
my $index = int($num / $bucket_size);
push @{$buckets[$index]}, $num;
}
my @sorted;
for my $bucket (@buckets) {
if ($bucket) {
my @temp = sort { $a <=> $b } @$bucket;
push @sorted, @temp;
}
}
print "Sorted array: @sorted\n";This approach uses a fixed bucket size instead of a fixed number of buckets. Beginners can see how values naturally fall into ranges. This style is helpful when learning how to control data grouping manually.
Program 4: Bucket Sort with Helper Function
This version introduces a helper function to keep the code clean. It teaches beginners how to organize logic into reusable parts.
#!/usr/bin/perl
use strict;
use warnings;
sub bucket_sort {
my @arr = @_;
my $bucket_size = 10;
my @buckets;
for my $value (@arr) {
my $index = int($value / $bucket_size);
push @{$buckets[$index]}, $value;
}
my @result;
for my $bucket (@buckets) {
if ($bucket) {
push @result, sort { $a <=> $b } @$bucket;
}
}
return @result;
}
my @numbers = (34, 12, 78, 56, 9, 23);
print "Sorted array: ", join(" ", bucket_sort(@numbers)), "\n";By using a function, this program shows better coding habits. Beginners learn how sorting logic can be reused in other programs. It also makes the main script cleaner and easier to read.
Program 5: Bucket Sort for Floating Point Numbers
This program is designed specifically for floating point numbers between 0 and 1. It is a very common use case for Bucket Sort.
#!/usr/bin/perl
use strict;
use warnings;
my @numbers = (0.78, 0.17, 0.39, 0.26, 0.72, 0.94);
my $n = scalar @numbers;
my @buckets = map { [] } (1..$n);
for my $value (@numbers) {
my $index = int($n * $value);
push @{$buckets[$index]}, $value;
}
my @sorted;
for my $bucket (@buckets) {
push @sorted, sort { $a <=> $b } @$bucket;
}
print "Sorted array: @sorted\n";This program highlights why Bucket Sort is often used for decimals. Each bucket holds values in a small range, making sorting very fast. Beginners can easily experiment by changing the input values.
Program 6: Bucket Sort with Dynamic Range Detection
This version automatically finds the minimum and maximum values. It makes the algorithm more flexible.
#!/usr/bin/perl
use strict;
use warnings;
my @numbers = (45, 12, 89, 33, 76, 5);
my $min = $numbers[0];
my $max = $numbers[0];
for (@numbers) {
$min = $_ if $_ < $min;
$max = $_ if $_ > $max;
}
my $bucket_count = 5;
my @buckets = map { [] } (1..$bucket_count);
for my $num (@numbers) {
my $index = int(($num - $min) / ($max - $min + 1) * $bucket_count);
push @{$buckets[$index]}, $num;
}
my @sorted;
for my $bucket (@buckets) {
push @sorted, sort { $a <=> $b } @$bucket;
}
print "Sorted array: @sorted\n";This program teaches beginners how to adapt Bucket Sort to unknown data ranges. It is more realistic for real-world data. The logic still stays simple and readable.
Program 7: Bucket Sort with Manual Insertion Sort Inside Buckets
This version avoids Perl’s built-in sort and uses a simple insertion sort inside each bucket.
#!/usr/bin/perl
use strict;
use warnings;
sub insertion_sort {
my @arr = @_;
for (my $i = 1; $i < @arr; $i++) {
my $key = $arr[$i];
my $j = $i - 1;
while ($j >= 0 && $arr[$j] > $key) {
$arr[$j + 1] = $arr[$j];
$j--;
}
$arr[$j + 1] = $key;
}
return @arr;
}
my @numbers = (42, 32, 33, 52, 37, 47);
my @buckets;
for my $num (@numbers) {
my $index = int($num / 10);
push @{$buckets[$index]}, $num;
}
my @sorted;
for my $bucket (@buckets) {
push @sorted, insertion_sort(@$bucket) if $bucket;
}
print "Sorted array: @sorted\n";This program helps beginners see how Bucket Sort works with other algorithms. Each bucket is small, so insertion sort works efficiently. It also reinforces understanding of sorting basics.
Program 8: Bucket Sort with Clear Step-by-Step Flow
This program keeps everything in one place for easier learning. It is ideal for beginners who want to see the full logic without jumping between functions.
#!/usr/bin/perl
use strict;
use warnings;
my @numbers = (18, 29, 14, 33, 9, 25);
my @buckets;
for my $num (@numbers) {
my $index = int($num / 10);
push @{$buckets[$index]}, $num;
}
my @sorted;
for my $bucket (@buckets) {
if ($bucket) {
my @temp = sort { $a <=> $b } @$bucket;
push @sorted, @temp;
}
}
print "Sorted array: @sorted\n";This version is easy to tweak and experiment with. Beginners can change the input values and bucket logic to see different results. It is perfect for practice.
Program 9: Bucket Sort Supporting Negative and Floating Point Numbers
By default, Bucket Sort does not handle negative values well. This final program tweaks the logic to support negative and floating point numbers.
#!/usr/bin/perl
use strict;
use warnings;
my @numbers = (-2.5, 3.1, -1.2, 4.6, 0.0, 2.8);
my $min = $numbers[0];
my $max = $numbers[0];
for (@numbers) {
$min = $_ if $_ < $min;
$max = $_ if $_ > $max;
}
my $bucket_count = 5;
my @buckets = map { [] } (1..$bucket_count);
for my $num (@numbers) {
my $index = int(($num - $min) / ($max - $min + 0.0001) * $bucket_count);
$index = $bucket_count - 1 if $index >= $bucket_count;
push @{$buckets[$index]}, $num;
}
my @sorted;
for my $bucket (@buckets) {
push @sorted, sort { $a <=> $b } @$bucket;
}
print "Sorted array: @sorted\n";This program adjusts values based on the minimum and maximum range. It allows Bucket Sort to work with negative and decimal numbers. Beginners learn that algorithms can be modified to handle real-world data.
Frequently Asked Questions (FAQ)
This section answers common beginner questions about Bucket Sort in Perl.
Q1: When should I use Bucket Sort?
Bucket Sort works best when data is evenly distributed and the range is known or can be calculated.
Q2: Is Bucket Sort faster than Quick Sort?
Bucket Sort can be faster in specific cases, but Quick Sort is usually better for general-purpose sorting.
Q3: Does Bucket Sort work with strings?
Bucket Sort is mainly used for numbers, but it can be adapted for strings with extra logic.
Q4: Why do we still sort inside each bucket?
Buckets group similar values, but sorting inside them ensures correct order.
Q5: Should beginners use Perl’s built-in sort instead?
For real applications, built-in sort is easier, but Bucket Sort is excellent for learning algorithms.
Conclusion
Bucket Sort is a powerful yet simple algorithm that shows how smart data grouping can improve performance. By learning how to implement Bucket Sort in Perl, beginners gain a deeper understanding of sorting beyond basic comparisons.
Practicing these Perl Bucket Sort programs will improve your confidence with arrays, loops, and logic. Keep experimenting with different data ranges and inputs. The more you practice, the more natural algorithm thinking will become.




