Computer Science

Merge Sort

Merge Sort is a sorting algorithm that uses a divide-and-conquer approach to sort an array of elements. It divides the array into two halves, sorts them separately, and then merges them back together in a sorted manner. It has a time complexity of O(n log n) and is widely used in computer science.

Written by Perlego with AI-assistance

10 Key excerpts on "Merge Sort"

  • The Complete Coding Interview Guide in Java
    eBook - ePub

    The Complete Coding Interview Guide in Java

    An effective guide for aspiring Java developers to ace their programming interviews

    Now, let's discuss the Merge Sort algorithm. The time complexity cases are as follows: best case O(n log n), average case O(n log n), worst case O(n log n). The space complexity may vary, depending on the chosen data structures (it can be O(n)).
    The Merge Sort algorithm is a recursive algorithm based on the famous divide and conquer strategy. Considering that you've been given an unsorted array, applying the Merge Sort algorithm requires you to continually split the array in half until we obtain empty sub-arrays or sub-arrays that contains a single element (this is divide and conquer ). If a sub-array is empty or contains one element, it is sorted by its definition – this is the recursion base case .
    If we haven't reached the base case yet, we divide both these sub-arrays again and attempt to sort them. So, if the array contains more than one element, we split it and we recursively invoke the sort operation on both sub-arrays. The following diagram shows the splitting process for the 52, 28, 91, 19, 76, 33, 43, 57, 20 array:
    Figure 14.2 – Splitting the given array in the Merge Sort algorithm
    Once the splitting is done, we call the fundamental operation of this algorithm: the merge operation (also known as the combine operation). Merging is the operation of taking two smaller sorted sub-arrays and combining them into a single, sorted, new sub-array. This is done until the entire given array is sorted. The following diagram shows the merging operation for our array:
    Figure 14.3 – Merging operation for Merge Sort
    The following code implements the Merge Sort algorithm. The flow begins from the sort() method. Here, we begin by asking the base case question. If the size of the array is greater than 1, then we call the leftHalf() and rightHalf() methods, which will split the given array into two sub-arrays. The rest of the code from sort() is responsible for calling the merge()
  • Beginning Java Data Structures and Algorithms
    eBook - ePub

    Beginning Java Data Structures and Algorithms

    Sharpen your problem solving skills by learning core computer science concepts in a pain-free manner

    O(n log n) .
    Passage contains an image

    Using Merge Sort

    Although the quicksort on average is pretty fast, it still has the theoretical worst time complexity of O(n²) . In this section, we shall examine another sorting algorithm, called Merge Sort , in which the worst time complexity is O(n log n) . Similar to quick sort, Merge Sort belongs to the divide and conquer class of algorithms.
    Merge Sort can be summarized in three simple steps as follows: 
    1. Split the array in the middle
    2. Recursively sort each part separately
    3. Merge the two sorted parts together
    In the following section, we will develop the preceding steps gradually, at each turn slowly building our understanding of how Merge Sorting works.
    Although Merge Sort is theoretically faster than quick sort, in practice, some implementations of quick sort can be more efficient than Merge Sort. Additionally, the Merge Sort uses about O(n) memory as opposed to quick sort, which is O(log n) .
    Passage contains an image

    Dividing the Problem

    In the preceding section, we saw how we can use a recursive technique to split the problem into smaller multiple ones until the solution becomes easy to solve. Merge Sort uses the same approach. The base case for our recursive technique is the same as quick sort. This is when the array is only one element long. When the array to sort only contains one item, the array is already sorted.
    Figure 2.3
  • Data Structures and Algorithm Analysis in C++, Third Edition
    A natural approach to problem solving is divide and conquer. In terms of sorting, we might consider breaking the list to be sorted into pieces, process the pieces, and then put them back together somehow. A simple way to do this would be to split the list in half, sort the halves, and then merge the sorted halves together. This is the idea behind Mergesort.
    Figure 7.8 An illustration of Mergesort. The first row shows eight numbers that are to be sorted. Mergesort will recursively subdivide the list into sublists of one element each, then recombine the sublists. The second row shows the four sublists of size 2 created by the first merging pass. The third row shows the two sublists of size 4 created by the next merging pass on the sublists of row 2. The last row shows the final sorted list created by merging the two sublists of row 3.
    Mergesort is one of the simplest sorting algorithms conceptually, and has good performance both in the asymptotic sense and in empirical running time. Surprisingly, even though it is based on a simple concept, it is relatively difficult to implement in practice. Figure 7.8 illustrates Mergesort. A pseudocode sketch of Mergesort is as follows:
    Before discussing how to implement Mergesort, we will first examine the merge function. Merging two sorted sublists is quite simple. Function merge examines the first element of each sublist and picks the smaller value as the smallest element overall. This smaller value is removed from its sublist and placed into the output list. Merging continues in this way, comparing the front elements of the sublists and continually appending the smaller to the output list until no more input elements remain.
    Implementing Mergesort presents a number of technical difficulties. The first decision is how to represent the lists. Mergesort lends itself well to sorting a singly linked list because merging does not require random access to the list elements. Thus, Mergesort is the method of choice when the input is in the form of a linked list. Implementing merge
  • Data structures based on non-linear relations and data processing methods
    • Xingni Zhou, Zhiyuan Ren, Yanzhuo Ma, Kai Fan, Xiang Ji(Authors)
    • 2020(Publication Date)
    • De Gruyter
      (Publisher)
    n.
    We can see that there need to be
    log 2
    n sortings in total, each of which has a time complexity of O(n). Therefore, the total time complexity of this algorithm is O(n
    log 2
    n), but this algorithm requires O(n) auxiliary memory space.
    Characteristics of Merge Sort: repeatedly invoke one run of Merge Sort algorithm; stable.
    Algorithm efficiency: complexity O(n
    log 2
    n).
    The best, worst and average time complexities of Merge Sort are all O(n
    log 2
    n), while its space complexity is O(n). Therefore, Merge Sort requires relatively much memory, but it is a relatively efficient sorting algorithm.
    Using recursion to implement Merge Sort is concise in its implementation, but since it requires huge memory, it is often impractical. There are also nonrecursive implementation forms for Merge Sort. Interested readers can refer to the related references by themselves. Compared with quick sort and heap sort, the biggest characteristic of Merge Sort is that it is a stable sorting method.

    3.6  Distribution sort

    Different from the sorting algorithms mentioned earlier, distribution sort is a sorting method that trades space for time. Its basic idea is that, during the sorting process, it is unnecessary to compare the keywords. It instead utilizes additional space to carry out “distribution” and “collection” in order to realize the sorting. When the additional space is relatively large, the time complexity of distribution sort can reach linear O(n). Simply speaking, distribution sort trades space for time; therefore, its time performance increases drastically compared with sorting methods based on comparison.
    Common distribution sort algorithms include counting sort, bucket sort and radix sort. This section describes about bucket sort and radix sort.
  • Learning JavaScript Data Structures and Algorithms
    Next, we will add every remaining value from the left array ({9}) to the merged result array and will do the same for the remaining values from the right array. At the end, we will return a merged array.
    If we execute the mergeSort function, this is how it will be executed:
    Note that first the algorithm splits the original array until it has smaller arrays with a single element, and then it starts merging. While merging, it does the sorting as well until we have the original array completely back together and sorted.
    Passage contains an image

    The quick sort

    The quick sort is probably the most used sorting algorithm. It has a complexity of O(n log n) , and it usually performs better than other O(n log n) sorting algorithms. Similarly to the Merge Sort, it also uses the divide-and-conquer approach, dividing the original array into smaller ones (but without splitting them as the Merge Sort does) to do the sorting.
    The quick sort algorithm is a little bit more complex than the other ones you have learned so far. Let's learn it step by step, as follows:
    1. First, we need to select a value from the array called pivot , which will be the value at the middle of the array.
    1. We will create two pointers (references)—the left-hand side one will point to the first value of the array, and the right-hand side one will point to the last value of the array. We will move the left pointer until we find a value that is bigger than the pivot, and we will also move the right pointer until we find a value that is less than the pivot and swap them. We will repeat this process until the left-hand side pointer passes the right-hand side pointer. This process helps to have values lower than the pivot reference before the pivot and values greater than the pivot after the pivot reference. This is called the partition
  • 50 Algorithms Every Programmer Should Know
    eBook - ePub

    50 Algorithms Every Programmer Should Know

    An unbeatable arsenal of algorithmic solutions for real-world problems

    For petite datasets, especially those already in order, it’s usually overkill to deploy a sophisticated algorithm. While an algorithm like Merge Sort is undeniably powerful, its complexities might overshadow its benefits for small data.
    Real-life example : Imagine sorting a handful of books on a shelf by their authors’ last names. It’s simpler and quicker to just scan through and rearrange them manually (akin to a bubble sort) than to employ a detailed sorting method.
    Partially sorted data When dealing with data that’s already somewhat organized, algorithms like insertion sort shine. They capitalize on the existing order, enhancing efficiency.
    Real-life example : Consider a classroom scenario. If students line up by height but a few are slightly out of place, the teacher can easily spot and adjust these minor discrepancies (mimicking insertion sort), rather than reordering the entire line.
    Large datasets For extensive data, where the sheer volume can be overwhelming, Merge Sort proves to be a reliable ally. Its divide-and-conquer strategy efficiently tackles big lists, making it an industry favorite.
    Real-life example : Think about a massive library that receives thousands of books. Sorting these by publication date or author necessitates a systematic approach. Here, a method like Merge Sort, which breaks down the task into manageable chunks, is invaluable.

    Introduction to searching algorithms

    At the heart of many computational tasks lies a fundamental need: locating specific data within complex structures. On the surface, the most straightforward approach might be to scan every single data point until you find your target. But, as we can imagine, this method loses its sheen as the volume of data swells.
    Why is searching so critical? Whether it’s a user querying a database, a system accessing files, or an application fetching specific data, efficient searching determines the speed and responsiveness of these operations. Without adept searching techniques, systems can become sluggish, especially with burgeoning datasets.
  • PHP 7 Data Structures and Algorithms
    If we pass the array by reference, then we do not have to return the array. The passed array will be modified inside the function. It is down to choice how we want to achieve the sorting. Passage contains an image

    Complexity of insertion sort

    Insertion sort has a complexity similar to bubble sort. The basic difference with bubble sort is that the number of swapping is much lower than bubble sort. Here is the complexity for insertion sort:
    Best time complexity Ω(n)
    Worst time complexity
    O(n2 )
    Average time complexity
    Θ(n2 )
    Space complexity (worst case) O(1)
     
    Passage contains an image

    Understanding divide-and-conquer technique for sorting

    So far, we have explored the sorting option with a full list of numbers. As a result, we had a big list of numbers to compare every time. This can be solved if we can somehow make the list smaller. The divide-and-conquer method can be very helpful for us. With this method, we will divide a problem into two or more subproblems or sets, and then solve the smaller problems before combining all those results from the subproblems to get the final result. This is what is known as divide-and-conquer.
    The divide-and-conquer method can allow us to solve our sorting problems efficiently and reduce the complexity of our algorithm. Two of the most popular sorting algorithms are Merge Sort and quick sort, which apply the divide-and-conquer algorithm to sort a list of items, and hence, they are considered to be the best sorting algorithms. Now, we will explore these two algorithms in the next section.
    Passage contains an image

    Understanding Merge Sort

    As we already know that Merge Sort applies the divide-and-conquer approach to solve the sorting problem, we need to find out two processes to address the issue. The first one is to divide the problem set into smaller enough problems to solve easily, and then combine those results. We will apply a recursive approach here for the divide-and-conquer part. The following image shows how to take the approach for divide-and-conquer. We will now consider a smaller list of numbers 20 , 45 93 67 97 52 88 33
  • Algorithms: Design Techniques And Analysis (Revised Edition)
    eBook - ePub

    Algorithms: Design Techniques And Analysis (Revised Edition)

    Design Techniques and Analysis(Revised Edition)

    • M H Alsuwaiyel(Author)
    • 2016(Publication Date)
    • WSPC
      (Publisher)
    MERGESORT with that of bottomupsort. It takes much more time to debug the latter, or even to comprehend the idea behind it. This suggests that a designer of an algorithm might be better off starting with an outline of a recursive description, if possible, which may afterwards be refined and converted into an iterative algorithm. Note that this is always possible, as every recursive algorithm can be converted into an iterative algorithm which functions in exactly the same way on every instance of the problem. In general, the divide-and-conquer paradigm consists of the following steps.
    (a)  The divide step. In this step of the algorithm, the input is partitioned into p ≥ 1 parts, each of size strictly less than n , the size of the original instance. The most common value of p is 2, although other small constants greater than 2 are not uncommon. We have already seen an example of the case when p = 2, i.e., Algorithm MERGESORT . If p = 1 as in Algorithm BINARYSEARCHREC , then part of the input is discarded and the algorithm recurses on the remaining part. This case is equivalent to saying that the input data is divided into two parts, where one part is discarded; note that the some work must be done in order to discard some of the elements. p may also be as high as log n , or even n , where is some constant, 0 < < 1.
    (b)  The conquer step. This step consists of performing p recursive call(s) if the problem size is greater than some predefined threshold n 0 . This threshold is derived by mathematical analysis of the algorithm. Once it is found, it can be increased by any constant amount without affecting the time complexity of the algorithm. In Algorithm MERGESORT , although n 0 = 1, it can be set to any positive constant without affecting the Θ(n log n ) time complexity. This is because the time complexity, by definition, is concerned with the behavior of the algorithm when n approaches infinity. For example, we can modify mergesort so that when n ≤ 16, the algorithm uses a straightforward (iterative) sorting algorithm, e.g., insertionsort. We can increase it to a much larger value, say 1000. However, after some point, the behavior of the algorithm starts to degrade. An (approximation to the) optimal threshold may be found empirically by fine-tuning its value until the desired constant is found. It should be emphasized, however, that in some algorithms, the threshold may not be as low as 1, it must be greater than some constant that is usually found by a careful analysis of the algorithm. An example of this is the median finding algorithm which will be introduced in Sec. 5.5
  • AP Computer Science A Premium, 2024: 6 Practice Tests + Comprehensive Review + Online Practice
    a[n-1] is sorted, both parts in increasing order. Example:
    The main disadvantage of Merge Sort is that it uses a temporary array.
    In this case, a[0] a[3] and a[4] a[5] are the two sorted pieces. The method call merge(a,0,3,5) should produce the “merged” array:
    The middle numerical parameter in merge (the 3 in this case) represents the index of the last element in the first “piece” of the array. The first and third numerical parameters are the lowest and highest indexes, respectively, of array a .
    Here’s what happens in Merge Sort:
    1.Start with an unsorted list of n elements.
    2.The recursive calls break the list into n sublists, each of length 1. Note that these n arrays, each containing just one element, are sorted!
    3.Recursively merge adjacent pairs of lists. There are then approximately n/2 lists of length 2; then, approximately n/4 lists of approximate length 4, and so on, until there is just one list of length n.
    An example of Merge Sort follows: Analysis of Merge Sort:
    1.The major disadvantage of Merge Sort is that it needs a temporary array that is as large as the original array to be sorted. This could be a problem if space is a factor.
    2.Merge Sort is not affected by the initial ordering of the elements. Thus, best, worst, and average cases have similar run times.

    Quicksort

    For large n
  • Interactive Problem Solving Using Logo
    • Heinz-Dieter Boecker, Hal Eden, Gerhard Fischer(Authors)
    • 2014(Publication Date)
    • Routledge
      (Publisher)
    merging , where two lists that are already sorted are combined, resulting in a single, sorted list. In each step of the process the smallest first element of the two lists (and therefore the smallest element of the two lists altogether, since they are in order) is taken as the next element of the combined list. This continues on until both of the initial lists are exhausted. Graphically:
    Figure 18-10 Merging Two Ordered Lists

    Examples of Merge Sort Programs

    The central procedure for the next two methods is Merge.
    Merge expects its two inputs to be lists that are already in nondescending order. These two inputs are combined into one list by Merge. To build sorting methods based on this procedure, it is necessary to provide it with sublists that have already been sorted by other procedures. Let’s discuss a couple of different possible solutions.

    Merging Method 1

    One possible solution that can be based on the basic idea of merging is represented by the following procedures:
    The procedure MergeSort 1 divides the list into two parts: The first part is composed of any elements of the list that are already in the correct order (this may be just one element), and the second part is the rest of the list (from the point where the elements were out of order). After the second part is sorted by a recursive call to MergeSort 1, the two parts are merged back together to obtain the desired result.
    Examples:
    ? Print MergeSort 1 [7 3 4 8 9 5 2]2 3 4 5 7 8 9 ? Print MergeSort 1 [9 8 7 6 5 4 3 2 1]1 2 3 4 5 6 7 8 9
    With tracing turned on for MergeSort and Merge:
    If MergeSort 1 is used on a list of descending numbers, then this method operates the same way that InsertSort does as both of the following traces show:
    The Insertion of one element into a list corresponds to merging a singleelement list with a multiple-element list.

    Merging Method 2

    A somewhat different solution based on Merge is as follows:
    This method divides the list into two parts, recursively sorts these two parts, then merges the result back together. This recursive process proceeds until the lists to be merged contain only one element and the result can be constructed from “bottom up.” The other procedures that are needed look like this:
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.