Bubble Sort - Implementation in Perl

This section provides a tutorial on how to implement the Bubble Sort algorithm in Perl.

Bubble Sort is a simple and slow sorting algorithm that repeatedly steps through the collection, compares each pair of adjacent elements and swaps them if they are in the wrong order.

The basic idea of Bubble Sort algorithm can be described as these steps:

1. Data elements are grouped into two sections: a sorted section and an un-sorted section.

2. Go through every element in the un-sorted section and re-arrange its position with its neighbor to put the element with higher order on the higher position. At the end, the element with the highest order will be on top of the un-sorted section, and moved to the bottom of the sorted section.

3. Repeat step 2 until no more elements left in the un-sorted section.

In the sorting algorithm, if you watch the move of the elements with higher orders, they are like bubbles in the water, floating slowly from the bottom to the top.

Here is my Perl implementation of Bubble Sort algorithm:

#- Sort_Function.pl
#- Copyright (c) HerongYang.com. All Rights Reserved.
#-
sub bubbleSort {
  my ($a, $fromIndex, $toIndex) = @_;
  for ($i=$toIndex-1; $i>$fromIndex; $i--) {
    $isSorted = 1;
    for ($j=$fromIndex; $j<$i; $j++) {
      if (($a->[$j])>$a->[$j+1]) {
        $isSorted = 0;
        $d = $a->[$j+1];
        $a->[$j+1] = $a->[$j];
        $a->[$j] = $d;
      }
    }
    last if ($isSorted);
  }
}

# Functions for other sorting algorithms ...

#- End
1;

The following diagram illustrates how this implementation works:


                                     --17  42  53  67  ...         92
                                    |       |   |   |               |
        +---+---+---------------+---+---+---+---+---+---------------+
        |   |                   |     /
       29  36  11   ...        24  39
        |                           |   |                           |
fromIndex                           j   i                   toIndex-1

Note that:

Here are the performance test results of bubbleSort() function using Perl 5.18. The execution of bubbleSort() is so slow. I have to reduce the number of tests to 10 and 1 to avoid long waiting time.

Array size: 10000
Average sorting time: 19343.8862792969 milliseconds
Number of tests: 10
Performance: 1934.38862792969 O(N) microseconds
Performance: 145.577250069532 O(N*Log2(N)) microseconds
Performance: 0.193438862792969 O(N*N) microseconds

Array size: 20000
Average sorting time: 78359.980859375 milliseconds
Number of tests: 10
Performance: 3917.99904296875 O(N) microseconds
Performance: 274.221578576619 O(N*Log2(N)) microseconds
Performance: 0.195899952148438 O(N*N) microseconds

Array size: 30000
Average sorting time: 177352.602050781 milliseconds
Number of tests: 1
Performance: 5911.75340169271 O(N) microseconds
Performance: 397.490932147987 O(N*Log2(N)) microseconds
Performance: 0.19705844672309 O(N*N) microseconds

Here is the comparison of bubbleSort() performance with other sorting functions. As you can see, Bubble Sort is much slower than other sorting functions.

Array Size        10000   20000   30000   100000   200000   300000
----------        -----   -----   -----   ------   ------   ------
JDK Arrays.sort                               25       66      112
PHP sort()            3       7      13       75
Perl sort()          11      22      36      171
Insertion Sort     4125   16015   37098
Selection Sort     8054   31249   68985
Bubble Sort       19344   78360  177353

Table of Contents

 About This Book

 Introduction of Sorting Algorithms

 Java API for Sorting Algorithms

 Insertion Sort Algorithm and Java Implementation

 Selection Sort Algorithm and Java Implementation

 Bubble Sort Algorithm and Java Implementation

 Quicksort Algorithm and Java Implementation

 Merge Sort Algorithm and Java Implementation

 Heap Sort Algorithm and Java Implementation

 Shell Sort Algorithm and Java Implementation

 Sorting Algorithms Implementations in PHP

Sorting Algorithms Implementations in Perl

 Sort_Test.pl - Sorting Performance Test

 Insertion Sort - Implementation in Perl

 Selection Sort - Implementation in Perl

Bubble Sort - Implementation in Perl

 Quicksort - Implementation in Perl

 Merge Sort - Implementation in Perl

 Heap Sort - Implementation in Perl

 Shell Sort - Implementation in Perl

 Sorting Algorithms Implementations in Python

 Performance Summary of All Implementations

 References

 Full Version in PDF/EPUB