DSA - Sorting
DSA - Sorting
Sorting
Lecturer: Duc Q. Nguyen
October 3rd, 2023
Motivations
Outline Taxonomies of sorting algorithms
Exchange
Divide-and-conquer
Learning outcomes
● L.O.6.1 - Depict the working steps of sorting algorithms step-by-steps.
● L.O.6.2 - Describe sorting algorithms by using pseudocode.
● L.O.6.3 - Implement sorting algorithms using C/C++ .
● L.O.6.4 - Analyze the complexity and develop experiment (program) to evaluate sorting
algorithms.
● L.O.6.5 - Use sorting algorithms for problems in real-life.
● L.O.8.4 - Develop recursive implementations for methods supplied for the following
structures: list, tree, heap, searching, and graphs.
● L.O.1.2 - Analyze algorithms and use Big-O notation to characterize the computational
complexity of algorithms composed by using the following control structures: sequence,
branching, and iteration (not recursion).
Motivations
● Goal: Given some data points, arrange those data points into
ascending/descending order by some quantity
○ E.g. sort cards by face value or suit
Motivations
● Advantages:
○ Sorted data is often easier to work with
○ Sorted data can allow for faster insert/retrieval/deletion
Motivations
● Questions:
○ What are the different ways we can sort data?
○ What is the “best” strategy?
Taxonomies of sorting algorithms
Sorting
Internal External
Sorted Unsorted
Insertion
● Insertion sort
23 78 45 8 32 56 12 64
23 78 45 8 32 56 12 64
23 78 45 8 32 56 12 64
23 45 78 8 32 56 12 64
8 23 45 78 32 56 12 64
Insertion
● Insertion sort
8 23 32 45 78 56 12 64
8 23 32 45 56 78 12 64
8 12 23 32 45 56 78 64
8 12 23 32 45 56 64 78
Insertion
● Shell sort
23 78 45 8 32 56 12 64
8 32 45 12 64 56 23 78
23 78 45 8 32 56 12 64
12 8 23 56 32 64 45 78
Insertion
● Shell sort
12 8 23 56 32 64 45 78
8 12 23 32 45 56 64 78
Insertion
● Complexity:
○ Insertion sort:
■ f (n) = n(n+1)/2 = O(n2)
○ Shell sort
■ O(n1.25)
Selection
Swap
Sorted Unsorted
Selection
● Selection sort
23 78 45 8 32 56 12 64
8 78 45 23 32 56 12 64
8 12 45 23 32 56 78 64
8 12 23 45 32 56 78 64
8 12 23 32 45 56 78 64
Selection
● Selection sort
8 12 23 32 45 56 78 64
8 12 23 32 45 56 78 64
8 12 23 32 45 56 64 78
8 12 23 32 45 56 64 78
Selection
Unsorted Sorted
Selection
● Heap sort
23 78 45 8 32 56 12 64
23
78 45
8 32 56 12
64
Selection
● Heap sort
78 64 56 23 32 45 12 8
78
64 56
23 32 45 12
8
Selection
● Heap sort
8 64 56 23 32 45 12 78
64 56
23 32 45 12
Selection
● Heap sort
64 32 56 23 8 45 12 78
64
32 56
23 8 45 12
Selection
● Heap sort
12 32 56 23 8 45 64 78
12
32 56
23 8 45
Selection
● Heap sort
56 32 45 23 8 12 64 78
56
32 45
23 8 12
Selection
● Heap sort
12 32 45 23 8 56 64 78
12
32 45
23 8
Selection
● Heap sort
45 32 12 23 8 56 64 78
45
32 12
23 8
Selection
● Heap sort
8 32 12 23 45 56 64 78
32 12
23
Selection
● Heap sort
32 23 12 8 45 56 64 78
32
23 12
8
Selection
● Heap sort
8 23 12 32 45 56 64 78
23 12
Selection
● Heap sort
23 8 12 32 45 56 64 78
23
8 12
Selection
● Heap sort
12 8 23 32 45 56 64 78
8
Selection
● Heap sort
8 12 23 32 45 56 64 78
8
Selection
● Heap sort
8 12 23 32 45 56 64 78
Selection
● Complexity:
○ Selection sort:
■ f (n) = n(n+1)/2 = O(n2)
○ Heap sort:
■ f (n) = O(nlog2n)
Exchange
Sorted Unsorted
Exchange
● Bubble sort
23 78 45 8 32 56 12 64
8 23 78 45 12 32 56 64
8 12 23 78 45 32 56 64
8 12 23 32 78 45 56 64
8 12 23 32 45 78 56 64
Exchange
● Bubble sort
8 12 23 32 45 78 56 64
8 12 23 32 45 56 78 64
8 12 23 32 45 56 64 78
8 12 23 32 45 56 64 78
Exchange
● Complexity:
○ Bubble sort:
■ f (n) = n(n+1)/2 = O(n2)
Divide-and-conquer
● Quick sort
23 78 45 8 32 56 12 64
8 12 23 78 45 32 56 64
8 12 23 78 45 32 56 64
8 12 23 45 32 56 64 78
Divide-and-conquer
● Quick sort
8 12 23 45 32 56 64 78
8 12 23 32 45 56 64 78
8 12 23 32 45 56 64 78
8 12 23 32 45 56 64 78
Divide-and-conquer
● Merge sort
23 78 45 8 32 56 12 64
23 78 45 8 32 56 12 64
23 78 45 8 32 56 12 64
23 78 8 45 32 56 12 64
Divide-and-conquer
● Merge sort
23 78 8 45 32 56 12 64
8 23 45 78 12 32 56 64
8 12 23 32 45 56 64 78
Divide-and-conquer
● Complexity:
○ Quick sort:
■ Average: f (n) = O(nlog2n)
■ Worst: f (n) = O(n2)
○ Merge sort:
■ f (n) = O(nlog2n)