Вы находитесь на странице: 1из 24

Data Structures & Algorithms

Data Structure is a systematic way to organize data in order to use it efficiently.


Data Structure is a way of collecting and organising data in such a way that we can perform
operations on these data in an effective way.

Basic types of Data Structures


Anything that can store data can be called as a data structure, hence Integer,
Float, Boolean, Char etc, all are data structures. They are known as Primitive
Data Structures.
Then we also have some complex Data Structures, which are used to store large
and connected data. Some example of Abstract Data Structure are :
 Linked List
 Tree
 Graph
 Stack, Queue etc.
All these data structures allow us to perform different operations on data. We
select these data structures based on which type of operation is required

Characteristics of a Data Structure


 Correctness − Data structure implementation should implement its
interface correctly.
 Time Complexity − Running time or the execution time of operations of
data structure must be as small as possible.
 Space Complexity − Memory usage of a data structure operation should
be as little as possible.

Algorithms Basics
Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a
certain order to get the desired output.

An algorithm is a finite set of instructions or logic, written in order, to accomplish a certain


predefined task.
Algorithm is not the complete code or program, it is just the core logic(solution) of a problem,
which can be expressed either as an informal high level description as pseudo-code or using a
flowchart.

Algorithms are generally created independent of underlying languages, i.e. an algorithm can
be implemented in more than one programming language.

From the data structure point of view, following are some important categories
of algorithms
 Search − Algorithm to search an item in a data structure.
 Sort − Algorithm to sort items in a certain order.
 Insert − Algorithm to insert item in a data structure.
 Update − Algorithm to update an existing item in a data structure.
 Delete − Algorithm to delete an existing item from a data structure.

Characteristics of an Algorithm
Not all procedures can be called an algorithm. An algorithm should have the
following characteristics – (Every Algorithm must satisfy the following
properties)
 Unambiguous − Algorithm should be clear and unambiguous. Each of its
steps (or phases), and their inputs/outputs should be clear and must lead to
only one meaning.
 Input − An algorithm should have 0 or more well-defined inputs.
 Output − An algorithm should have 1 or more well-defined outputs, and
should match the desired output.
 Finiteness − Algorithms must terminate after a finite number of steps.
 Feasibility − Should be feasible with the available resources.
 Independent − An algorithm should have step-by-step directions, which
should be independent of any programming code.
 Correctness- Every step of the algorithm must generate a correct output.
An algorithm is said to be efficient and fast, if it takes less time to execute and
consumes less memory space. The performance of an algorithm is measured on
the basis of following properties :
1. Time Complexity
2. Space Complexity

Space Complexity
Its the amount of memory space required by the algorithm, during the course of
its execution. Space complexity must be taken seriously for multi-user systems
and in situations where limited memory is available.
An algorithm generally requires space for following components :
 Instruction Space: Its the space required to store the executable version of the
program. This space is fixed, but varies depending upon the number of lines of code in
the program.
 Data Space: It’s the space required to store all the constants and variables (including
temporary variables) value.
 Environment Space: It’s the space required to store the environment information
needed to resume the suspended function.
Time Complexity
Time Complexity is a way to represent the amount of time required by the
program to run till its completion. It's generally a good practice to try to keep the
time required minimum, so that our algorithm completes it's execution in the
minimum time possible.

How to Write an Algorithm?


There are no well-defined standards for writing algorithms. Rather, it is problem
and resource dependent. Algorithms are never written to support a particular
programming code.
As we know that all programming languages share basic code constructs like
loops (do, for, while), flow-control (if-else), etc. These common constructs can
be used to write an algorithm
Symbols Used In Flowchart
Different symbols are used for different states in flowchart, For example:
Input/Output and decision making has different symbols. The table below
describes all the symbols that are used in making flowchart
Symbol Purpose Description

Used to indicate the flow of logic by connecting


Flow line
symbols.

Terminal(Stop/Start) Used to represent start and end of flowchart.

Input/Output Used for input and output operation.

Used for airthmetic operations and data-


Processing
manipulations.

Used to represent the operation in which there are two


Desicion
alternatives, true and false.

On-page Connector Used to join different flowline

Off-page Connector Used to connect flowchart portion on different page.

Predefined Used to represent a group of statements performing


Process/Function one processing task.

Though, flowchart are useful in efficient coding, debugging and analysis of a


program, drawing flowchart in very complicated in case of complex programs
and often ignored.
Algorithm is not the computer code. Algorithm are just the instructions which
gives clear idea to you idea to write the computer code.

NB: In design and analysis of algorithms, usually the second method is used to
describe an algorithm. It makes it easy for the analyst to analyze the algorithm
ignoring all unwanted definitions.
Writing step numbers, is optional.
We design an algorithm to get a solution of a given problem. A problem can be
solved in more than one ways.

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.
The way to write arithmetic expression is known as a notation. An arithmetic
expression can be written in three different but equivalent notations, i.e., without
changing the essence or output of an expression. These notations are −
 Infix Notation
 Prefix (Polish) Notation
 Postfix (Reverse-Polish) Notation
These notations are named as how they use operator in expression. We shall
learn the same here in this chapter.
Infix Notation
We write expression in infix notation, e.g. a - b + c, where operators are used in-
between operands. It is easy for us humans to read, write, and speak in infix
notation but the same does not go well with computing devices. An algorithm to
process infix notation could be difficult and costly in terms of time and space
consumption.
Prefix Notation
In this notation, operator is prefixed to operands, i.e. operator is written ahead
of operands. For example, +ab. This is equivalent to its infix notation a + b.
Prefix notation is also known as Polish Notation.
Postfix Notation
This notation style is known as Reversed Polish Notation. In this notation style,
the operator is postfixed to the operands i.e., the operator is written after the
operands. For example, ab+. This is equivalent to its infix notation a + b.

The following table briefly tries to show the difference in all three notations –

Sr.No. Infix Notation Prefix Notation Postfix Notation

1 a+b +ab ab+

2 (a + b) ∗ c ∗+abc ab+c∗

3 a ∗ (b + c) ∗a+bc abc+∗

4 a/b+c/d +/ab/cd ab/cd/+

5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗

6 ((a + b) ∗ c) - d -∗+abcd ab+c∗d-


Rules (Guidelines) of Drawing Flowcharts for Algorithms
NB: Flowchart is pictorial representation of an algorithm.

1. The Title for every flowchart is compulsory.


2. There must be START and END point for every flowchart.
3. The symbols used in flowchart should have only one entry point on the
top. The exit point for symbols (except for decision/diamond symbol) is
on the button.
4. There should be two exit points for decision symbol; exit points can be on
the bottom and one side or on the sides.
5. The flow of flowchart is generally from top to bottom. But in some cases,
it can also flow to upward direction
6. The direction of the flow of control should be indicated by arrowheads.
7. The operations for every step should be written inside the symbol.
8. The language used in flowchart should be simple so that it can be easily
understood.
9. The flow-lines that show the direction of flow of flowchart must not cross
each other.
10.While connecting different pages of the same flowchart, Connectors must
be used.

Examples of Flowcharts for Algorithms


MORE EXAMPLES.
Example 1: Calculate the Interest of a Bank Deposit
Algorithm:
 Step 1: START
 Step 2: Read amount,
 Step 3: Read years,
 Step 4: Read rate,
 Step 5: Calculate the interest with formula
"Interest=Amount*Years*Rate/100
 Step 6: Print interest,
 Step 7: END
Flowchart:
Example 2: Determine and Output Whether Number N is Even or Odd
Algorithm:
 Step 1: START
 Step 2: Read number N,
 Step 3: Set remainder as N modulo 2,
 Step 4: If remainder is equal to 0 then number N is even, else number N
is odd,
 Step 5: Print output.
 Step 6: END

Flowchart:
Example 3: Determine Whether a Temperature is Below or Above the
Freezing Point
Algorithm:
 Step 1: START
 Step 2: Input temperature,
 Step 3: If it is less than 32, then print "below freezing point", otherwise
print "above freezing point"
 Step 4: STOP
Flowchart:

Example 4: Determine Whether a Student Passed the Exam or Not:


Algorithm:
o Step 1: BEGIN
o Step 2: Input grades of 4 courses M1, M2, M3 and M4,
o Step 3: Calculate the average grade with formula
"Grade=(M1+M2+M3+M4)/4"
o Step 4: If the average grade is less than 60, print "FAIL", else print
"PASS".
o Step 5: STOP
Flowchart:

Example 5: Print 1 to 20:


Algorithm:
Step 1: START
Step 2: Initialize X as 0,
Step 3: Increment X by 1,
Step 4: Print X,
Step 5: If X is less than 20 then go back to step 2.
Step 6: END
Flowchart:

Examples 7: An algorithm to calculate even numbers between 20 and 40

Step1: Start

Step2: Store 20 in I

Step3: Display I

Step4: Add 2 to I

Step5: IF(I<=40) then go to Step 3

Step6: End
Example 8: Write an algorithm to find the smallest number between two
numbers
Step1: Start

Step2: Input two numbers, say A and B

Step3: If A<B then small = A

Step4: If B<A then Small = B

Step5: Print Small

Step 6: End

Вам также может понравиться