You are on page 1of 8

Data Structure and Algorithm

MIDTERM

ᕦ(ò_óˇ)ᕤ

Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is a
"text-based" detail (algorithmic) design tool.

Pseudocode is a language for describing algorithms that allows the algorithm designer to focus on the logic of the
algorithm without being distracted by details of programming language syntax.

The rules of Pseudocode are reasonably straightforward. All statements showing "dependency" are to be
indented. These include while, do, for, if, switch.

Pseudo code is a term which is often used in programming and algorithm based fields. It is a methodology that
allows the programmer to represent the implementation of an algorithm.

Simply, we can say that it’s the cooked up representation of an algorithm.

Often at times, algorithms are represented with the help of pseudo codes as they can be interpreted by
programmers no matter what their programming background or knowledge is.

Pseudo code, as the name suggests, is a false code or a representation of code which can be understood by even
a layman with some school level programming knowledge.

Algorithm: It’s an organized logical sequence of the actions or the approach towards a particular problem. A
programmer implements an algorithm to solve a problem. Algorithms are expressed using natural verbal but
somewhat technical annotations.

Pseudo code: It’s simply an implementation of an algorithm in the form of annotations and informative text
written in plain English. It has no syntax like any of the programming language and thus can’t be compiled or
interpreted by the computer.

Advantages of Pseudocode

 Improves the readability of any approach. It’s one of the best approaches to start implementation of an algorithm.
 Acts as a bridge between the program and the algorithm or flowchart. Also works as a rough documentation, so
the program of one developer can be understood easily when a pseudo code is written out. In industries, the
approach of documentation is essential. And that’s where a pseudo-code proves vital.
 The main goal of a pseudo code is to explain what exactly each line of a program should do, hence making the
code construction phase easier for the programmer.

How Do I Write Pseudocode?

Writing pseudocode is pretty easy actually:

 Start with the algorithm you are using, and phrase it using words that are easily transcribed into computer
instructions.
 Indent when you are enclosing instructions within a loop or a conditional clause. A loop is a set of instructions
that is repeated. A conditional clause is formed by a comparison and what to do if that comparison succeeds or
fails. This technique makes sure it's easy to read.
 Avoid words associated with a certain kind of computer language.

It turns out that there are some fairly standard words you can use. These include standard looping structures like
the following:

 FOR … ENDFOR
 WHILE…ENDWHILE

There are also some terms for standard conditional clauses:

 IF … ENDIF
 WHILE … ENDWHILE (this is both a loop and a conditional clause by the way)
 CASE … ENDCASE

There are more, but that's enough for us to present some examples.

Examples of the Pseudocode

For our first example, we will pretend we have a square game board with one or more bombs hidden among the
squares. We want to scan the game board and print the number of hidden bombs. Our algorithm methodically
checks each row and each column to see if a hidden bomb is there, and if it is, we add 1 to the total number of
bombs. This is one way to write that:
How to write a Pseudo-code?

1. Arrange the sequence of tasks and write the pseudocode accordingly.


2. Start with the statement of a pseudo code which establishes the main goal or the aim.

Example:

This program will allow the user to check


the number whether it's even or odd.

3. The way the if-else, for, while loops are indented in a program, indent the statements likewise, as it helps to
comprehend the decision control and execution mechanism. They also improve the readability to a great extent.

Example:

if "1"
print response
"I am case 1"

if "2"
print response

"I am case 2"

4. Use appropriate naming conventions. The human tendency follows the approach
to follow what we see. If a programmer goes through a pseudo code, his
approach will be the same as per it, so the naming must be simple and distinct.
5. Use appropriate sentence casings, such as CamelCase for methods, upper case
for constants and lower case for variables.
6. Elaborate everything which is going to happen in the actual code. Don’t make the
pseudo code abstract.
7. Use standard programming structures such as ‘if-then’, ‘for’, ‘while’, ‘cases’ the
way we use it in programming.
8. Check whether all the sections of a pseudo code is complete, finite and clear to
understand and comprehend.
9. Don’t write the pseudo code in a complete programmatic manner. It is necessary
to be simple to understand even for a layman or client, hence don’t incorporate
too many technical terms.

STATEMENTS
A statement is defined as an instruction that directs the computer to perform a specific action. In writing
pseudocode, we will refer to singular instructions as statements.

Keywords
A keyword is a word that is reserved by a program because the word has a special meaning. Keywords can be
commands or parameters. Every programming language has its own keywords (reserved words). Keywords
cannot be used as variable names.

In Pseudocode, they are used to indicate common input-output and processing operations. They are written fully
in uppercase.

START: This is the start of your pseudocode.

INPUT: This is data retrieved from the user through typing or through an input device.

READ / GET: This is input used when reading data from a data file.

PRINT, DISPLAY, SHOW: This will show your output to a screen or the relevant output
device.

COMPUTE, CALCULATE, DETERMINE: This is used to calculate the result of an expression.

SET, INIT: To initialize values


INCREMENT, BUMP: To increase the value of a variable

DECREMENT: To reduce the value of a variable

CONDITIONALS
During algorithm development, we need statements which evaluate expressions and execute instructions
depending on whether the expression evaluated to True or False. Here are some common conditions used in
Pseudocode:

IF — ELSE IF — ELSE

This is a conditional that is used to provide statements to be executed if a certain condition is met. This also
applies to multiple conditions and different variables.

Here is an if statement with one condition

IF you are happy


THEN smile
ENDIF

Here is an if statement with an else section. Else allows for some statements to be executed if the “if” condition
is not met.

IF you are happy THEN


smile
ELSE
frown
ENDIF

We can add additional conditions to execute different statements if met.

IF you are happy THEN


smile
ELSE IF you are sad
frown
ELSE
keep face plain
ENDIF

ITERATION
To iterate is to repeat a set of instructions in order to generate a sequence of outcomes. We iterate so that we can
achieve a certain goal.

FOR structure

The FOR loop takes a group of elements and runs the code within the loop for each element.

FOR every month in a year Compute number of daysENDFOR


WHILE structure

Similar to the FOR loop, the while loop is a way to repeat a block of code as long as a predefined condition
remains true. Unlike the FOR loop, the while loop evaluates based on how long the condition will remain true.

To avoid a scenario where our while loop runs infinitely, we add an operation to manipulate the value within
each iteration. This can be through an increment, decrement, et cetera.

PRECONDITION: variable X is equal to 1


WHILE Population < Limit Compute Population as Population + Births — DeathsENDWHILE

FUNCTIONS
When solving advanced tasks it is necessary to break down the concepts in block of statements in different
locations. This is especially true when the statements in question serve a particular purpose. To reuse this code,
we create functions. We can then call these functions every-time we need them to run.

Function clear monitor


Pass In: nothing
Direct the operating system to clear the monitor
Pass Out: nothing
Endfunction

To emulate a function call in pseudocode, we can use the Call keyword

call: clear monitor


EXCEPTION HANDLING
An exception is an event which occurs during program execution that disrupts the normal flow of the
instructions. These are events that are non-desirable.

We need to observe such events and execute code-blocks in response to them. This is called exception handling.

BEGIN
statements
EXCEPTION
WHEN exception type
statements to handle exception
WHEN another exception type
statements to handle exception
END

Big(O) Notation and Time Complexity

So if you have a giant list of names, or phone numbers, or some other kind of input—what’s the best possible way of sorting them, or
searching them, based on whether the list has 10 entries, or 100, or a million?

That’s what Big-O notation tells you. It tells you how well that particular approach will do against very large problems.

1. O(1) – Constant Time. Time will always be the same.


2. O(n) – Linear Time. Time increases linearly based on size of data set.
3. O(n2) – Quadratic Time. Time increases in polynomial time (e.g., nested loops)
4. O(2n) – Time doubles with each data item added.
5. O(n!) – Factorial Complexity. Time grows exponentially due to the number of permutations.
6. O(log n) – Logarithmic Complexity. Time grows very slowly despite new data being added.
Constant Time

No matter what you provide as input to the algorithm, it’ll still run in the same amount of time.

 1 item, 1 second
 10 items, 1 second
 100 items, 1 second

Example: Determining if a binary number is even or odd.

Linear Time

Linear time is a concept where by time is seen sequentially, as a series of events that are leading toward something: beginning, and an
end.

I like to think of linear as step by step. Start at the beginning and slowly work your way to the end. With programming a for loop is a
great example of linear time.
Each time we step though the loop is incremented by one. Having your algorithm work in linear time can be quite efficient.

Informally, this means that the running time increases at most linearly with the size of the input. More precisely, this means that there
is a constant c such that the running time is at most cn for every input of size n. For example, a procedure that adds up all elements of
a list requires time proportional to the length of the list, if the adding time is constant, or, at least, bounded by a constant.

The calculation time increases at the same pace as the input.

 1 item, 1 second
 10 items, 10 seconds
 100 items, 100 seconds

Quadratic Time
The calculation time increases at the pace of n2.

 1 item, 1 second
 10 items, 100 seconds
 100 items, 10,000 seconds

Example: Bubble sort.

An example of an O(2n) function is the recursive calculation of Fibonacci numbers. O(2 n) denotes an algorithm whose growth doubles
with each addition to the input data set. The growth curve of an O(2n) function is exponential - starting off very shallow, then rising
meteorically.

Factorial Complexity
The calculation time increases at the pace of n!, which means if n is 5, it’s 5x4x3x2x1, or 120. This isn’t so bad at low values of
n, but it quickly becomes impossible.

 N=1, 1 option
 N=10, 3,628,800 options
 N=100, 9.332621544×10157 options

Example: Traveling salesman.


Logarithmic Complexity
The calculation time barely increases as you exponentially increase the input numbers.

 1 item, 1 second
 10 items, 2 seconds
 100 items, 3 seconds

Example: Binary search.

BEST, AVERAGE, AND WORST CASE

Best case time complexity: when array is already sorted.

Average case time complexity: when the given elements are sorted in random order.

Worst case time complexity: when the array is sorted or reverse sorted.

You might also like