Download presentation
Presentation is loading. Please wait.
1
Sorting and recurrence analysis techniques
CSE 373: Data Structures and Algorithms Sorting and recurrence analysis techniques Autumn 2018 Shrirang (Shri) Mare Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker Brand, Zora Fung, Stuart Reges, Justin Hsia, Ruth Anderson, and many others for sample slides and materials ...
2
Sorting problem statement
Given n comparable elements, rearrange them in an increasing order. Input An array π΄ that contains n elements Each element has a key π and an associated data Keys support a comparison function (e.g., keys implement a Comparable interface) Expected output An output array π΄ such that for any π and π, π΄[π] β€ π΄[π] if π < π (increasing order) Array π΄ can also have elements in reverse order (decreasing order) CSE 373 AU 18
3
Desired properties in a sorting algorithm
Stable In the output, equal elements (i.e., elements with equal keys) appear in their original order In-place Algorithm uses a constant additional space, π(1) extra space Adaptive Performs better when input is almost sorted or nearly sorted (Likely different big-O for best-case and worst-case) Fast. π (π log π) No algorithm has all of these properties. So choice of algorithm depends on the situation. CSE 373 AU 18
4
Sorting algorithms β High-level view
- π π 2 Insertion sort Selection sort Quick sort (worst) - π(π log π) Merge sort Heap sort Quick sort (avg) - Ξ©(π log π) -- lower bound on comparison sorts - π(π) β non-comparison sorts Bucket sort (avg) CSE 373 AU 18
5
A framework to think about sorting algos
Some questions to consider when analyzing a sorting algorithm. What is the state of the data during each step while sorting Yes/No Yes/No/Can-be Yes/No/Can-be > or < or approx. equal Which data structure is better suited for this algo CSE 373 AU 18
6
Visualization: https://visualgo.net/en/sorting
Insertion sort Idea: At step π, insert the π π‘β element in the correct position among the first π elements. CSE 373 AU 18
7
Visualization: https://visualgo.net/en/sorting
Selection sort Idea: At step π, find the smallest element among the not-yet-sorted elements (π β¦ π) and swap it with the element at π. CSE 373 AU 18
8
Heap sort Idea: CSE 373 AU 18
9
In-place heap sort Idea: 1. Treat initial array as a heap
2. When you call removeMin(), that frees up a slot towards the end in the array. Put the extract min element there. 3. More specifically, when you remove the π π‘β element, put it at π΄ πβπ 4. This gives you a reverse sorted array. But easy to fix in-place. CSE 373 AU 18
10
Design technique: Divide-and-conquer
Very important technique in algorithm to attack problems Three steps: 1. Divide: Split the original problem into smaller parts 2. Conquer: Solve individual parts independently (think recursion) 3. Combine: Put together individual solved parts to produce an overall solution Merge sort and Quick sort are classic examples of sorting algorithms that use this technique CSE 373 AU 18
11
Merge sort To sort a given array,
Visualization: Merge sort To sort a given array, Divide: Split the input array into two halves Conquer: Sort each half independently Combine: Merge the two sorted halves into one sorted whole (HW3 Problem 6!) CSE 373 AU 18
12
Merge sort Split array in the middle Sort the two halves
1 2 3 4 8 57 91 22 Merge sort 1 8 2 1 2 57 91 22 Split array in the middle Sort the two halves Merge them together 8 2 57 1 91 22 91 22 1 22 91 1 2 8 1 2 22 57 91 π π = π if πβ€1 2π π 2 + π 2 π otherwise 1 2 3 4 8 22 57 91
13
Review: Unfolding (technique 1)
π π = π if πβ€1 2π π 2 + π 2 π otherwise CSE 373 AU 18
14
Technique 2: Tree method
Level Number of Nodes at level Work per Node Work per Level 1 2 π base Last recursive level: CSE 373 AU 18
15
Technique 2: Tree method
Level Number of Nodes at level Work per Node Work per Level 1 2 π base Last recursive level: CSE 373 AU 18
16
Technique 2: Tree method
Level Number of Nodes at level Work per Node Work per Level 1 2 π base Last recursive level: CSE 373 AU 18
17
Technique 2: Tree method
Level Number of Nodes at level Work per Node Work per Level 1 2 π base Last recursive level: CSE 373 AU 18
18
Technique 2: Tree method
Level Number of Nodes at level Work per Node Work per Level 1 2 π base Last recursive level: CSE 373 AU 18
19
Technique 2: Tree method
Level Number of Nodes at level Work per Node Work per Level 1 π 2 π 2 4 π 4 π 2 π π 2 π base 2 log2π Last recursive level: log π β1 Combining it all togetherβ¦ π π =π+ π=0 log 2 π β1 π =π+π log π CSE 373 AU 18
20
Tree Method Practice π π = 4 π€βππ πβ€1 3π π 4 +π π 2 ππ‘βπππ€ππ π
Level (i) Number of Nodes Work per Node Work per Level 1 2 π base π π = 4 π€βππ πβ€1 3π π 4 +π π 2 ππ‘βπππ€ππ π Tree Method Practice π π 4 +π π 4 +π π 4 +π π 2 π n 2 π π 4 π n 4 2 π n 4 2 π π 4 π n 4 2 π π 4 π n π n π n π n π n π n π n π n π n β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ 4 4 4 4 4 4 4 4 4 Example provided by CS 161 β Jessica Su
21
Tree Method Practice π π = 4 π€βππ πβ€1 3π π 4 +π π 2 ππ‘βπππ€ππ π
Level (i) Number of Nodes Work per Node Work per Level 1 ππ2 3 π π 4 2 3 16 π π 2 2 9 π π 9 256 π π 2 π 3 π π π 4 π 2 π π π 2 base 3 log4π 4 4β3 log4π π π = 4 π€βππ πβ€1 3π π 4 +π π 2 ππ‘βπππ€ππ π Tree Method Practice π π 4 +π π 4 +π π 4 +π π 2 π n 2 π π 4 π n 4 2 π n 4 2 π π 4 π n 4 2 π π 4 Last recursive level: log 4 π β1 Combining it all togetherβ¦ π n π n π n π n π n π n π n π n π n β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ π π = 4 π log43 + π=0 log 4 π β π π π 2 β¦ β¦ β¦ β¦ 4 4 4 4 4 4 4 4 4 Example provided by CS 161 β Jessica Su
22
Technique 3: Master Theorem
Given a recurrence of the following form: π π = π π€βππ π=1 ππ π π + π π ππ‘βπππ€ππ π Where π, π, and π are constants, then π(π) has the following asymptotic bounds If log π π <π then π π βΞ π π If log π π =π then π π βΞ π π log 2 π If log π π >π then π π βΞ π log π π
23
Apply Master Theorem log π π =π β log 2 2 =1
π π = π π€βππ π=1 ππ π π + π π ππ‘βπππ€ππ π log π π =π π π βΞ π π log 2 π log π π >π π π βΞ π log π π If π π βΞ π π log π π <π then Given a recurrence of the form: a = 2 b = 2 c = 1 d = 1 π π = 1 π€βππ πβ€1 2π π 2 +π ππ‘βπππ€ππ π log π π =π β log 2 2 =1 π π βΞ π π log 2 π βΞ π 1 log 2 π
24
Reflecting on Master Theorem
π π = π π€βππ π=1 ππ π π + π π ππ‘βπππ€ππ π log π π =π π π βΞ π π log 2 π log π π >π π π βΞ π log π π If π π βΞ π π log π π <π then Given a recurrence of the form: The case Recursive case conquers work more quickly than it divides work Most work happens near βtopβ of tree Non recursive work in recursive case dominates growth, nc term Work is equally distributed across call stack (throughout the βtreeβ) Overall work is approximately work at top level x height Recursive case divides work faster than it conquers work Most work happens near βbottomβ of tree Leaf work dominates branch work log π π <π log π π =π βπππβπ‘ β log π π log π π >π πππππβππππ β π π log π π ππππππππ βπ π log π π
25
Recurrence analysis techniques
1. Unfolding method more of a brute force method Tedious but works 2. Tree methods more scratch work but less error prone 3. Master theorem quick, but applicable only to certain type of recurrences does not give a closed form (gives big-Theta) CSE 373 AU 18 β Shri mare
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.