Professional Documents
Culture Documents
SORTING ALGORITHMS
MOTIVATION
Example:
int iArr[10] = {8, 6, 2, 7, 5, 1, 3, 0, 9, 4};
//How do we generate {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}?
MOTIVATION
APPLICATIONS
‣ Sorting database records
‣ Sorting books
‣ Improving Search Speeds
‣ Finding the closest pair of numbers in a set
‣ Determine if an element is unique
‣ Frequency distribution
‣ Determine k-th largest element in a set
‣ Improving the performance of some Graph algorithms
‣ String processing
‣ Data compression
‣ Scheduling Optimization
‣ Convex Hull problem (Computational Geometry)
‣ …and anything that can benefit from a sorted list of items
REVIEW
BUBBLE SORT
Pseudocode:
repeat
hasChanged := false
repeat with index from 1 to itemCount
if ((item at index - 1) > (item at (index))
swap (item at index - 1) with (item at (index))
hasChanged := true
until hasChanged = false
REVIEW
SELECTION SORT
index item
0 3
1 4
2 1
3 2
SORTING ALGORITHMS
SELECTION SORT
What is the lowest item from
index item
the entire list?
0 3
1 4
2 1
3 2
SORTING ALGORITHMS
SELECTION SORT
What is the lowest item from
index item
the entire list?
0 3
item 1 @ index 2
1 4
2 1
3 2
SORTING ALGORITHMS
SELECTION SORT
What is the lowest item from
index item
the entire list?
0 1
item 1 @ index 2
> Swap index 2 with index 0 1 4
2 3
3 2
SORTING ALGORITHMS
SELECTION SORT
What is the lowest item from
index item
the entire list?
0 1
item 1 @ index 2
> Swap index 2 with index 0 1 4
SELECTION SORT
What is the lowest item from
index item
index 1 to index 3?
0 1
> item 2 @ index 3
1 4
2 3
3 2
SORTING ALGORITHMS
SELECTION SORT
What is the lowest item from
index item
index 1 to index 3?
0 1
> item 2 @ index 3
> Swap with index 1
1 2
> Rinse and repeat until you
2 3
are at the last item
3 4
SORTING ALGORITHMS
SELECTION SORT
See selection_sort.c
SORTING ALGORITHMS
COMPLEXITY ANALYSIS
‣ As terrible as bubble sort
MERGE PSEUDOCODE
function merge(left,right)
var list result
while length(left) > 0 and length(right) > 0
if first(left) ≤ first(right)
append first(left) to result
left = rest(left)
else
append first(right) to result
right = rest(right)
if length(left) > 0
append rest(left) to result
if length(right) > 0
append rest(right) to result
return result
SORTING ALGORITHMS
TIME COMPLEXITY
‣ The function merge() takes at most N operations to
combine the arrays
MERGE SORT
The time complexity of merge sort is significantly better
against the performance of bubble sort and selection sort,
however, that came at the cost of huge additional space
requirements
Can we do better?
SORTING ALGORITHMS
INSERTION SORT
INSERTION SORT
‣ Now, we have a quick and memory efficient algorithm for
ideal and almost ideal cases
‣ Can we do better?
SORTING ALGORITHMS
QUICKSORT
See https://visualgo.net/sorting for animations
SORTING ALGORITHMS
PARTITIONING
18 17 4 27 19 8 22
SORTING ALGORITHMS
PARTITIONING
18 17 4 27 19 8 22
SORTING ALGORITHMS
PARTITIONING
17 18 4 27 19 8 22
SORTING ALGORITHMS
PARTITIONING
17 18 4 27 19 8 22
SORTING ALGORITHMS
PARTITIONING
17 4 18 27 19 8 22
SORTING ALGORITHMS
PARTITIONING
17 4 18 27 19 8 22
SORTING ALGORITHMS
PARTITIONING
17 4 18 27 19 8 22
SORTING ALGORITHMS
PARTITIONING
17 4 18 27 19 8 22
SORTING ALGORITHMS
PARTITIONING
17 4 18 27 19 8 22
SORTING ALGORITHMS
PARTITIONING
17 4 18 27 19 8 22
SORTING ALGORITHMS
PARTITIONING
17 4 8 18 27 19 22
SORTING ALGORITHMS
PARTITIONING
17 4 8 18 27 19 22
SORTING ALGORITHMS
PARTITIONING
17 4 8 18 27 19 22
SORTING ALGORITHMS
PARTITIONING
17 4 8 18 27 19 22
SORTING ALGORITHMS
PARTITIONING
4 8 17 18 19 22 27
SORTING ALGORITHMS
PARTITIONING
4 8 17 18 19 22 27
SORTING ALGORITHMS
PARTITIONING
4 8 17 18 19 22 27
SORTING ALGORITHMS
PARTITIONING
4 8 17 18 19 22 27
SORTING ALGORITHMS
PARTITIONING
4 8 17 18 19 22 27
SORTING ALGORITHMS
PARTITIONING
4 8 17 18 19 22 27
SORTING ALGORITHMS
PARTITIONING
4 8 17 18 19 22 27
SORTING ALGORITHMS
QUICKSORT CONCEPT
1. Pick a pivot (simplest pivot is the first element of the array)
4. You will now have two subarrays, pick a pivot for each
subarray and determine their array sizes
5. Recursion!
SORTING ALGORITHMS
QUICKSORT IN C++
See quick_sort.cpp
SORTING ALGORITHMS
SUMMARY
Sorting Average Time Worst Time Space
Algorithm Complexity Complexity Complexity
Bubble Sort Θ(N2) or Θ(N) O(N2) O(1)
Ω(N) if array is
Insertion Sort O(N2) O(1)
almost sorted
Θ(N*log(N)) O(N*log(N))
Quick Sort O(N2)
amortized or O(1)
SEE YOU
THURSDAY :D