You are on page 1of 6

Brute force method

 INTRODUCTION:
Brute force is a straightforward approach to solving a problem usually directly based on problem
statement and definitions of the concepts involved. Brute force is applicable to a wide variety of problems.
A brute-force algorithm can serve an important theoretical or educational purpose to judge a more
efficient alternative for solving a problem.
Brute force is a straightforward approach to solve a problem based on the problem’s statement and
definitions of the concepts involved. It is considered as one of the easiest approach to apply and is useful for
solving small size instances of a problem. Brute-force search is typically used when the problem size is
limited, or when there are problem-specific heuristics that can be used to reduce the set of candidate
solutions to a manageable size. The method is also used when the simplicity of implementation is more
important than speed. Brute force algorithms also have non-security application.
Example:
We use all possible solutions for solving a problem i.e. to arrange some numbers, we use all sorting
method:
Bubble sort, selection sort, linear sort, insertion sort etc
Brute Force Search and Sort:
1. SEARCHING ALGORITHM.
 Sequential Search O(n).
2. SORTING ALGORITHM.
 Selection Sort O(n2).
 Bubble Sort O(n2).

 Sequential search:
The algorithm simply compares successive elements of a given list with a given search key
until either a match is found or the list is exhausted without finding a match.

ALGORITHM:

Algorithm Sequential Search (A [0...n], K)


A[n] ← K
i←0
While A [i] ≠ K do
i←i+1
If i < n return i
else return -1
The complexity of a sequential search algorithm is Θ (n) in the worst possible case and Θ (1) in the best
possible case, depending on where the desired element is situated.

SORTING ALGORITHM:
A sorting algorithm is an algorithm made up of a series of instructions that takes an array as input,
performs specified operations on the array, sometimes called a list, and. outputs a sorted array. Sorting
algorithms are often taught early in computer science classes as they provide a straightforward way to
introduce other key computer science topics like Big o notation, divide and conquer methods
 Selection sort:
By scanning the entire list to find its smallest element and exchange it with the first element, putting the
smallest element in its final position in the sorted list.
• Then scan the list with the second element, to find the smallest among the last n-1 elements and exchange
it with second element.
• Continue this process till the n-2 elements. Generally, in the its pass through the list, numbered 0 to n-2,
the algorithm searches for the smallest item among the last n-i elements and swaps it with Ai.
• A0 ≤ A1 ≤ ……….. ≤ Ai-1 Ai, ………… Amin , ………. An-1 in their final positions the last n-i elements
After n-1 passes the list is sorted

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 nondecreasing order
• 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]

 Bubble sort:
It is based on the comparisons of adjacent elements and exchanging it. This procedure is done repeatedly
and ends up with placing the largest element to the last position. The second pass bubbles up the second
largest element and so on after n-1 passes, the list is sorted. Pass i(0≤ i ≤ n-2) of bubble sort can be
represented as:
• A0, . . . , Aj?↔Aj+1, . . . , An−i−1 | An−i≤ . . . ≤ An−1in their final positions

ALGORITHM:
//Sorts a given array by bubble sort
//Input: An array A[0..n − 1] of orderable elements
//Output: Array A[0..n − 1] sorted in nondecreasing order
for i ←0 to n − 2 do for j ←0 to n − 2 − i do if A[j + 1]<A[j ] swap A[j ] and A[j + 1]

ALGORITHM FOR SORTING:


CPP CODE:
#include <iostream>
using namespace std;
class node
{
public:
int rollno;
string name;
node *NEXT;

node ()
{ rollno=0;
NEXT=NULL;
}
};
class student
{
public:
node *HEAD = NULL;
void Create(int rollno,string name);
void Display();
void Sort();
};
int main()
{
student list1;
int rollno,n=0;
string name;
do {
cout << "Enter roll no\n ";
cin >>rollno;
cout << "Enter name\n";
cin >>name;
list1.Create(rollno,name);
cout << "\npress 1 to add another\npress 0 to sort\n";
cin>>n;
}
while(n==1);
cout << "\nDisplay before sorting.\n";
list1.Display();
list1.Sort();
cout << "\nDisplay after sorting.\n";
list1.Display();
}
void student:: Sort()
{
node *h = HEAD, *i, *j, *next_i;
for(i = h; i!=NULL && i->NEXT!=NULL; i=i->NEXT)
{
node *min;
min = i;
for(j = i->NEXT; j!=NULL ; j=j->NEXT)
{
if(j->rollno < min->rollno)
min=j;
}
if(min!=i)
{
int temp;
temp = min->rollno;
min->rollno = i->rollno;
i->rollno = temp;
}
}
HEAD = h;
}
void student:: Display()
{
node *current;
current = HEAD;
cout <<" "<<"BACK" <<" "<< "NUMBER" <<" " << "NEXT" << endl;
while(current != NULL)
{
cout <<" "<< current <<" "<< current->rollno <<" "<< current->NEXT << endl;
current = current->NEXT;
}
system("pause>0");
}
void student:: Create(int rollno,string name)
{
node *front, *tail;
tail = new node;
tail->rollno = rollno;
tail->name = name;
tail->NEXT = NULL;
if(HEAD == NULL)
HEAD = tail;
else
{
front = HEAD;
while(front->NEXT!=NULL)
front = front->NEXT;
front->NEXT = tail;
}
}
Output:

You might also like