Professional Documents
Culture Documents
ALGORITHMS
(TIU-UCS-T201)
Presented by
Suvendu Chattaraj
(Department of CSE, TIU, WB)
What we will learn today?
•What is an algorithm?
•Why to study algorithm?
•What is Data Structure? Objective
•Types of data structure
•Relation between data structure and
algorithm
•Algorithm:
•Properties
•Expression
Al'Khwarizmi
What is an algorithm? (780-850, Baghdad,
Iraq) was a
mathematician who
wrote on Hindu-Arabic
numerals. The
word algorithm derives
By definition: from his name
“A finite sequence of instructions, each of which has a clear meaning and can be
performed with a finite amount of effort in a finite length of time”…
Note:
• Algorithm must be precise enough to be understood by human beings
• In order to be executed by a computer, an algorithm is represented as a
program (written in a rigorous formal programming language)
• Specifications such as declaration of variables before use, mentioning of array
size etc. which are normally used in a program can be ignored in algorithm
because human minds are much more flexible in understanding than a
computer
What the following will do?
Read num1, num2, num3
If (num1 < num2)
If(num2 < num3)
Write num1 , num2, num3
Else
If(num3 < num1)
Write num3, num1, num2
Else
Write num1, num3, num2
Else
If(num1 < num3)
Write num2 , num1, num3
Else
If(num3 < num2)
Write num3, num2, num1
Else
Write num2, num3, num1
What the following will do?
Read num1, num2, num3 Algorithm that reads in three numbers and
If (num1 < num2) writes them all in sorted order
If(num2 < num3)
Write num1 , num2, num3 Check:
Else 1. A finite sequence of instructions
If(num3 < num1) 2. each of which has a clear meaning
Write num3, num1, num2
3. can be performed with a finite amount
Else
of effort
Write num1, num3, num2
Else
4. in a finite length of time
If(num1 < num3)
Write num2 , num1, num3 Additionally:
Else 1. precise enough to be understood by
If(num3 < num2) human beings (not necessarily need to
Write num3, num2, num1 be a programmer / coder)
Else 2. Free from programming language
Write num2, num3, num1 specification requirements
Why we need to study algorithms?
Very relevant question because…
Processor speed
Average PC Desktop (1.5 - 2.5 Ghz)
Average Laptop or Macintosh (1.0 Ghz)
1 Ghz = 1 billion cycles per second = 109 cycles per second
2.5 Ghz = 2.5 X 109 cycles per second
Each instruction is associated with certain number of instruction cycles
The clock speed determines the number of instructions that can be
executed by the processor in a second
Typically, a high-end desktop x86 processor can execute billion (109)
instructions per second (Enormous!!!!!)
Let’s check
Computer A Computer B
(Executes 1 billions (109) of instructions (Executes 10 millions (10X 106 = 107)
per seconds) instructions per seconds)
Computer A is 100 times faster than Computer B is 100 times slower than
Computer B Computer A
A sorting algorithm whose running A sorting algorithm whose running
time grows like (2n2) w.r.t the size of time grows like (50 n lg n) w.r.t the size
the input n, is implemented of the input n, is implemented
Resulting code requires Resulting code requires 50 n lg n
2n2 instructions to sort n numbers instructions to sort n numbers
An array of 1 million numbers need to An array of 1 million numbers need to
be sorted; i.e n = 1 million = 106 be sorted; i.e n = 1 million = 106
How much time Computer A would How much time Computer B would
take? take?
How much time Computer A and B would take?
• •