Tutorial 7: Advanced Sorting Algorithms

CAB301 - Algorithms and Complexity

School of Computer Science, Faculty of Science

Agenda

  1. Lecture Recap: Advanced Sorting Algorithms
    • Merge Sort
    • Quick Sort
    • Heap Sort
  2. Tutorial Questions + Q&A
CAB301 - Algorithms and Complexity
School of Computer Science, Faculty of Science

Merge Sort

Divide and Conquer algorithm, relies on a merge operation:

  • How to combine two sorted arrays into a single sorted array?

ALGORITHM
if



alt text

CAB301 - Algorithms and Complexity
School of Computer Science, Faculty of Science

Quick Sort

Divide and Conquer algorithm, relies on a partition operation that, given a pivot, divides the array into two parts:

  • Left part contains elements less than the pivot, and right part greater.

ALGORITHM
if


Partitioning process

alt text

Here, the split position is .

CAB301 - Algorithms and Complexity
School of Computer Science, Faculty of Science

Heap Sort

Heap Sort keeps the array as a max-heap:

  • Complete binary tree
  • Each node is no less than its children.

Repeatedly perform Maximum Key Deletion:

  1. Exchange the root's key with the last key.
  2. Decrease the heap size by 1.
  3. Heapify the complete binary tree.

alt text

alt text

alt text

CAB301 - Algorithms and Complexity
School of Computer Science, Faculty of Science