You are on page 1of 7

qwertyuiopasdfghjklzxcvbnmq wertyuiopasdfghjklzxcvbnmqw ertyuiopasdfghjklzxcvbnmqwer tyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyui opasdfghjklzxcvbnmqwertyuiop asdfghjklzxcvbnmqwertyuiopas dfghjklzxcvbnmqwertyuiopasdf ghjklzxcvbnmqwertyuiopasdfgh jklzxcvbnmqwertyuiopasdfghjkl zxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcv bnmqwertyuiopasdfghjklzxcvbn mqwertyuiopasdfghjklzxcvbnm qwertyuiopasdfghjklzxcvbnmq

wertyuiopasdfghjklzxcvbnmqw ertyuiopasdfghjklzxcvbnmrtyui
A comparative investigation of the efficiency of common sorting Algorithms 5/22/2012 Zindove Tumai 210152443

Abstract This report focuses on comparing the efficiency of various sorting algorithms that are in everyday use. An investigation was carried out on the following algorithms: Non-recursive bubble sort, nonrecursive selection sort, non-recursive insertion sort, double insertion sort, double selection sort, merge sort, shell sort and quick sort. The recursive versions of the bubble, insertion and selection sorts were also investigated and compared on their own. The efficiency/performance of these sorting algorithms was done by designing an experiment in which the time taken by each of them to sort objects under the same conditions (that is, sorting the same number of objects in the same direction) was recorded. Results were used to plot graphs, and analysis was done, and conclusions were drawn. Introduction When thinking of it in simple terms, sorting sounds very pointless, yet it is one of the most important concepts in everyday life. What was going to happen if the words in the dictionary were not sorted? This rhetoric question can shed light on how important sorting is in everyday life whenever working with lists of data, whether big or small. The following examples of sorted lists show how handy sorting is: words in the dictionary, files in a directory, the index of a book, card catalogue in a library, list of courses offered by a university , just to mention a few. This discussion brings us to the main objective of this experiment, that is, to determine the most efficient sorting algorithm which can be used for a specific task. Full details are going to be in the experimental method section. Related Work The various sorting algorithms are going to be described in this section, and their sorting in ascending order is going to be described. They can also sort in descending order in the manner opposite to what is going to be discussed. The recursive versions of the bubble sort, the selection sort and the insertion sort organise data exactly the same way as the non-recursive counterparts, but they use recursive calls to accomplish the job. Bubble sort This algorithm sorts a given list by passing from one end of the list comparing adjacent elements, swapping them, if necessary, so that the smaller element comes first. The size of the list is therefore a prerequisite so that it knows the number of passes to make. Bubble Sort is simple and straightforward, however, it is highly inefficient, and is rarely used. It is inefficient because even if the list is almost sorted, n-1 passes need to be done for n elements (Bruno 1999:518-520). Insertion sort Insertion Sort works by dividing the list of objects into two portions, the sorted portion and the unsorted portion. By taking the first element as the sorted list, the algorithm takes elements one by one from the unsorted portion and inserts them into correct positions in the sorted portion of the list until the unsorted portion is empty. There are therefore two important steps involved in this algorithm, namely, finding the correct position at which to insert a new item and to move elements over to make room for the new element(Bruno 1999:513). The insertion sort is a little over twice as efficient as the bubble sort [Basit, 2007].

Selection sort Adam (1996:339) explains that the selection sort is an attempt to localize the exchanges of array elements by finding a misplaced element first and putting it in its final place. For ascending order, the smallest element is selected and swapped with the element in the first position. The smallest element in the remaining elements is found and swapped with the one in the second position, and the process continues until all the elements are in their correct positions. The best thing about this algorithm is the required number of assignments, which can hardly be beaten by any other algorithm (Adam 1996: 340); however, the number of swaps required (some of which are just redundant) makes it expensive. Shell Sort This algorithm was named after its inventor, Donald Shell, who invented the algorithm in 1959. Shell sort is an improvement of the insertion sort, and is better known as the diminishing increment sort, or the comb sort [Basit, 2007]. Instead of comparing adjacent elements, like what the insertion sort does, shell sort makes multiple passes through the list, sorting a number of equally sized sets of elements using the insertion sort. The distance, h, between elements being compared decreases by 1 after each pass until adjacent elements are compared (hence its name diminishing increment). In this way, shell sort improves insertion sort by quickly shifting elements to their destinations [Basit, 2007]. Despite being robust and efficient for medium-size lists, shell sort is considered to be somewhat complex and not nearly as efficient as the quick sort [Basit, 2007]. Double selection sort This algorithm is an improvement of the ordinary Selection Sort; it reduces the number of comparisons required in the ordinary selection sort. Double selection sort sorts a list by traversing the unsorted list to find the smallest and the largest element. The smallest element is swapped with the extreme left element and the larger one is simultaneously swapped with the extreme right element thereby sorting the list in ascending order from the ends towards the centre. Double selection sort is 25% faster than selection sort [Chavey, 2010], however, this improvement on the selection sort does not make double selection sort a better algorithm than double insertion sort since it is most appropriate when the comparisons required are cheaper than the assignments required[Chavey,2010]. Double selection sort is illustrated in the table below, in which the shaded portion of each row represents a pass of the algorithm. Table 1 Visualisation of Double Selection Sort 9 1 1 1 1 3 3 2 2 2 1 2 3 3 3 5 5 5 4 4 7 7 4 5 5 6 6 6 6 6 4 4 7 7 7 2 9 9 9 9

Double insertion sort Double insertion sort uses the concept of the insertion sort to sort an array from the centre going out. This algorithm is capable of sorting both lists with an even number of objects and lists with an odd number of objects, however, only the list of an even number of objects is going to be used for the purpose of describing how the algorithm works. For a list with an even number of elements, double insertion sort first sorts the center two elements. It then examines an element which is immediately to the left of the sorted portion and the one on the immediate right, inserts the lager of these two to the right and the smaller to the left using the insertion sort(it first swaps them if necessary). In this way, the insertion sort is improved (speeded up) in the sense that elements tend to move large steps towards their final destinations [Chavey, 2010]. Double insertion is 33% to 50 % faster than insertion sort [Chavey, 2010]. Merge sort This algorithm uses the concept of merging two sorted lists to come up with a bigger sorted list. It is a divide-and-conquer sorting algorithm (Alex: 2011) in that it divides the unsorted list into smaller lists until single-element lists are obtained, and merges them into one sorted list. Merge sort is fast and efficient; however, it is less handy than the quick sort because of the extra memory required for the merging process and the extra work of copying items to the temporary extra memory (Weiss 1999:269) Table 2 is an illustration of the divide and conquer process of the merge sort algorithm. Table 2 Merge sort visualised

Quick sort Quick sort is the fastest known algorithm in practice (Weiss 1999:269). It is effective for large lists of data with mostly unsorted elements .This algorithm is also a divide-and-conquer recursive algorithm like the merge sort (Weiss 1999:269); it divides the work into portions. The sorting process involves repeatedly selecting a pivot element and putting it in its correct position. For ascending order, elements smaller than the pivot element are organised to its left while elements bigger than the pivot element are organised to its right.

Experimental Method A social network was being simulated and the objects were contacts. Contacts have number of friends in common with the profile owner as one of their attributes. All the algorithms under test were given various lists of contacts (between 1 and 20 000) to sort them in descending order of number of common friends. The data type of the number of common friends was integer, and all algorithms were sorting on this same type of data for fair comparison. A timing class, which takes care of garbage collection, was implemented in order to record the time taken by each algorithm to sort a given number of objects. The sorting time for all the algorithms was recorded in a table for use in the plotting of graphs. Analysis of the results obtained was through plotting of graphs to show a clear comparison of the algorithms. Only the resulting graphs were presented in the results section and the tables were left out to avoid unnecessary repetition of data comparison. For the recursive algorithms (recursive bubble, recursive selection and recursive insertion), the list size was restricted to a maximum of 5000 objects to avoid stack overflow in the program. These algorithms were therefore compared on their own and not with those implemented for a maximum of 20000 objects. Results Figure 1 shows the comparison of the 8 sorting algorithms which were investigated and figure 2 shows comparison of the recursive versions of the bubble sort, the selection sort and the insertion sort.

time Vs array size for all the algorithms


45000 40000 35000 30000 Bubble Sort Insertion Sort Selection Sort Doulbe Insertion Sort Double Selection Sort

sorting time(milliseconds)

25000
20000 15000 10000 5000

Shell Sort
Merge Sort Quick Sort 0 5000 10000 15000 20000 25000

number of objects
Figure1: Comparison of all the sorting algorithms that were investigated

Recursive versions of the basic sorting Algorithms


2000 1800 1600 1400 1200 1000 800 600 400 200 0 Recursive Bubble Recursive Insertion Recursive Selection

Sorting Time(ms)

1000

2000

3000

4000

5000

6000

# of objects
Figure 2: Recursive bubble, Insertion and Selection Sorts compared Discussion of Results From figure 1 it is observed that for list size of approximately up to 100 objects, all the algorithms take almost the same amount of sorting time, which reflects that all the sorting algorithms are equally good for relatively small lists. In general, from figure 1, sorting time for all the algorithms increases as the size of the list grows bigger. Overall comparison of the sorting algorithms shows that bubble sort is the worst of all the algorithms which were investigated, with the gradient of its plot showing dramatic growth for list size in excess of 2500 objects. Insertion sort is the most efficient amongst the basic sorting algorithms (bubble, selection and insertion), with it being even faster than the double selection sort for both relatively small lists and large lists. Double insertion sort shows a slow growth in the gradient of its plot, which makes it reasonably efficient for large lists; however, it is slower than all the advanced sorting algorithms (merge, shell and quick sort). Theory predicted that quick sort is the fastest of all the investigated algorithms; however, experimental results show that shell sort is the fastest of all, with merge sort being the third fastest for all list sizes. It has been pointed out that quick sort is very efficient for extremely large lists, and the maximum list size of 20 000 used in the experiment might not be large enough for quick sort to show its exceptional capability, which is the possible reason why it was experimentally slower than shell sort. For the recursive algorithms (figure 2), it is observed that the efficiency of these algorithms follows the same trend as the non-recursive versions. Recursive insertion sort is the fastest, and recursive bubble sort is the worst for list sizes greater than 1000.

Conclusion Theory predicted that the quick sort is faster than shell sort, but this was not the case from the results of the experiment. It has been mentioned in the theory that quick sort is most effective when the elements are mostly unsorted, but from the particular experiment carried out, the majority of the number of contacts in common were zeros, which made the lists a bit diluted in terms of elements which must be adjacent being far apart from each other. This is a possible reason why quick sort was beaten by shell sort in the experiment. Bubble sort was mentioned as being highly inefficient and in the experiment it came out to be the worst algorithm especially for large lists, which was in agreement with the theory. Although the actual percentage difference in speed of certain algorithms (like in the theory) was not calculated, it was observed that from the results that theory was proved correct , with only the exception of the quick and the shell sort. References 1. Bruno, R.P.1999: Data structures and algorithms with Object-Oriented Design Patterns in C++. New York: John Wiley & Sons, Inc. 2. Adam, D.1996 Data structures and algorithms in C++. Boston: PWS publishing Company. 3. Basit, S. (2007): Enhanced Shell Sorting Algorithm, October. Available at http://waset.org/journals/waset/v27/v27-12.pdf [Accessed on 18 May 2012]. 4. Chavey, D.P. (2010): Double Sorting: testing their skills, March. [Beloit College, Dept of Mathematics & Computer Science , 700 College Street; Beloit, WI 53511, chavey@beloit.edu] 5. Alexia.(2011): http://www.cprogramming.com/tutorial/computersciencetheory/mergesort.html [Accessed on 19 May 2012]. 6. Weiss,M.A.(1999):Data structures and algorithms analysis in C++ .Harlow: Addison Wesley Longman, Inc.

You might also like