ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Merge Sort
    AlgoDS/Algorithm 2019. 8. 31. 14:45

    1. Overview

    Like QuickSort, Merge Sort is a Divide and Conquer algorithm. It divides input array into two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is a key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one. See the following C implementation for details.

    2. Description

    2.1 Procedure

    MergeSort(arr[], l,  r)
    If r > l
         1. Find the middle point to divide the array into two halves:  
                 middle m = (l+r)/2
         2. Call mergeSort for first half:   
                 Call mergeSort(arr, l, m)
         3. Call mergeSort for second half:
                 Call mergeSort(arr, m+1, r)
         4. Merge the two halves sorted in step 2 and 3:
                 Call merge(arr, l, m, r)

    2.2 Implementation

    # Python program for implementation of MergeSort 
    def mergeSort(arr): 
        if len(arr) >1: 
            mid = len(arr)//2 #Finding the mid of the array 
            L = arr[:mid] # Dividing the array elements  
            R = arr[mid:] # into 2 halves 
      
            mergeSort(L) # Sorting the first half 
            mergeSort(R) # Sorting the second half 
      
            i = j = k = 0
              
            # Copy data to temp arrays L[] and R[] 
            while i < len(L) and j < len(R): 
                if L[i] < R[j]: 
                    arr[k] = L[i] 
                    i+=1
                else: 
                    arr[k] = R[j] 
                    j+=1
                k+=1
              
            # Checking if any element was left 
            while i < len(L): 
                arr[k] = L[i] 
                i+=1
                k+=1
              
            while j < len(R): 
                arr[k] = R[j] 
                j+=1
                k+=1
      
    # Code to print the list 
    def printList(arr): 
        for i in range(len(arr)):         
            print(arr[i],end=" ") 
        print() 
      
    # driver code to test the above code 
    if __name__ == '__main__': 
        arr = [12, 11, 13, 5, 6, 7]  
        print ("Given array is", end="\n")  
        printList(arr) 
        mergeSort(arr) 
        print("Sorted array is: ", end="\n") 
        printList(arr) 
      
    # This code is contributed by Mayank Khanna 

    2.3 Time Complexity

    Particular Complexity
    Best O()
    Worst and Average O()
    Auxiliary Space O(n)
    Auxiliary Space with linked lists O(1)
    Boundary Case Bubble sort takes minimum time (Order of n) when elements are already sorted
    Algorithmic paradigm Divide and Conquer
    Sorting In Place No in a typical implementation
    Stable Yes

    In the case of linked lists, the case is different mainly due to the difference in memory allocation of arrays and linked lists. Unlike arrays, linked list nodes may not be adjacent in memory. Unlike an array, in the linked list, we can insert items in the middle in O(1) extra space and O(1) time. Therefore merge operation of merge sort can be implemented without extra space for linked lists. In arrays, we can do random access as elements are contiguous in memory. Let us say we have an integer (4-byte) array A and let the address of A[0] be x then to access A[i], we can directly access the memory at (x + i*4). Unlike arrays, we can not do random access on the linked list. Quick Sort requires a lot of this kind of access. In the linked list to access i’th index, we have to travel each and every node from the head to i’th node as we don’t have a contiguous block of memory. Therefore, the overhead increases for quicksort. Merge sort accesses data sequentially and the need for random access is low.

    2.4 Application

    • Merge Sort is useful for sorting linked lists in O(n log n) time. 
    • Inversion Count Problem
    • Used in External Sorting

    3. Reference

    https://www.geeksforgeeks.org/merge-sort/

    https://www.geeksforgeeks.org/counting-inversions/

    https://www.geeksforgeeks.org/merge-sort-for-linked-list/

    https://www.youtube.com/watch?v=JSceec-wEyw&feature=emb_title

    'AlgoDS > Algorithm' 카테고리의 다른 글

    Heap Sort  (0) 2020.01.16
    Quicksort  (0) 2020.01.16
    Binary Search  (0) 2020.01.16
    Recursion  (0) 2020.01.16
    Searching and Sorting with Array  (0) 2019.09.04

    댓글

Designed by Tistory.