Professional Documents
Culture Documents
Brute Force Algorithm
Brute Force Algorithm
Describes a primitive programming style, one in which the programmer relies on the
computer's processing power instead of using his or her own intelligence to simplify the
problem, often ignoring problems of scale and applying naive methods suited to small
problems directly to large ones. The term can also be used in reference to programming
style: brute-force programs are written in a heavyhanded, tedious way, full of repetition
and devoid of any elegance or useful abstraction
In order to apply brute-force search to a specific class of problems, one must implement
four procedures, first,next, valid, and output. These procedures should take as a
parameter the data P for the particular instance of the problem that is to be solved, and
should do the following:
The brute force algorithm consists in checking, at all positions in the text between 0
and n-m, whether an occurrence of the pattern starts there or not. Then, after each
attempt, it shifts the pattern by exactly one position to the right.
EXAMPLES:
1.
Selection sort can be one of the examples of brute force algorithm...
Algorithm Selectionsort(a[0...n-1])
for i<-0 to n-2 do
min <- i
for j <- i+1 to n-1 do
if A[j]< A[min] min <-j
swap A[i] and A[min]
2.
Progam to check if a given number is a prime number is a brute force
algorithm.
while(p <= n)
{
c=0;
for(i=2; i<=p/2; i++)
{ if(p % i==0) c++; }
if(c == 1) printf("%d ", p);
p++;
}
then fix arr2 and check all possible combination using now arr1 and arr3
For better time complexity you can use binary search instead of linear search