Home > CS3310: Design and Analysis of Algorithms > Brute ForceBrute Force
Brute Force: Straightforward approach, usually based directly on problem’s statement and definitions of the concepts involved.Example: Examples
Selection Sort: Iterate over array. Every iteration, swap the smallest element with the element in A[i] (4i4 starts at 0). Each iteration, increment i. Go until you’ve covered the whole array (up to n-2, because at that point you have nothing left to swap)
ALGORITHM SelectionSort(A[0..n-1])
// Sorts a given array by selection sort
// Input: An array A[0..n-1] of orderable elements
// Output: Array A[0..n-1] sorted in ascending order
for i \leftarrow 0 to n \text{$-$} 2 do
min \leftarrow i
for j \leftarrow i \text{$+$} 1 to n \text{$-$} 1 do
if A[j] \lt A[min] min \leftarrow j
swap A[i] and A[min]
Analysis is straightforward.
Time: The basic operation is the comparison A[j] < A[min], and number of times it executes depends on array size:
Q: Why not min <- j or swap?
- A: Because both of those execute less often.
C(n) = \sum_{i=0}^{n-2} \sum_{j=i+1}^{n-1} 1 \\ \text{apply Counting summation} \\ = \sum_{i=0}^{n-2} [ (n-1) - (i+1) + 1 ] \\ = \sum_{i=0}^{n-2} ( n - 1 - i ) \\ \text{apply Arithmetic progression} \\ = \frac{(n-1)n}{2}
Thus, it is O(n^2).
Space: O(n)
Compare adjacent elements of the list and exchange them if they’re out of order.
ALGORITHM BubbleSort(A[0..n-1])
for i \leftarrow 0 to n-2 do
for j \leftarrow 0 to n-2-i do
if A[j+1] \lt A[j] swap (A[j], A[j+1])
Basic operation is the comparison
\begin{aligned} C(n) &= \sum_{i=0}^{n-2} \sum_{j=0}^{n-2-i} 1 \\ &= \sum_{i=0}^{n-2} \left[ (n-2-i) - 0 + 1 \right] \\ &= \sum_{i=0}^{n-2} (n-1-i) \\ &= \frac{(n-1)n}{2} \in \Theta(n^2) \end{aligned}
We want to find if some pattern (substring) is in a text (string).
Steps:
Pseudocode:
ALGORITHM BruteForceStringMatch(T[0..n-1], P[0..m-1])
// Implements brute-force string matching
// Input: Array T representing text, array p representing pattern
// Output: Index of first char of matching substring, or -1
for i \leftarrow 0 to n \text{$-$} m do
j \leftarrow 0
while j<m and P[j]=T[i+j] do
j \leftarrow j \text{$+$} 1
if j = m return i
return -1
Analysis: