← Back to Home

CITS2200 - Lecture 4
Merge Sort - Slides

Lower Bound Complexity

  • Lower bound complexity is the absolute minimum amount of time/resources an algorithm can take to solve a problem
    • Lowest time attainable with a perfectly efficient algorithm
    • Denoted with instead of

The lower bound for sorting non-natural/general numbers is:

  • @ You cannot make an algorithm faster than this

Merge-sort achieves this level of complexity

  • Merge sort has complexity used instead of when describing the actual complexity of an algorithm

Quick note: Algorithms are typically shown sorting in ascending order, but it only requires a small change for any algorithm to sort in descending order

  • ! In terms of increasing time:

Divide-and-Conquer

Before we explore merge-sort:

  • Divide-and-conquer is a general algorithm design paradigm
    • Paradigm → pattern or model

Divide-and-conquer is split into three parts:

  1. Divide → divide input data into two disjoint subsets and
    • Disjoint → no elements shared between sets
  2. Recur → solve subproblems associated with and
  3. Conquer → combine solutions for and into solution for

Merge-sort is based on the divide-and-conquer paradigm

Merge-Sort

Takes input sequence with elements:

  1. Divide: partition into two sequences and of elements each
  2. Recur: recursively sort and
  3. Conquer: merge and into a unique sorted sequence

Example of a Python merge and merge-sort algorithm:

def merge(S1, S2, S): # merge two sorted Python lists
    i = j = 0
    while i + j < len(S):
        if j == len(S2) or (i < len(S1) and S1[i] < S2[j]):
            S[i + j] = S1[i]   # copy ith element of S1
            i += 1
        else:
            S[i + j] = S2[j]   # copy jth element of S2
            j += 1
 
def merge_sort(S): #sort list using merge-sort algorithm
    n = len(S)
    if n < 2:
        return  # list is already sorted
 
    # divide
    mid = n // 2
    S1 = S[0:mid]   # first half
    S2 = S[mid:n]   # second half
 
    # conquer (recursion)
    merge_sort(S1)
    merge_sort(S2)
 
    # merge results
    merge(S1, S2, S)

Analysis of Merge-Sort:

  • The height of the merge-sort tree is
    • Each split divides size by 2:
  • Overall work done at the nodes of depth is
    • At depth , there are subarrays of size
  • Thus, total run time of merge-sort is

In algorithms, always means → which is the standard


CITS2200 - Lecture 6