Understand the definition of the brute force design pattern.
Describe several algorithms that are classified as brute force design.
Discuss the strengths and weaknesses of brute force design.
Intro
You should read chapter 3.
We will begin to study design approaches.
How can we solve problems?
How can we design algorithms for our solution?
Brute force design is a is a straight forward approach to solving a problem, usually directly based on the problem statement and definitions of the concepts involved. (From the book)
The author classifies Selection Sort as a brute force sort.
And the string match we saw in chapter 1.
And the algorithms Dr. Meyer presented as well.
Brute force design
Is usually not the most efficient solution.
The design methodology can be applied to almost every problem.
But is sometimes "good enough".
It can provide insight into the problem
Analyzing brute force solutions can help us understand the problem better.
And can help us understand what is expensive in the brute force solution
And can provide a base line check for other algorithms.
Bubble Sort
A brute force sort.
BUBBLE_SORT(A)
for i ← 0 to n-2 do
for j ← 0 to n-2-i do
if A[j+1] > A[j] then
swap(A[j+1],A[j])
return
Questions?
Trace with 8 2 4 1 9 3
Can we do better?
Notice
After the last swap occurs, the remainder of the array is in order
If no swaps occur, then the array is in order.
If either of these conditions are met, we can stop early.
BETTER_BUBBLE(A)
n ← A.size()-1
while n > 0 do
newN ← 0
for i ← 0 to n-1 do
if A[j+1] > A[j] then
swap(A[j+1],A[j])
newN ← i
n ← newN
return
Same questions, but IS IT BETTER?
Bubble Sort can be effective if there are a few items, just one place out of order.