Brute Force Applications Sorting Strung Match
Brute Force Applications Sorting Strung Match
Brute force is a straightforward approach to solving a problem, usually directly based on the
problem statement and definition...(Levitin 2007)
3. If the problem is only infrequently solved then the expense of developing a better
algorithm is not justified.
4. The brute force algorithm may be good for small problem size.
Selection sort
Based on sequentially finding the smallest elements
Algorithm SelectionSort(A[0...n-1])
for i ← 0 to n-2 do
min ← i
4. The count
5. Solve
Bubble Sort
Based on consecutive swapping adjacent pairs. This causes a slow migration of the smallest
elements to the left of the array.
Algorithm BubbleSort(A[0...n-1])
for i ← 0 to n-2 do
for j ← 0 to n-2-i do
Number of Comparisons
The algorithm can be improved. If no swaps are made through one cycle of the outer loop
then the array is sorted. Then best case is linear.
Sequential Searching
Appending the search key to the end of the list guarantees the search will be successful, so we
can use the whole loop.
Algorithm SequentialSearch2(A[0...n], K)
A[n] ← K
while A[i] ≠ K do
i++
else return -1
Another improvement is to use a sorted array A[0...n-1], how? What is the worst and best
case?
for i ← 0 to n-m do
j←0
j++
if j = m then return i
return -1