Visualize the computational complexity of two sorting algorithms,
bubble sort and merge sort, which sort list elements in ascending order. Bubble sort is a
simple sorting algorithm that repeatedly steps through a list, compares adjacent pairs of
elements, and swaps elements if they are in the wrong order. Merge sort is a "divide and
conquer" algorithm that takes advantage of the ease of merging sorted sublists into a new
sorted list.
In your current folder, save the following code in
bubbleSort.m
.
Save the following code in mergeSort.m
.
Create the following parameterized test class, TestSort
, which
compares the performance of the bubble sort and merge sort algorithms. The
len
property contains the number of list elements you want to
test.
Run performance tests for all test elements that correspond to the
'testBubbleSort'
method, and save the results in the
Baseline
array. Your results might vary from the results
shown.
Running TestSort
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .....
Done TestSort
__________
Run performance tests for all elements that correspond to the
'testMergeSort'
method, and save the results in the
Measurement
array.
Running TestSort
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
Done TestSort
__________
Visually compare the minimum of the MeasuredTime
column in the
Samples
table for each corresponding pair of
Baseline
and Measurement
objects.
In this comparison plot, most data points are blue because they are below the shaded
similarity region. This result indicates the superior performance of merge sort for the
majority of test cases. However, for small enough lists, bubble sort performs better
than or comparable to merge sort, as shown by the orange and gray points on the
plot.
As a comparison summary, the plot reports a 78% performance improvement due to merge
sort. This value is the geometric mean of the improvement percentages corresponding to
all data points. If the comparison plot contained invalid data points, a comparison
summary would not be generated.
You can press or hover over any data point to view detailed information about the
time measurement results being compared.
To study the worst-case sorting algorithm performance for different list lengths,
create a comparison plot based on the maximum of sample measurement times.
Reduce SimilarityTolerance
to 0.01
when
comparing the maximum of sample measurement times. Return the
ComparisonPlot
object in cp
so that you can modify
its properties later.