You are on page 1of 6

a) Write down the difference between algorithm and

pseudocode. Write down the necessary steps of


Binary Search Algorithm and Search the 56 in the
following array.
23 29 34 50 53 56 60 68 70

Algorithm vs Pseudocode.
An algorithm is a step by step procedure to solve a
problem. A procedure is a finite sequence of
instructions, where each is carried out in a finite
amount of time. Every problem can be solved with
the help of an algorithm. For example, when the user
wants to login to a Facebook account, first he has to
go to Facebook.com. Then he has to give the correct
username and password. Then he has to click the
login button. If the username and password are
correct, the user can enter his account. Likewise,
every problem has a sequence of steps to solve it.
This is also an algorithm because it provides a correct
sequence of steps to solve the problem. When
writing programs, it is important to identify the
algorithm for the program. For example, to add two
numbers, first sum variable is initialized to 0. Then
two numbers are entered. Then, the addition is
stored to the sum variable. Finally, the sum is
printed. That is the algorithm to add two numbers.
Pseudocode is an informal way of writing a program.
It is not exactly a computer program. It represents
the algorithm of the program in natural language
and mathematical notations. Usually, there is no
particular code syntax to write a pseudocode.
Therefore, there is no strict syntax as a usual
programming language. It uses simple English
language.
An algorithm is an unambiguous specification of how
to solve a problem. Pseudocode is an informal high-
level description of the operating principle of a
computer program or other algorithm.
An algorithm helps to simplify and understand the
problem. On the other hand, pseudocode is a
method of developing an algorithm.
An algorithm is an arrangement of steps to solve a
problem. A pseudo-code uses natural language or
compact mathematical notation to write algorithms.
The main difference between algorithm and
pseudocode is that an algorithm is a step by step
procedure to solve a given problem while a
pseudocode is a method of writing an algorithm.

The necessary steps of Binary Search Algorithm and


Search the 56 in the following array.
Following are the steps of implementation that we will
be following:
1. Start with the middle element:
o If the target value is equal to the middle
element of the array, then return the index of
the middle element.
o If not, then compare the middle element with
the target value,
 If the target value is greater than the
number in the middle index, then pick the
elements to the right of the middle index,
and start with Step 1.
 If the target value is less than the number
in the middle index, then pick the
elements to the left of the middle index,
and start with Step 1.
2. When a match is found, return the index of the
element matched.
3. If no match is found, then return -1

int binarySearch(int values[], int len, int


target)

int max = (len - 1);

int min = 0;

int guess; // this will hold the index of


middle elements

int step = 0; // to find out in how many


steps we completed the search

while(max >= min)


{

guess = (max + min) / 2;

// we made the first guess, incrementing


step by 1

step++;

if(values[guess] == target)

printf("Number of steps required for


search: %d \n", step);

return guess;

else if(values[guess] > target)

// target would be in the left half

max = (guess - 1);

else

// target would be in the right half

min = (guess + 1);

}
}

// We reach here when element is not

// present in array

return -1;

int main(void)

int values[] = {13, 21, 54, 81, 90};

int n = sizeof(values) / sizeof(values[0]);

int target = 81;

int result = binarySearch(values, n, target);

if(result == -1)

printf("Element is not present in the


given array.");

else

printf("Element is present at index: %d",


result);
}

return 0;

You might also like