graph TD A[Unsorted Array] --> B[Choose Pivot] B --> C[Partition] C --> D[Elements ≤ pivot] C --> E[Elements > pivot] D --> F[Recursively sort left] E --> G[Recursively sort right]
CS 351 - Data Structures and Algorithms
Part 1: QuickSort Deep Dive
Part 2: Evaluating AI-Generated Algorithms
Understanding the heart of QuickSort: partitioning
graph TD A[Unsorted Array] --> B[Choose Pivot] B --> C[Partition] C --> D[Elements ≤ pivot] C --> E[Elements > pivot] D --> F[Recursively sort left] E --> G[Recursively sort right]
Question for Discussion:
What makes a good partitioning algorithm?
Think about:
Invented by: Nico Lomuto (1990s)
Key Idea: Single pointer scanning from left to right
Characteristics:
Invented by: Tony Hoare (1961) - Original QuickSort creator
Key Idea: Two pointers moving toward each other
Characteristics:
Aspect | Lomuto | Hoare |
---|---|---|
Simplicity | High | Medium |
Swaps (average) | More | Fewer |
Pivot placement | Exact final position | Approximate |
Cache performance | Good | Better |
Edge cases | Easier to handle | More complex |
Activity 1: Algorithm Visualization
Visit: https://courses.lpcordova.com/cs351/algoviz/quicksort.html
Your Mission:
Try these scenarios:
[1, 2, 3, 4, 5, 6, 7, 8]
[8, 7, 6, 5, 4, 3, 2, 1]
[3, 7, 1, 9, 4, 6, 8, 2]
[5, 3, 5, 1, 5, 7, 5]
Discussion Questions:
Group Discussion: What patterns did you notice?
Specific observations to consider:
Performance Characteristics:
graph LR A[Input Type] --> B{Partitioning Quality} B -->|Good| C["O(n log n)"] B -->|Poor| D["O(n²)"] E[Pivot Selection] --> B F[Data Distribution] --> B
Real-world implications:
The New Reality: AI can generate algorithm code instantly, but can we trust it?
Objectives:
Scenario: You ask an AI to implement Merge Sort
Your prompt: “Write a merge sort algorithm in Python”
Let’s see what we might get and how to evaluate it…
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
return merge(left, right)
def merge(left, right):
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
The TRACE Method for Algorithm Evaluation:
Time Complexity Analysis
Readability and Structure
Accuracy and Correctness
Corner Cases and Edge Handling
Efficiency and Space Usage
Questions to ask:
For our Merge Sort: - Expected: O(n log n) - Implementation: ✓ Correct recursive structure - Hidden issue: Array slicing creates copies (Python-specific)
Code Quality Checklist:
Our example assessment:
Testing Strategy:
Critical scenarios to test:
Space Complexity Analysis:
graph TD A[Merge Sort Space] --> B[Recursive Call Stack] A --> C[Temporary Arrays] B --> D["O(log n)"] C --> E["O(n)"] D --> F["Total: O(n)"] E --> F
Python-specific considerations:
Activity 2: Crafting Better Prompts
Basic Prompt: “Write a heap sort algorithm”
Enhanced Prompt: “Write an in-place heap sort algorithm in Python with the following requirements:
Your turn: Work in pairs to improve this prompt for bubble sort
The CLEAR Framework:
Context - Provide background and constraints
Length - Specify desired code length/complexity
Examples - Include input/output examples
Analysis - Request complexity analysis
Requirements - List specific technical requirements
Context: “I’m implementing sorting algorithms for a computer science course”
Length: “Write a concise but well-documented implementation”
Examples: “Should sort [64, 34, 25, 12, 22, 11, 90] to [11, 12, 22, 25, 34, 64, 90]”
Analysis: “Include time and space complexity in comments”
Requirements: “Use Python, include error handling, optimize for readability”
Watch out for:
Activity 3: Code Review Challenge
I’ll show you an AI-generated selection sort. Use the TRACE method to evaluate it:
Work in groups:
DO:
DON’T:
graph TD A[Define Problem] --> B[Craft Detailed Prompt] B --> C[Get AI Response] C --> D[Apply TRACE Method] D --> E{Acceptable?} E -->|No| F[Refine Prompt] E -->|Yes| G[Test Thoroughly] F --> C G --> H[Optimize if Needed] H --> I[Document and Deploy]
QuickSort Insights:
AI Algorithm Evaluation:
For your QuickSort assignment:
Questions to ponder: