1 of 27

CSE 373 AU24 Week 3 AX

Recurrences + Effects of Sorting

2 of 27

Announcements

  • Lectures: Tries (tomorrow) - do pre-lecture reading and homework quiz!
  • Project: Deques Analysis due tomorrow!
  • Resubmission Policies Update

3 of 27

Micro-Teach: Recurrences (Tree Method)

4 of 27

Big Idea

Two steps:

  1. Code to Recurrence:

Model recursive code with recurrence

  1. Recurrence to Big-Theta:

Apply Tree Method on the recurrence equation

Goal: Analyze the runtime of some recursive code

5 of 27

What is a Recurrence?

An expression that lets us express the runtime of a function recursively.��

6 of 27

What is a Recurrence?

An expression that lets us express the runtime a function recursively.��

Base Case

Recursive Case

7 of 27

What is a Recurrence?

Base Case

Recursive Case

Recursive Call

8 of 27

Q1 Warm-Up: Model Code with Recurrence

9 of 27

Walkthrough: Understand Code Structure

Base case when the input N <= 1

10 of 27

Walkthrough: Understand Code Structure

Recursive case when N > 1

11 of 27

Walkthrough: Understand Code Structure

Non-recursive work

12 of 27

Walkthrough: Understand Code Structure

Recursive work

13 of 27

Walkthrough: Understand Code Structure

Recursive work

Base case when the input N <= 1

Non-recursive work

T(N) =

Base case

Recursive work + Non-recursive work

Recurrence:

14 of 27

Walkthrough: Code to Recurrence

15 of 27

Recurrence Diagram: Format

RECURSIVE WORK

T(N) = size of current

subproblem

T(N) = size of current subproblem

(in recursive call)

T(N) = size of current

subproblem

(in recursive call)

~ NON-RECURSIVE WORK

(for subproblem)

~ non-recursive work

(for subproblem)

~ non-recursive work

(for subproblem)

T(N)

T(N)

T(N)

T(N)

. . .

. . .

~ non-recursive

~ non-recursive

~ non-recursive

~ non-recursive

16 of 27

17 of 27

18 of 27

19 of 27

Worksheet Q2:

T(N) = 3T(N/3) + N

  • How many branches will each subtree have?
  • What is the recursive and non-recursive work for the second level?

20 of 27

Recurrence relations (solution)

3 * T(N / 3) + N

T(N) =

21 of 27

Case Study #2: Stable Sorting

22 of 27

Scenario: Default vs. Alternative Sorts on Canvas

By default, Canvas sorts students alphabetically by their last name. Help the TA Team brainstorm new criteria to sort by to reduce bias! Develop a metric and provide reasoning for your choice.

Possible Examples:

  • Grades
  • Declining student performance
  • How often the instructor has seen a student’s name on Canvas

23 of 27

Scenario: Stable Sorting with Alternative Criteria

Assume we have chosen your criteria to implement a stable sorting algorithm on Canvas. However, when tied entries are found, the algorithm will revert to sorting alphabetically by last name. This reintroduces biases.

Choose a solution and explain your reasoning:

  • Implement an unstable sort instead
  • Choose different stable sort (specify)
  • Maintain current sorting order

💡Recall: A sort is stable if it preserves the original order of equivalent keys. An unstable sort doesn’t guarantee the original order of elements in the event of a equivalent keys.

24 of 27

Sorting Algorithms & Optimization

The TA Team has an additional request that you determine the most optimal/efficient approach to achieve the desired result. Connect your choice of stability and alternative sorting criteria to the tangible algorithms we have learned.

Worst-case

Best-case

Stable

In-place

Space Complexity

Selection

Θ(N2)

Θ(N2)

Θ(1)

Insertion

Θ(N2)

Θ(N)

Θ(1)

Mergesort

Θ(NlogN)

Θ(NlogN)

Θ(NlogN)

*Θ(N) [w/ timsort]

25 of 27

Design Decisions: Takeaways

In this process, you see that user needs, developer needs, value systems, etc. often are at odds with one another.

As our criteria shifted and assumptions changed, designs we once thought were perfect or functional can fail to adapt, and loopholes remained with regard efficiency, accessibility, and bias mitigation.

We can connect these ideas back to the Duo ADT; sorting with Binary Search to find duplicates in arrays was great, but soon posed problems with resizing for add operations. Continue to challenge course concepts in their applicability for scenarios and their relative efficiencies!

26 of 27

Any Questions?

27 of 27

Closing Announcements

  • Thanks for coming to section! See you tomorrow in class! Stay safe and stay hydrated :)
  • Remember analysis for Deques is due tomorrow night!

Please don't hesitate to reach out if you have any

questions or concerns about the course, or if there's

anything else we can help you with! We're here to support

you however we can!