Schudt 1

Erich Schudt

Performance Analysis of , and Sort

Introduction

Sorting techniques are useful in many different fields. One such field is databases, whether a dictionary database or a database of lists of house. Many application use sorting algorithms to gain desired outputs. As such, it is only natural that a performance analysis of different sorting algorithms be analyzed. In this case, that of bubble sort, merge sort and binary search tree sort. Their performances with respect to time were analyzed with different sizes of lists and varying randomness of the list.

Method

A program was written to perform this analysis. First of all, a list of size N is chosen and the percent randomness of the list and its corresponding time to sort using all three algorithms was done. This was done for 5 different lists expanding each time by 2 * the previous list size. The initial list size was 1024.

To compute the percent randomness, some elements of a sorted list were swapped. The following two factors were considered when calculating the randomness of the resultant list:

 How many numbers were swapped, which counted for 90% of a list’s randomness.  How far a swapped number was away from its original position, which counted for 10% of a list’s randomness.

These two factors combined gave the randomness of a list. The formula used to calculate the randomness is as follows:

* 90) + ( ∑

Where

 n = the number of elements out of place  N = the total number of elements (the size of the list)  d = the distance a number is away from its sorted position  D = the maximum distance the number can be away from its sorted position Schudt 2

The time taken to sort a random list was then recorded for each . A graph of the size, the percentage randomness and the time taken was computed using Mathematica for each sorting algorithm and compared. Results

The program was run on a Dell Optiplex 745 with 1GB of ram and a 1.8GHZ processor running Windows xp.

A 3D graph was plotted using Mathematica with z axis as the time, and the xy plane as the percent randomness and the size of the list to sort for each algorithm

Bubble sort

As seen in the z axis, the time taken to sort a list ranged from 0 to 5 seconds. It can be seen that the greater the size of the list, the more time bubble sort takes to sort the list. Also, the greater the percent randomness, the greater the time needed to sort the list. Therefore, the maximum time was recorded at maximum randomness and at maximum list size. However, the time it takes to sort a list with about 10% randomness or less was below 1 second up to a list size of about 10,000. This shows that bubble sort work best for a small list and a small randomness. This can be explored further by minimizing the randomness of the list1 and running the program again.

1 This can be done by changing the random level in the code to RandomLevel.min Schudt 3

As seen in the diagram to the left by looking at the z axis, the peak time to sort a list of about 16, 000 elements is now only 2 seconds.

Merge Sort

The time taken to sort a list using the is much less than that of bubble sort. Its peak time was a bit over 0.003 seconds. The distribution looks very uniform showing that no matter the size of the list or the amount of randomness, merge sort seems to work at a constant rate. This could be explored more by changing the percentage randomness.

Schudt 4

Binary Search Tree Sort

The binary search tree seems to work like the merge sort but is relatively slower when the percentage of randomness is a lot lower. The time taken to sort any list ranges from 0 – 0.008 seconds but this turns to 0 – 0.003 seconds (as seen in merge sort) when the percentage randomness increases. It’s performance with more random and less random lists can be explored as shown below:

The time in the z axis now ranges from 0 – 0.03 seconds. The plane peaks at the least random lists, showing it takes a much longer time to sort lists that are almost sorted.

The time in the z axis now ranges from 0 – 0.008 seconds. Except for slight discrepancies, binary tree search sort becomes closer to merge sort as the list is more random Schudt 5

Discussion

From the analysis, merge sort is the fasts sorting algorithm between the three. Binary search tree sort follows next then bubble sort, which is very slow compared to the other two. Merge sort and bubble sort diagrams follow an almost uniform pattern. This is because their efficiency is constant:

Merge sort is N lg N

Bubble sort is N²

Binary Search Tree sort has diagrams that are bumpy or wavy. This is because its performance ranges from N lg N to N² depending on how random the list is. Given a completely reverse list, binary search tree sort would have efficiency equal to that of bubble sort. Further experiments would have to be carried out to prove this. But generally, it is an efficient way of sorting.

Questions

Other than time, how else could the performances of sorting algorithms be analyzed?

What other factors aside from the number of comparisons should be considered when rating sorting algorithms.

Is there a way to improve the performance of binary search tree sorting, bubble sorting and merge sorting?