0% found this document useful (0 votes)
2 views

algorithm design II - Copy_021021

The document discusses algorithm design, emphasizing the importance of understanding computer hardware and software, and the steps involved in problem-solving approaches. It outlines the criteria for a good algorithm, various notations for expressing algorithms, and the concept of variables and data types. Additionally, it covers control structures like sequences, conditional branching, and loops, as well as data structures such as arrays and records.

Uploaded by

donkengmichael2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

algorithm design II - Copy_021021

The document discusses algorithm design, emphasizing the importance of understanding computer hardware and software, and the steps involved in problem-solving approaches. It outlines the criteria for a good algorithm, various notations for expressing algorithms, and the concept of variables and data types. Additionally, it covers control structures like sequences, conditional branching, and loops, as well as data structures such as arrays and records.

Uploaded by

donkengmichael2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

CHAPTER ONE

CHAPTER ONE: ALGORITHM DESIGN


INTRODUCTION

Computer hardware refers to the physical parts the computer such as monitor, keyboard,
CPU, hard disk … Software refers to the set of instructions called program that directs the
hardware. Programs are written to solve problems or carry out tasks on the computer,
program is like the translation of the various steps to solve a problem or carry out tasks
called algorithm into a language that the computer can understand. So as we are thinking of
an algorithm, we must keep in mind that the computer will only do what we instruct it to do.
Therefore we must know the computer problem solving approach.

I. COMPUTER PROBLEM SOLVING APPROACH

The first step in writing instructions to carry out a task with the computer is to determine
what the output should be, that is exactly what the task should produce.
The second step is to identify the data input required for obtaining the output.
The third step is to determine how to process the inputs in order to obtain the desired
output. That is to determine what formula, method, way or steps that can be used to obtain
the output.
The problem solving approach should be adopted each time we are looking for the algorithm
that produces a solution of problem.
Example: A car travels 150km in 2h. How fast is the car moving?
Output: speed
Input: distance, time
Processes: Ask for the distance and time, set the speed to: distance/time, display the speed.

II. PRODUCTION OF AN ALGORITHEM

The process of producing an algorithm has several steps:

1. Read the problem carefully until you understand what the algorithm should do and
have the clear idea of the output.
2. Apply the problem solving approach that is determine the output, the input and
processes, methods, ways or steps to apply for solving the problem.
3. Implement the chosen processes to solve the problem while respecting the
conditions, if there is any.
4. Test the algorithm, that is try the algorithm with the exact or some randomly values
to ensure the correctness and the effectiveness of the algorithm.

III. Criteria for a good algorithm


An algorithm is a finite set of instructions which, if followed, accomplish a particular task. In
addition every algorithm must satisfy the following criteria:

1. input: there must be zero or more quantities which are externally supplied;
2. output: at least one quantity must be produced;
3. definiteness: each instruction must be clear and unambiguous;
4. finiteness: if we trace out the instructions of an algorithm, then for all cases the
algorithm will terminate after a finite number of steps;
5. Effectiveness: every instruction must be sufficiently basic that it can in principle be
carried out by a person using only pencil and paper. It is not enough that each
operation be definite, but it must also be feasible.

IV. NOTATION FOR EXPRESSING ALOGORITHM

An algorithm can be expressed in a variety of notations. The common ones are: Natural
languages, pseudo codes and flowchart.

a) Natural language.
A natural language is a human written or spoken language used by a community. Natural
language expressions of algorithms are rarely used for complex or technical processes.
Below is an algorithm (in natural language) which collects two numbers , sum up the
numbers , finds the difference of the numbers and then displays both the sum and the
difference of the numbers.

1. Get two numbers


2. Find the sum of the numbers
3. Find the difference between the numbers
4. Display the sum
5. Display the difference

b) FLOWCHARTS
A flowchart is a diagrammatic or symbolic representation that illustrates the sequence of
operations to be performed to get the solution of a problem, using symbols called charts and
arrows. These charts play a vital role in the programming of the problem and are quite
helpful in understanding the logic of complicated and lengthy problems. In flowcharts,
symbols are used to represent activities while arrows to represent the direction of activity
through the process. Typical examples of these symbols include the following:

Terminal symbol, oval shape, it Indicates the starting or ending of the program, process, or
interrupt program.
Process symbol, a rectangle, it indicates any type of internal operation inside the
Processor or Memory

Input/output symbol, a parallelogram, it represents any Input / Output (I/O) operation,


Indicating that the computer is to obtain data or output results

Decision symbol, the diamond represents a decision branching point or a question that can
be answered in a binary format (Yes/No, True/False)

Connector symbol, the circle allows the flowchart to be drawn without intersecting lines or
without a reverse flow. It also indicates that the flow continues where a matching symbol
(containing the same letter) has been placed.

Predefined Process Used to invoke a subroutine or an interrupt program.

Flow Lines Shows direction of flow.

Off-page-connector, used to indicate that the flowchart continue in the next page

Example: let us use the flowchart to write an algorithm to perform the summation of two
numbers a and b , taking from the user.
start A

sum=a+b
write (a,b)

write (sum )
read (a,b)

A end

c) Pseudo codes
Unlike a flowchart which is a graphic-based tool, pseudo codes are text-based algorithmic
design tools. In other words, a pseudo code is a way of writing an algorithm using short
natural language statements that are similar to programming language statements. Each
statement here expresses just an action or a step that the computer has to carry out in
performing the task. With pseudo codes every algorithm must start with the keyword
“Algorithm”, following by the name you have chosen for the algorithm, and must end by the
keyword “end”, the general syntax is :
Algorithem name-of –the-algorithm
Var variable1,variable2 (as types)
Begin
statment1
statment2
statment2
end.

V. THE CONCEPT OF VARIABLE AND BASIC ACTIONS


1. THE CONCEPT OF VARIABLE
Viewed as a container of value, a variable is a memory location that the CPU uses for storing
data in the course of solving a problem. An elementary variable can only contain a single
value at a given time.
1.1 CHARACTERISTIC OF A VARIABLE
A variable is characterized by its name, type, nature, use, initial value, final value and the
meaning. These characteristics enable to clearly understand the role of each variable in the
algorithm.
a) The name or the identifier
It helps to uniquely identify the variable in the algorithm. It consists of a set of
alphanumeric
Rules used in naming variables:

 The first character must be a letter


 They may include letters, numbers, and underscore (_)
 You cannot use a reserved word (word needed by algorithm)

b) The type of a variable


It indicates the set in which the variable takes its values. That is the range of values that
the variable can contain. It also implicitly indicates the operations that can be performed
with a variable.
c) The nature of a variable
A variable can be constant (having a value which must never change)or
changeable(variable)
d) The use of a variable
An object can be:
 An input : that is, used to store data needed to solve a problem
 An output: that is used to store a result of a problem
 An input/output : that it contains data at the beginning and the result at the end
of the algorithm
 An internal variable: that is , it contain neither a data nor the result of the
problem. This type of variable is used to facilitate the resolution of the problem.
e) The initial value.
This is the value that a variable takes for the first time in the algorithm. It can be difficult
to predict, In such case we allow the variable without an initial value.
f) The final value

This is the value that object contains at the end of its use in the algorithm.
g) The meaning of the variable.

It is advisable to indicate in a short sentence the role of each variable in the algorithm. It
can be a comment.

VI. Simple data type


In algorithm, we distinguish various types of data as follows:
a) Integer.
An integer is an variable that takes its value in a finite subset of integers (Z). The
subset of integers generally depends on the target machine. The subset of integers is
[-231; 231-1].

Operations on the integers


The arithmetic operations defined on integers are:
 Addition (+)
 Subtraction(+)
 Multiplication(*)
 Division (div)

The relational operators applicable to integer are.

 Less than (<)


 Less than or equal (<=)
 Greater than (>)
 Greater than or equal (>=)
 Different from (!=)
 Equal (=)

b) Real
Real numbers take their values in a finite set of integers and decimals. The operators
allowed on real are:
 Division (/)
 All others operators above can be applied on real

c) Characters
These are symbols used to represent words or phrases in natural languages. Each of
these symbols is represented in the computer by an 8 bits code known as ASCII
(American Standard Code for Information Interchange). We distinguish alphabetic,
numeric and special character. A constant character is written in single quote.
d) Boolean
A Boolean expression is an expression that yields two possible results (that is TRUE or
FALSE). On such object, possible logical operator like AND, OR and NOT can be
performed.

VII. ELEMENTARY ACTIONS


To solve a problem using the computer, it is important to known the list of primitive
actions of the computer. A primitive action is the one that the computer can perform
without any further additional information. Generally the computer we use can perform
the following actions.
 Assign a value (constant or not) to a variable using the symbol
Value
e.g variable name
 Get the value of a variable from an input device (such as keyboard), using the
keyword Read
e.g Read (value), Read(value1, value2……)

 Display the value of a variable on an output device (like the monitor) using the
keyword Write
e.g Write (“enter the value of a ”)

VIII. AVAILABLE CONTROL STRUCTURE


a) SEQUENCE
This is a sequence of statements that are executed in the same order in which they appear in
the algorithm or program, as from the first to the last.
e.g
algorithm name
(declaration)
Begin
Statement1
Statement2
statement3
.
.
.
Statement n
End

b) CONDITIONAL BRANCHING IF

This structure chooses a single subtask from a list of one or more alternatives others
according to the prescribed condition. A condition is an expression that yields Boolean
results (that is “TRUE” or “FALSE”). The relational operators used to express condition are:

 Less than (<)


 Less than or equal (<=)
 Greater than (>)
 Greater than or equal (>=)
 Different from (!=)
 Equal (=)
Conditional branching can be built from simple ones using the following logical operators as
“AND”, “OR” and “NOT”. Eg if (a>b)and (a>c).
Conditional branching can be implemented using the IF statement whose syntax is as follow:
1st form.
If (condition) then
Statement

Endif.
Exercise : represent the flow chart of this syntax.
Exercise ; write an algorithm to computer and display the solution of the equation : ax+b=0.

2nd form
If (condition) then
Statement1
Else
Statement2
End if
Here the condition is evaluated, if it is true, the statement1 is executed and if it is false,
statements2 is executed.
Exercise: represent the flow chart of this syntax and write the algorithm to compute the
absolute value of an integer. The write an algorithm that reads two integers and displays
their maximum.
Exercise write an algorithm that request for the coefficients a, b and c of the second
quadratic equation ax2+bx+c=0 , then compute the solution of the equation.

c) CONDITIONAL BRANCHING SWITCH


This is the mechanism to jump into a series of statements, the exact starting point depend
on the value of an expression. The syntax is the following:
Switch (expression)
Case value1: statement1
Case value2: statement2
.
.
Case value n: statement n
Default : statement
End switch

Exercise write an algorithm that a binary digit and display it in letter. Write an algorithm
that reads a positive integer corresponding to the number of a day within the week,
compute and displays the day’s name in full.

Exercise : write an algorithm that reads the average of a student and displays the grade . the
grades are assigned as follows:
100% : excellent, greater than or equal to 90% : very good, greater than or equal to 80% :
good, greater than or equal to 60%: fairly good, greater than or equal to 50%: average,
greater than or equal to : fail.

IX. REPETITIONS (LOOPS)


This structure allows the same sequence to be executed over and over a number of times
during the program execution. This repetition is called “looping”. A loop is an instruction or
set of instructions that is carried out repeatedly. They are typically three types of loop: the
while loop, the Repeat loop and the For loop.

a) THE WHILE LOOP


Syntax
While (condition) do
Statement
End while.

In this loop, the condition is evaluated, if it is true then, “statement” is executed, condition
is reevaluated and the cycle continues, until condition becomes false, which mean the end of
the loop.

Exercise: draw the flowchart of the above loop. Write an algorithm which reads a positive
integer n and then compute and display the sum of the first n integers.

b) THE REPEAT UNTIL LOOP


SYMTAX

Repeat
Statement
Until (condition)

In this loop, the statement is executed and the condition is evaluated, if the condition is false
, then statement is executed again , and the cycle continue until the condition becomes true,
it is the end of the loop.
Exercise : draw the flowchart of this loop and write an algorithm that reads a positive integer
n, compute and displays its square root.
Write an algorithm that reads a sequence of positive values, computes and displays the sum,
the number of values, the minimum , the maximum, the average , the variance and the
standard deviation. You may indicate the end of the sequence by a negative or null value.

c) FOR LOOP
Syntax
For counter from start_value to stop_value
Statements
Endfor

Here , statement will be executed for a value of counter from start_value to stop_value. The
for loop is used when the number of iteration is known in advance.
Exercise draw the flowchart of the For loop and write an algorithm that read a number n,
computes and displays its factorial.
Write an algorithm that requests for a positive integer n, computes and displays the sum of
its factors.
CHAPTER TWO

X. NOTION OF DATA STRUCTURE


A data structure is a specialized format for organizing and storing data in the computer. Any
data structure is designed to organize data to suit a particular purpose so that it can be
access and worked with in appropriate ways. Some of the most commonly used data
structures are: arrays, records, linked lists, tree, queues, stack and tables.
a) Arrays
An array is defined as a set of data items of the same types. The individual data in an
array are called elements; each element has a unique identifying index, which is used to
access that particular element in the set. Arrays allow us to in one structure what we
would normally put in many variables. They facilitate the organization and search for
individual elements of the storage medium. Sets of elements such as examination marks
for a class may be conveniently stored and arranged into a sequence and referred to by
indexes. Eg Mark= (10 92 25 36 14). An element may be accessed separately by stating
its position in the array, eg Mark(1) will refer to 10 in the example above. Arrays can be
more than one dimension. A one dimensional array is called vector while a two
dimensional array is called matrix.
b) String
A string is a sequence of consecutive characters that are manipulated as group. Some
few operations can be applied to string such as concatenation which adds one string
onto the end of the other, that data & type would then be datatype.
Strings are declared as packed arrays of characters, for example string_name=
array_name[1…3] OF CHAR.
c) Records
A record is collection of related data elements (called fields ) that are treated as a single
unit. Record will be easily understood if compared to arrays. Records differ from arrays
in that data, the individual data items in records may differ in type whereas all the data
items in the array have the same type. Examples of records

Candidate := record
Candidate_number: integer
Candidate_namer: String
Candidate_mark: real
Endrecord
In this example the word candidate is the record name and Candidate_number is a
record’s field.
d) Linked Lists
A finite set of entries with a certain order is known as a list. Two common forms of list
data structures are list linked list and arrays. A linked list is a data structure consisting of
a number of nodes, each containing one element and one or more links to others nodes.
Linked list permit insertion and removal of nodes at any point of the list. Linked list do no
allow random access
e) Queue
In computer science, a queue (/ˈkjuː/ KEW) is a particular kind of abstract data type or
collection in which the entities in the collection are kept in order and the principal (or
only) operations on the collection are the addition of entities to the back terminal
position, known as enqueue, and removal of entities from the front terminal position,
known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure. In a
FIFO data structure, the first element added to the queue will be the first one to be
removed. This is equivalent to the requirement that once a new element is added, all
elements that were added before have to be removed before the new element can be
removed.

f) Stalk
In computer science, a stack is a particular kind of data structure or collection in which
the principal (or only) operations on the collection are the addition of an entity to the
collection, known as push and removal of an entity, known as pop. The relation between
the push and pop operations is such that the stack is a Last-In-First-Out (LIFO) data
structure. In a LIFO data structure, the last element added to the structure must be the
first one to be removed. This is equivalent to the requirement that, considered as a
linear data structure, or more abstractly a sequential collection, the push and pop
operations occur only at one end of the structure, referred to as the top of the stack. G)

g) Trees
In computer science, a tree is a type of data structure in which each element is attached
to one or more elements directly beneath it. The elements of this hierarchical data
structure are called nodes. The node which at the top of the tree is called root and the
node with no children is called leaf.
h) Tables
A table is a group of records, each of which is composed of fields common to all the
other records. The main purpose of this data structure is to improve data access by
reducing search time.
i) Files
A file is made up of one or more tables.

TUTORIAL ONE

Tutorials on algorithms
Conational structures

Use the conditional structure if or case to answer each of the following questions

1. Write an algorithm to accept two integers and check whether they are equal or not

2. Write an algorithm to check whether a given number is even or odd.

3. Write an algorithm to check whether a given number is positive or negative.

4. Write an algorithm to find whether a given year is a leap year or not

5. Write an algorithm to read the age of a Cameroonian and determine whether it is eligible
for casting his/her own vote.

7. Write an algorithm to accept the height of a person in centimeter and categorize the person
according to their height.

8. Write an algorithm to find the largest of three numbers


9. Write an algorithm to accept a coordinate point in a XY coordinate system and determine in
which quadrant the coordinate point lies

10. Write an algorithm to find the eligibility of admission for a professional course based on
the following criteria:
Marks in Maths >=65
Marks in Phy >=55
Marks in Chem>=50
Total in all three subject >=180
or
Total in Math and Subjects >=140

11. Write a an algorithm to calculate the root of a Quadratic Equation.

12. Write an algorithm to read roll number, name and marks of three subjects and calculate
the total and the average of the student.

13. Write a an algorithm to read temperature in centigrade and display a suitable message
according to temperature state below :
Temp < 0 then Freezing weather
Temp 0-10 then Very Cold weather
Temp 10-20 then Cold weather
Temp 20-30 then Normal in Temp
Temp 30-40 then Its Hot
Temp >=40 then Its Very Hot

19. Write an algorithm to calculate and print the Electricity bill of a given customer. The
customer id., name and unit consumed by the user should be taken from the keyboard and
display the total amount to pay to the customer. The charge are as follow :

Unit Charge/unit
Up to 199 120
200 and above but less than 400 150
400 and above but less than 600 180
600 and above 200
20. Write a program in C to accept a grade and declare the equivalent description :

Grade Description
E Excellent
V Very Good
G Good
A Average
F Fail
21. Write an algorithm to read any day number in integer and display day name in the word.
22. Write an algorithm to read any digit, display in the word.
23. Write an algorithm to read any Month Number in integer and display Month name in the
word
24. Write an algorithm to read any Month Number in integer and display the number of days
for this month.

25. Write an algorithm which is a Menu-Driven Program to compute the area of the various
geometrical shape.

26. Write a an algorithm which is a Menu-Driven Program to perform a simple


calculationExpected

Loops

Use the For loop, the while and the repeat until loop to answer the following questions

1. Write an algorithm to display the first 10 natural numbers.

2. Write an algorithm to find the sum of first 10 natural numbers.

3. Write an algorithm to display n terms of natural number and their sum.

4. Write an algorithm to read 10 numbers from keyboard and find their sum and average.

6. Write an algorithm to display the multiplication table of a given integer.

XI. ALGORITHM STRATEGIES AND DESIGN


The study of algorithms has come to be recognized as the cornerstone of computer science, and
it becomes a necessity to study the various strategies and techniques in order to choose the
most appropriate while solving a problem. Algorithm design techniques give guidance and
direction on how to create a new algorithm. Second, the study of these techniques help us to
categorize or organize the algorithms we know and in the way to understand them better."
An algorithm strategy is a general approach to solve problems algorithmically. Although
more than one technique may be applicable to a specific problem, it is often the case that an
algorithm constructed by one approach is clearly superior to equivalent solutions built using
alternative techniques. We shall discus here the following algorithm strategies: greedy
algorithm, divide and conquer strategy and dynamic programming.

1) Greedy Algorithms

An algorithm is designed to achieve optimum solution for a given problem. In greedy


algorithm approach, decisions are made from the given solution domain. As being greedy, the
closest solution that seems to provide an optimum solution is chosen.

Greedy algorithms try to find a localized optimum solution, which may eventually lead to
globally optimized solutions. However, generally greedy algorithms do not provide globally
optimized solutions.

Counting Coins
This problem is to count to a desired value by choosing the least possible coins and the greedy
approach forces the algorithm to pick the largest possible coin. If we are provided coins of ₹
1, 2, 5 and 10 and we are asked to count ₹ 18 then the greedy procedure will be −

 1 − Select one ₹ 10 coin, the remaining count is 8


 2 − Then select one ₹ 5 coin, the remaining count is 3
 3 − Then select one ₹ 2 coin, the remaining count is 1
 4 − And finally, the selection of one ₹ 1 coins solves the problem

Though, it seems to be working fine, for this count we need to pick only 4 coins. But if we
slightly change the problem then the same approach may not be able to produce the same
optimum result.

For the currency system, where we have coins of 1, 7, 10 value, counting coins for value 18
will be absolutely optimum but for count like 15, it may use more coins than necessary. For
example, the greedy approach will use 10 + 1 + 1 + 1 + 1 + 1, total 6 coins. Whereas the same
problem could be solved by using only 3 coins 7+7+1

Hence, we may conclude that the greedy approach picks an immediate optimized solution and
may fail where global optimization is a major concern.

Examples

Most networking algorithms use the greedy approach. Here is a list of few of them −

 Travelling Salesman Problem


 Prim's Minimal Spanning Tree Algorithm
 Kruskal's Minimal Spanning Tree Algorithm
 Dijkstra's Minimal Spanning Tree Algorithm
 Graph - Map Coloring
 Graph - Vertex Cover
 Knapsack Problem
 Job Scheduling Problem

There are lots of similar problems that uses the greedy approach to find an optimum solution.

2) Divide and Conquer

In divide and conquer approach, the problem in hand, is divided into smaller sub-problems
and then each problem is solved independently. When we keep on dividing the subproblems
into even smaller sub-problems, we may eventually reach a stage where no more division is
possible. Those "atomic" smallest possible sub-problem fractions are solved. The solution of
all sub-problems is finally merged in order to obtain the solution of an original problem.
Broadly, we can understand divide-and-conquer approach in a three-step process.

Divide/Break

This step involves breaking the problem into smaller sub-problems. Sub-problems should
represent a part of the original problem. This step generally takes a recursive approach to
divide the problem until no sub-problem is further divisible. At this stage, sub-problems
become atomic in nature but still represent some part of the actual problem.

Conquer/Solve

This step receives a lot of smaller sub-problems to be solved. Generally, at this level, the
problems are considered 'solved' on their own.

Merge/Combine

When the smaller sub-problems are solved, this stage recursively combines them until they
formulate a solution of the original problem. This algorithmic approach works recursively and
conquer & merge steps works so close that they appear as one.

Examples

The following computer algorithms are based on divide-and-conquer programming approach


 Merge Sort
 Quick Sort
 Binary Search
 Strassen's Matrix Multiplication
 Closest pair points
There are various ways available to solve any computer problem, but the mentioned are a
good example of divide and conquer approach.

3) Dynamic Programming

Dynamic programming approach is similar to divide and conquer in breaking down the
problem into smaller and yet smaller possible sub-problems. But unlike, divide and conquer,
these sub-problems are not solved independently. Rather, results of these smaller sub-
problems are remembered and used for similar or overlapping sub-problems.

Dynamic programming is used where we have problems, which can be divided into similar
sub-problems, so that their results can be re-used. Mostly, these algorithms are used for
optimization. Before solving the in-hand sub-problem, dynamic algorithm will try to examine
the results of the previously solved sub-problems. The solutions of sub-problems are
combined in order to achieve the best solution.

So we can say that −

 The problem should be able to be divided into smaller overlapping sub-problem.


 An optimum solution can be achieved by using an optimum solution of smaller sub-
problems.
 Dynamic algorithms use memorization.

Comparison

In contrast to greedy algorithms, where local optimization is addressed, dynamic algorithms


are motivated for an overall optimization of the problem.

In contrast to divide and conquer algorithms, where solutions are combined to achieve an
overall solution, dynamic algorithms use the output of a smaller sub-problem and then try to
optimize a bigger sub-problem. Dynamic algorithms use memorization to remember the
output of already solved sub-problems.

Example

The following computer problems can be solved using dynamic programming approach −

 Fibonacci number series


 Knapsack problem
 Tower of Hanoi
 All pair shortest path by Floyd-Warshall
 Shortest path by Dijkstra
 Project scheduling

Dynamic programming can be used in both top-down and bottom-up manner. And of course,
most of the times, referring to the previous solution output is cheaper than recomputing in
terms of CPU cycles.

4) TOP-DOWN DESIGN And BOTTOM-UP DESIGN


A system consists of components, which have components of their own; indeed a system is a
hierarchy of components. The highest-level component corresponds to the total system. To
design such hierarchies there are two possible approaches: top-down and bottom-up. The
top-down approach starts from the highest-level component of the hierarchy and proceeds
through to lower levels. By contrast, a bottom-up approach starts with the lowest-level
component of the hierarchy and proceeds through progressively higher levels to the top-
level component.

a) A top-down design

A top-down approach (also known as stepwise design) is essentially the breaking down of a
system to gain insight into the sub-systems that make it up. In a top-down approach an
overview of the system is formulated, specifying but not detailing any first-level subsystems.
Each subsystem is then refined in yet greater detail, sometimes in many additional
subsystem levels, until the entire specification is reduced to base elements. Once these base
elements are recognized then we can build these as computer modules. Once they are built
we can put them together, making the entire system from these individual components.

top-down approach starts by identifying the major components of the system, decomposing
them into their lower-level components and iterating until the desired level of detail is
achieved. Most design methodologies are based on the top-down approach.

A top-down approach is suitable only if the specifications of the system are clearly known
and the system development is from scratch. However, if a system is to be built from an
existing system, a bottom-up approach is more suitable, as it starts from some existing
components.

b) A bottom-up design

A bottom-up design approach starts with designing the most basic or primitive components
and proceeds to higher-level components that use these lower-level components.

XII. Algorithm Complexity

Suppose X is an algorithm and n is the size of input data, the time and space used by the
algorithm X are the two main factors, which decide the efficiency of X.

 Time Factor − Time is measured by counting the number of key operations such as
comparisons in the sorting algorithm.
 Space Factor − Space is measured by counting the maximum memory space required
by the algorithm.

The complexity of an algorithm fn gives the running time and/or the storage space required by
the algorithm in terms of n as the size of input data.

Space Complexity
Space complexity of an algorithm represents the amount of memory space required by the
algorithm in its life cycle. The space required by an algorithm is equal to the sum of the
following two components −

 A fixed part that is a space required to store certain data and variables, that are
independent of the size of the problem. For example, simple variables and constants
used, program size, etc.
 A variable part is a space required by variables, whose size depends on the size of the
problem. For example, dynamic memory allocation, recursion stack space, etc.

Time Complexity

Time complexity of an algorithm represents the amount of time required by the algorithm to
run to completion. Time requirements can be defined as a numerical function Tn, where Tn
can be measured as the number of steps, provided each step consumes constant time.

For example, addition of two n-bit integers takes n steps. Consequently, the total
computational time is Tn = c ∗ n, where c is the time taken for the addition of two bits. Here,
we observe that Tn grows linearly as the input size increases.

Asymptotic Analysis

Asymptotic analysis of an algorithm refers to defining the mathematical boundation/framing


of its run-time performance. Using asymptotic analysis, we can very well conclude the best
case, average case, and worst case scenario of an algorithm.

Usually, the time required by an algorithm falls under three types −

 Best Case − Minimum time required for program execution.


 Average Case − Average time required for program execution.
 Worst Case − Maximum time required for program execution.

Asymptotic Notations

Following are the commonly used asymptotic notations to calculate the running time
complexity of an algorithm.

 Ο Notation
 Ω Notation
 θ Notation

Big Oh Notation, Ο

The notation Οn is the formal way to express the upper bound of an algorithm's running time.
It measures the worst case time complexity or the longest amount of time an algorithm can
possibly take to complete.

Omega Notation, Ω
The notation Ωn is the formal way to express the lower bound of an algorithm's running time.
It measures the best case time complexity or the best amount of time an algorithm can
possibly take to complete.

Theta Notation, θ

The notation θn is the formal way to express both the lower bound and the upper bound of an
algorithm's running time. It is represented as follows −

Common Asymptotic Notations

Following is a list of some common asymptotic notations −

constant Ο1
logarithmic Οlogn
linear Οn
n log n Οnlogn
quadratic Ο(n2)
cubic Ο(n3)
polynomial nΟ1
exponential 2Οn

XIII. Linear Search Agorithm

Linear search is a very simple search algorithm. In this type of search, a sequential search is
made over all items one by one. Every item is checked and if a match is found then that
particular item is returned, otherwise the search continues till the end of the data collection.

Algorithm

Linear Search ( Array A, Value x)

Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit

Pseudocode

procedure linear_search (list, value)

for each item in the list


if match item == value

return the item's location

end if

end for

end procedure

XIV. Binary Search Algorithm

Binary search is a fast search algorithm with run-time complexity of Οlogn. This search
algorithm works on the principle of divide and conquers. For this algorithm to work properly,
the data collection should be in the sorted form.

Binary search looks for a particular item by comparing the middle most item of the collection.
If a match occurs, then the index of item is returned. If the middle item is greater than the
item, then the item is searched in the sub-array to the left of the middle item. Otherwise, the
item is searched for in the sub-array to the right of the middle item. This process continues on
the sub-array as well until the size of the subarray reduces to zero.

Binary search halves the searchable items and thus reduces the count of comparisons to be
made to very less numbers.

Pseudocode

The pseudocode of binary search algorithms should look like this −

Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched

Set lowerBound = 1
Set upperBound = n

while x not found


if upperBound < lowerBound
EXIT: x does not exists.

set midPoint = lowerBound + ( upperBound - lowerBound )


/ 2

if A[midPoint] < x
set lowerBound = midPoint + 1

if A[midPoint] > x
set upperBound = midPoint - 1

if A[midPoint] = x
EXIT: x found at location midPoint

end while

end procedure

XV. SORTING ALGORITHMS

Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to
arrange data in a particular order. Most common orders are in numerical or lexicographical
order.

The importance of sorting lies in the fact that data searching can be optimized to a very high
level, if data is stored in a sorted manner. Sorting is also used to represent data in more
readable formats. Following are some of the examples of sorting in real-life scenarios −

 Telephone Directory − The telephone directory stores the telephone numbers of


people sorted by their names, so that the names can be searched easily.
 Dictionary − The dictionary stores words in an alphabetical order so that searching of
any word becomes easy.

In-place Sorting and Not-in-place Sorting

Sorting algorithms may require some extra space for comparison and temporary storage of
few data elements. The algorithms that do not require any extra space and sorting is said to
happen in-place, or for example, within the array itself. This is called in-place sorting.
Bubble sort is an example of in-place sorting.

However, in some sorting algorithms, the program requires space which is more than or equal
to the elements being sorted. Sorting which uses equal or more space is called not-in-place
sorting. Merge-sort is an example of not-in-place sorting.

Selection Sort

The principle of Selection Sort is to find the smallest element of the data sequence (from
index 0 to n-1) and put it to the beginning of the sequence. This procedure is then applied on
the yet unsorted areas (1 to n-1, 2 to n-1 and so on), until the area from n-2 to n-1 has been
sorted.

Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-
based algorithm in which the list is divided into two parts, the sorted part at the left end and
the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the
entire list.

The smallest element is selected from the unsorted array and swapped with the leftmost
element, and that element becomes a part of the sorted array. This process continues moving
unsorted array boundary by one element to the right.

This algorithm is not suitable for large data sets as its average and worst case complexities are
of Ο(n2), where n is the number of items.
. Algorithm

Step 1 − Set MIN to location 0


Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat until list is sorted

Pseudocode

procedure selection sort


list : array of items
n : size of list

for i = 1 to n - 1
/* set current element as minimum*/
min = i

/* check the element to be minimum */

for j = i+1 to n
if list[j] < list[min] then
min = j;
end if
end for

/* swap the minimum element with the current element*/


if indexMin != i then
swap list[min] and list[i]
end if

end for

end procedure

Insertion Sort
This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which
is always sorted. For example, the lower part of an array is maintained to be sorted. An
element which is to be 'insert'ed in this sorted sub-list, has to find its appropriate place and
then it has to be inserted there. Hence the name, insertion sort.

The array is searched sequentially and unsorted items are moved and inserted into the sorted
sub-list in the same array. This algorithm is not suitable for large data sets as its average and
worst case complexity are of Ο(n2), where n is the number of items.

Algorithm

Now we have a bigger picture of how this sorting technique works, so we can derive simple
steps by which we can achieve insertion sort.

Step 1 − If it is the first element, it is already sorted.


return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is
greater than the
value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted

Pseudocode

procedure insertionSort( A : array of items )


int holePosition
int valueToInsert

for i = 1 to length(A) inclusive do:

/* select value to be inserted */


valueToInsert = A[i]
holePosition = i

/*locate hole position for the element to be inserted */


while holePosition > 0 and A[holePosition-1] >
valueToInsert do:
A[holePosition] = A[holePosition-1]
holePosition = holePosition -1
end while

/* insert the number at hole position */


A[holePosition] = valueToInsert

end for

end procedure

Bubble Sort Algorithm

Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based


algorithm in which each pair of adjacent elements is compared and the elements are swapped
if they are not in order. This algorithm is not suitable for large data sets as its average and
worst case complexity are of Ο(n2) where n is the number of items..

Algorithm

We assume list is an array of n elements. We further assume that swap function swaps the
values of the given array elements.
begin BubbleSort(list)

for all elements of list


if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for

return list

end BubbleSort

Pseudocode

We observe in algorithm that Bubble Sort compares each pair of array element unless the
whole array is completely sorted in an ascending order. This may cause a few complexity
issues like what if the array needs no more swapping as all the elements are already
ascending.

To ease-out the issue, we use one flag variable swapped which will help us see if any swap
has happened or not. If no swap has occurred, i.e. the array requires no more processing to be
sorted, it will come out of the loop.

Pseudocode of BubbleSort algorithm can be written as follows −

procedure bubbleSort( list : array of items )

loop = list.count;

for i = 0 to loop-1 do:


swapped = false

for j = 0 to loop-1 do:

/* compare the adjacent elements */


if list[j] > list[j+1] then
/* swap them */
swap( list[j], list[j+1] )
swapped = true
end if

end for

/*if no number was swapped that means


array is sorted now, break the loop.*/

if(not swapped) then


break
end if

end for
end procedure return list

Merge Sort Algorithm

Merge sort is a sorting technique based on divide and conquer technique. With worst-case
time complexity being Οnlogn, it is one of the most respected algorithms.

Merge sort first divides the array into equal halves and then combines them in a sorted
manner.

Algorithm

Merge sort keeps on dividing the list into equal halves until it can no more be divided. By
definition, if it is only one element in the list, it is sorted. Then, merge sort combines the
smaller sorted lists keeping the new list sorted too.

Step 1 − if it is only one element in the list it is already


sorted, return.
Step 2 − divide the list recursively into two halves until it
can no more be divided.
Step 3 − merge the smaller lists into new list in sorted
order.

Pseudocode

We shall now see the pseudocodes for merge sort functions. As our algorithms point out two
main functions − divide & merge.

Merge sort works with recursion and we shall see our implementation in the same way.

procedure mergesort( var a as array )


if ( n == 1 ) return a
var l1 as array = a[0] ... a[n/2]
var l2 as array = a[n/2+1] ... a[n]

l1 = mergesort( l1 )
l2 = mergesort( l2 )

return merge( l1, l2 )


end procedure

procedure merge( var a as array, var b as array )

var c as array

while ( a and b have elements )


if ( a[0] > b[0] )
add b[0] to the end of c
remove b[0] from b
else
add a[0] to the end of c
remove a[0] from a
end if
end while

while ( a has elements )


add a[0] to the end of c
remove a[0] from a
end while

while ( b has elements )


add b[0] to the end of c
remove b[0] from b
end while

return c

end procedure

To know about merge sort implementation in C programming language, please click here.

Quick Sort

Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data
into smaller arrays. A large array is partitioned into two arrays one of which holds values
smaller than the specified value, say pivot, based on which the partition is made and another
array holds values greater than the pivot value.

Quick sort partitions an array and then calls itself recursively twice to sort the two resulting
subarrays. This algorithm is quite efficient for large-sized data sets as its average and worst
case complexity are of Οnlogn, where n is the number of items.
Partition in Quick Sort

Following animated representation explains how to find the pivot value in an array.

The pivot value divides the list into two parts. And recursively, we find the pivot for each sub-
lists until all lists contains only one element.

Quick Sort Pivot Algorithm

Based on our understanding of partitioning in quick sort, we will now try to write an
algorithm for it, which is as follows.

Step 1 − Choose the highest index value has pivot


Step 2 − Take two variables to point left and right of the
list excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left
and right
Step 8 − if left ≥ right, the point where they met is new
pivot

Quick Sort Pivot Pseudocode

The pseudocode for the above algorithm can be derived as −

function partitionFunc(left, right, pivot)


leftPointer = left
rightPointer = right - 1

while True do
while A[++leftPointer] < pivot do
//do-nothing
end while

while rightPointer > 0 && A[--rightPointer] > pivot do


//do-nothing
end while

if leftPointer >= rightPointer


break
else
swap leftPointer,rightPointer
end if

end while

swap leftPointer,right
return leftPointer

end function

Quick Sort Algorithm

Using pivot algorithm recursively, we end up with smaller possible partitions. Each partition
is then processed for quick sort. We define recursive algorithm for quicksort as follows −

Step 1 − Make the right-most index value pivot


Step 2 − partition the array using pivot value
Step 3 − quicksort left partition recursively
Step 4 − quicksort right partition recursively

Quick Sort Pseudocode

To get more into it, let see the pseudocode for quick sort algorithm −

procedure quickSort(left, right)

if right-left <= 0
return
else
pivot = A[right]
partition = partitionFunc(left, right, pivot)
quickSort(left,partition-1)
quickSort(partition+1,right)
end if

end procedure

You might also like