Merge Sort Roberto Hibbler Dept
Total Page:16
File Type:pdf, Size:1020Kb
Merge Sort Roberto Hibbler Dept. of Computer Science Florida Institute of Technology Melbourne, FL 32901 [email protected] ABSTRACT solution in Section 3, the evaluation of our results in Section 4, Given an array of elements, we want to arrange those elements and our final conclusion in Section 5. into a sorted order. To sort those elements, we will need to make comparisons between the individual elements efficiently. Merge Sort uses a divide and conquer strategy to sort an array 2. RELATED WORK efficiently while making the least number of comparisons The three algorithms that we will discuss are Bubble Sort, between array elements. Our results show that for arrays with Selection Sort, and Insertion Sort. All three are comparison sort large numbers of array elements, Merge Sort is more efficient algorithms, just as Merge Sort. than three other comparison sort algorithms, Bubble Sort, Insertion Sort, and Selection Sort. Our theoretical evaluation The Bubble Sort algorithm works by continually swapping shows that Merge Sort beats a quadratic time complexity, while adjacent array elements if they are out of order until the array is our empirical evaluation shows that on average Merge Sort is 32 in sorted order. Every iteration through the array places at least times faster than Insertion Sort, the current recognized most one element at its correct position. Although algorithmically efficient comparison algorithm, with one real data set. correct, Bubble Sort is inefficient for use with arrays with a large number of array elements and has a ͉ʚͦ͢ʛ worst case time Keywords complexity. Knuth observed[1], also, that while Bubble Sort Merge Sort, sorting, comparisons, Selection Sort, time shares the worst-case time complexity with other prevalent complexity sorting algorithms, compared to them it makes far more element swaps, resulting in poor interaction with modern CPU hardware. 1. INTRODUCTION We intend to show that Merge Sort needs to make on average The ability to arrange an array of elements into a defined order fewer element swaps than Bubble Sort. is very important in Computer Science. Sorting is heavily used with online stores, were the order that services or items were The Selection Sort algorithm arranges array elements in order by purchased determines what orders can be filled and who first finding the minimum value in the array and swapping it receives their order first. Sorting is also essential for the with the array element that is in its correct position depending database management systems used by banks and financial on how the array is being arranged. The process is then repeated systems, such as the New Stock Exchange, to track and rank the with the second smallest value until the array is sorted. This billions of transactions that go on in one day. There are many creates two distinctive regions within the array, the half that is algorithms, which provide a solution to sorting arrays, including sorted and the half that has not been sorted. Selection Sort shows algorithms such as Bubble Sort[1], Insertion Sort[2], and an improvement over Bubble Sort by not comparing all the Selection Sort[3]. While these algorithms are programmatically elements in its unsorted half until it is time for that element to be correct, they are not efficient for arrays with a large number of placed into its sorted position. This makes Selection Sort less elements and exhibit quadratic time complexity. affected by the input’s order. Though, it is still no less inefficient with arrays with a large number of array elements. We are given an array of comparable values. We need to arrange Also, even with the improvements Selection Sort still shares the these values into either an ascending or descending order. same worst-case time complexity of ͉ʚͦ͢ʛ. We intend to show that Merge Sort will operate at a worst-case time complexity We introduce the Merge Sort algorithm. The Merge Sort faster than ͉ʚͦ͢ʛ. algorithm is a divide-and-conquer algorithm. It takes input of an array and divides that array into sub arrays of single elements. A The Insertion Sort algorithm takes elements from the input array single element is already sorted, and so the elements are sorted and places those elements in their correct place into a new array, back into sorted arrays two sub-arrays at a time, until we are left shifting existing array elements as needed. Insertion Sort with a final sorted array. We contribute the following: improves over Selection Sort by only making as many comparisons as it needs to determine the correct position of the 1. We introduce the Merge Sort algorithm. current element, while Selection Sort makes comparisons 2. We show that theoretically Merge Sort has a worst- against each element in the unsorted part of the array. In the case time complexity faster than ͦ . )v ͉ʚ͢ ʛ average case, Insertion Sort’s time complexity is ͉ʚ ʛ, but its 3. We show that empirically Merge Sort is faster than ͨ worst case is ͦ , the same as Bubble Sort and Selection Selection Sort. ͉ʚ͢ ʛ Sort. The tradeoff of Insertion Sort is that on the average more elements are swapped as array elements are shifted within the This paper will discuss in Section 2 comparison sort algorithms array with the addition of new elements. Even with its related to the problem, followed by the detailed approach of our limitations, Selection Sort is the current fastest comparison based sorting algorithm since it equals Bubble Sort and array after the comparisons are made, the remainder is added so Selection Sort in the worst case, but is exceptionally better in the no elements are lost (lines 24 – 27). In the following examples, average case. We intend to show that Merge Sort operates at an using the given input, the division of the array (Figure 3) and average case time complexity faster than ͉ʚͦ͢ʛ. how the array is merged back into a sorted array (Figure 4) are illustrated. 3. APPROACH Inputs – A: array of n elements A large array with an arbitrary order needs to be arranged in an (38 27 43 3 9 82 10 1) ascending or descending order, either lexicographically or Output – array A in ascending order numerically. Merge sort can solve this problem by using two key ideas. The first key idea of merge sort is that a problem can be divided and conquered. The problem can be broken into smaller arrays, and those arrays can be solved. Second, by dividing the array into halves, then dividing those halves by recursively halving them into arrays of single elements, two sorted arrays are merged into one array, as a single element array is already sorted. Refer to the following pseudocode: Input – A: array of n elements Figure 3: Shows the splitting of the input array into single Output – array A sorted in ascending order element arrays. 1. proc mergesort(A: array) 2. var array left, right, result 3. if length(A)<=1 4. return(A) 5. var middle=length(A)/2 6. for each x in A up to middle 7. add x to left 8. for each x in A after middle 9. add x to right 10. left=mergesort(left) 11. right=mergesort(right) 12. result=merge(left,right) 13. return result Figure 1: Pseudocode of the Merge Sort algorithm Figure 4: Shows the merging of the single element arrays during the Merge Step. Input – left:array of m elements, right: array of k elements As the example shows, array A is broken in half continuously Output – array result sorted in ascending order until they are in arrays of only a single element, then those 14. proc merge(left: array, right: array) single elements are merged together until they form a single 15. var array result 16. which length(left) > 0 and length(right) > sorted array in ascending order. 0 17. if first(left) <= first(right) 18. append first(left) to result 4. EVALUATION 19. left=rest(left) 20. else 4.1 Theoretical Analysis 21. append first(right) to result 4.1.1 Evaluation Criteria 22. right=rest(right) All comparison based sorting algorithms count the comparisons 23. end while 24. if length(left) > 0 of array elements as one of their key operations. The Merge Sort 25. append left to result algorithm can be evaluated by measuring the number of 26. if length(right) > 0 comparisons between array elements. As the key operation, we 27. append right to result can measure the number of comparisons made to determine the 28. return result overall efficiency of the algorithm. We intend to show that Figure 2: Pseudocode of the Merge step of Merge Sort because the Merge Sort algorithm makes less comparisons over the currently acknowledged most efficient algorithm, Insertion Sort[Sec 2], Merge Sort is the most efficient comparison sort As the pseudocode shows, after the array is broken up into a left algorithm. half and a right half (lines 5 - 9), the two halves are divided recursively (lines 10 – 11) until they are all within a single element array. Then, the two halves’ elements are compared to determine how the two arrays should be arranged (lines 16 -22). Should any one half contain elements not added to the sorted 4.1.2 Merge Sort Case Scenarios 4.1.2.2 Best Case 4.1.2.1 Worst Case The best case of Merge Sort, depicted in Figure 5, occurs when Merge Sort makes the element comparisons we want to measure the largest element of one array is smaller than any element in the other. In this scenario, only ͢ comparisons of array during the merge step, where pairs of arrays are recursively Ɵ2 merged into a single array.