ALGORITHM
THE WORD ALGORITHMMEANS” A SET OF FINITE RULES OR INSTRUCTIONS TO BE FOLLOWED IN CALCULATIONS OR OTHER
PROBLEM-SOLVING OPERATIONS”
OR
” A PROCEDURE FOR SOLVING A MATHEMATICAL PROBLEM IN A FINITE NUMBER OF STEPS THAT FREQUENTLY INVOLVES
RECURSIVE OPERATIONS”.
THEREFORE ALGORITHM REFERS TO A SEQUENCE OF FINITE STEPS TO SOLVE A PARTICULAR PROBLEM.
USE OF ALGORITHM
ALGORITHMSPLAY A CRUCIAL ROLE IN VARIOUS FIELDS AND HAVE MANY APPLICATIONS. SOME OF THE KEY
AREAS WHERE ALGORITHMS ARE USED INCLUDE:
1. COMPUTER SCIENCE: ALGORITHMS FORM THE BASIS OF COMPUTER PROGRAMMING AND ARE USED
TO SOLVE PROBLEMS RANGING FROM SIMPLE SORTING AND SEARCHING TO COMPLEX TASKS SUCH AS
ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING.
2. MATHEMATICS: ALGORITHMS ARE USED TO SOLVE MATHEMATICAL PROBLEMS, SUCH AS FINDING THE
OPTIMAL SOLUTION TO A SYSTEM OF LINEAR EQUATIONS OR FINDING THE SHORTEST PATH IN A GRAPH.
3. OPERATIONS RESEARCH: ALGORITHMS ARE USED TO OPTIMIZE AND MAKE DECISIONS IN FIELDS
SUCH AS TRANSPORTATION, LOGISTICS, AND RESOURCE ALLOCATION.
4. ARTIFICIAL INTELLIGENCE: ALGORITHMS ARE THE FOUNDATION OF ARTIFICIAL INTELLIGENCE AND
MACHINE LEARNING, AND ARE USED TO DEVELOP INTELLIGENT SYSTEMS THAT CAN PERFORM TASKS SUCH
AS IMAGE RECOGNITION, NATURAL LANGUAGE PROCESSING, AND DECISION-MAKING.
5. DATA SCIENCE: ALGORITHMS ARE USED TO ANALYZE, PROCESS, AND EXTRACT INSIGHTS FROM
LARGE AMOUNTS OF DATA IN FIELDS SUCH AS MARKETING, FINANCE, AND HEALTHCARE.
THESE ARE JUST A FEW EXAMPLES OF THE MANY APPLICATIONS OF ALGORITHMS. THE USE OF ALGORITHMS
IS CONTINUALLY EXPANDING AS NEW TECHNOLOGIES AND FIELDS EMERGE, MAKING IT A VITAL COMPONENT
OF MODERN SOCIETY.
4.
NEED OF ALGORITHM
►Algorithms are necessary for solving complex problems efficiently
and effectively.
► They help to automate processes and make them more reliable,
faster, and easier to perform.
► Algorithms also enable computers to perform tasks that would be
difficult or impossible for humans to do manually.
► They are used in various fields such as mathematics, computer
science, engineering, finance, and many others to optimize
processes, analyze data, make predictions, and provide solutions to
problems.
5.
What are theCharacteristics of an Algorithm?
► Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps should be
clear in all aspects and must lead to only one meaning.
► Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs. It
may or may not take input.
► Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it
should be well-defined as well. It should produce at least 1 output.
► Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.
► Feasible: The algorithm must be simple, generic, and practical, such that it can be executed
with the available resources. It must not contain some future technology or anything.
► Language Independent: The Algorithm designed must be language-independent, i.e. it must
be just plain instructions that can be implemented in any language, and yet the output will be
the same, as expected.
6.
CONTINUE……..summary
Characteristics of anAlgorithm
1. Finiteness
○ The algorithm must always terminate after a finite number of steps.
2. Definiteness
○ Each step must be precisely and unambiguously defined.
3. Input
○ An algorithm should have zero or more inputs provided externally.
4. Output
○ It should produce at least one output or result.
5. Effectiveness
○ All operations must be basic enough to be performed exactly and in finite time.
6. Generality
○ The algorithm should be applicable to a set of inputs, not just a single specific input.
7.
Steps for Writingan Algorithm
1. Understand the Problem
○ Clearly define what needs to be solved or achieved.
2. Define Inputs and Outputs
○ Identify what inputs are required and what outputs are expected.
3. Break Down the Problem
○ Divide the problem into smaller, manageable parts or steps.
4. Develop Step-by-Step Instructions
○ Write clear, sequential steps to solve the problem logically.
5. Use Control Structures
○ Incorporate decisions (IF...ELSE), loops (FOR, WHILE), and other control flow tools.
6. Check for Correctness
○ Ensure the algorithm covers all possible cases and terminates correctly.
7. Optimize if Needed
○ Refine steps to make the algorithm efficient in terms of time and space.
Rewrite the procedure….
►Step1: get dressed and go to market[start]
► Step2:check your wallet for money
► Step 3:if there is no money in the wallet, replenish it
► Step 4:go to shop
► Step5:ask for your favorite brand of pen
► Step 6:if pen is not available go to step 10
► Step 7:give the money to the shopkeeper
► Step 8:keep the purchased pen safely
► Step 9:go back to home[exit]
► step 10 :Ask for any other brand of pen, if you are satisfied with that particular brand go
to step 7 else go to step 9
What is Pseudocode?
Pseudocodeis a simplified, half-English, half-code outline of a computer program. It uses plain
language and basic programming structures to describe the logic of an algorithm without the strict
syntax of actual programming languages.
13.
Pseudocode In algorithm
►A programmer starts with an algorithm before writing code because it helps break
down complex problems into smaller, more manageable pieces. Even though it is
not a code, it helps identify issues and errors before the code is written, is known as
algorithm_pseudocode.
► Reminder
► In computer science pseudocode in algorithm and mnemonics in assembly
language are differ –
► Mneumonics are short ,memorable abbreviation used to represent instructions or
operations in assembly language
14.
Example -Pseudocodes
Porfit orLoss
► Ask for cost price
► Program starts
► Ask for the selling price
► If ( selling price > cost price )then
► Profit = selling price- cost price
► Display profit
► Else loss = cost price- selling price
► Display loss
► Program ends
Even or odd
► Ask for the number
► Calculate remainder on dividing the
number by 2
► If ( remainder = 0 ) then
► Display ‘number is ’ an even number
► Else
► Display ‘number is’ an odd number
► Program ends
15.
Guidelines for WritingPseudocode
1. Use Plain Language:
○ Write in simple English to explain the logic clearly.
○ Avoid specific programming syntax or code.
2. Use Standard Control Structures:
○ Use common programming terms like IF, ELSE, WHILE, FOR, END, etc.
3. Be Clear and Concise:
○ Each step should represent a logical action.
○ Avoid unnecessary detail.
4. Follow Logical Sequence:
○ The flow of the pseudocode should match the logical steps of the algorithm.
5. Indentation:
○ Use proper indentation to show hierarchy or nested structures (e.g., inside IF or LOOP).
6. Consistency:
○ Use consistent naming for variables and operations.
7. No Syntax Rules:
○ Don't worry about semicolons, brackets, or language-specific rules.
16.
Benefits of Pseudocode
BenefitsDescription
1. Easy to Understand Written in plain language, it's readable by non-programmers.
2. Focuses on Logic Helps focus on the logic and structure before coding.
3. Improves Planning Encourages step-by-step design and thinking.
4. Language-Independent Can be translated into any programming language.
5. Good for Collaboration Team members from different backgrounds can understand it
6. Aids in Debugging Helps visualize the program flow before actual coding.
17.
Limitations of Pseudocode
LimitationDescription
1. No Standard Syntax Lack of formal rules can lead to inconsistent formats.
2. Not Executable You can’t run or test pseudocode on a computer.
3. May Be Too Abstract Over-simplification can omit important implementation detail
4. Interpretation Varies Different readers may interpret pseudocode differently.
5. No Error Checking Can't detect errors like a compiler would in real code.
18.
SUMMARY OF PSEUDOCODE
AspectDetails
Purpose Describe logic of an algorithm before coding
Best Use Case Early stages of software development or algorithm design
Strength Simple, clear, focuses on logic
Weakness Not runnable or standardized
19.
Flow chart
► Flowchartsare graphical representations of data, algorithms, or
processes, providing a visual approach to understanding code.
► Flowcharts illustrate step-by-step solutions to problems, making them
useful for beginner programmers.
► Flowcharts help in debugging and troubleshooting issues.
► Flowchart consists of sequentially arranged boxes that denote the
process flow.
► .
Uses of Flowchartsin Computer Programming/Algorithms
► The following are the uses of a Flowchart:
► It is a pictorial representation of an algorithm that increases the readability
of the program.
► Complex programs can be drawn in a simple way using a Flowchart.
► It helps team members get an insight into the process and use this
knowledge to collect data, detect problems, develop software, etc.
► A Flowchart is a basic step for designing a new process or adding extra
features.
► Communication with other people becomes easy by drawing Flowcharts
and sharing them.
23.
Advantages of Flowchart
►It is the most efficient way of communicating the logic of
the system.
► It acts as a guide for a blueprint during the program
design.
► It also helps in the debugging process.
► Using Flowcharts we can easily analyze the programs.
► Flowcharts are good for documentation.
24.
Disadvantages of Flowchart
►Flowchartsare challenging to draw for
large and complex programs.
►It does not contain the proper amount of
details.
►Flowcharts are very difficult to reproduce.
►Flowcharts are very difficult to modify.
25.
NUMBER SYSTEM
In digitalcomputer data and instruction are stored using binary code represented by
binarydigit s 1 and 0 called bits
The data may contain digits alphabets or special character which are then converted to
bits all operations are performed using binary bits
classified in to 2 types:
Non-positional number system
ex:- Roman numbers can not be used effectively to perform arithmetic operation
positional number system
ex:-decimal , octal , hexadecimal
26.
continue…
The base orradix of a number system is the total number of digits present in that
system
ex:
The number in decimal number system include (0 to 9)
ex: number -5
represent as 5(10)
read as
5 base 10
The number in binary number system include (0 ,1)
ex: Number- 001
represent as 001(2)
read as
001 base 2
27.
Positional number systemcontinue…
1. Decimal number system
2. Number system in our daily life
3. It has 10 numbers from 0 to 9 :hence its radix number is 10
4. The positional values are expressed in power of 10
5. Ex: 542.76(10) its equivalent values as
6. 5*102
+4*101
+2*100
+7*10-1
+6*10-2
Binary number system
►Binary number base value is 2
► The positional values are expressed in power of 2
► Ex: 11011.101(2)
► The positional values are expressed as
► 1*2 4
+ 1*2 3
+ 0*2 2
+ 1*21
+ 1*20
+ 1*2-1
+ 0*2-2
+ 1*2-3
30.
3.Octal number system
PROBLEMIN BINARY NUMBER:
Binary Number Generate Along Sequence Of 0’ S And 1’ S
How To Over Come This Problem ?
Ans: By Using Octal And Hexadecimal Number System
31.
3.Octal number systemcontinue..
► It has 8 digits 0 to 7
► Radix is 8
► Positional values are expressed in power of 8
32.
4.Hexa decimal numbersystem
► This number system has 16 digits 0 to 9 and A,B,C,D,E,F equivalents 10,11,12,13,14 and 15
respectively
► The radix is 16
► Ex: 5AF.D (16)
► Which can be represented as 5*162
+A*161
+F*160
+D*16-1
A=10
F=15
D=13
final answer 1455.8125 (10)
33.
Summery
Number system Base(Radix)Used digits
Example
Binary 2 0,1 11110000(2)
Octal 8 0,1,2,3,4,5,6,7 (360)(8)
Decimal 10 0,1,2,3,4,5,6,7,8,9 (240)(10)
Hexadecimal 16 0,1,2,3,4,5,6,7,8,9, 901(16)
10,11,12,13,14,15 respectively A,B,C,D,E,F (F0)
(16)
34.
DECIMAL TO OTHER
DecimalNumber System to Other Base
To convert Number system from Decimal Number System to Any Other Base is
quite easy;
you have to follow just two steps:
A) Divide the Number (Decimal Number) by the base of target base system (in
which you want to convert the number: Binary (2), octal (8) and Hexadecimal
(16)).
B) Write the remainder from step 1 as a Least Signification Bit (LSB) (TOP)
to Step last as a Most Significant Bit (MSB)(BOTTOM)
35.
Number System conversionof all types
► 1.Decimal to Binary
► Step 1 : Divide the given decimal number by 2
► Step 2 : Note the quotient and reminder
► Step 3 : Repeat the step 1 and step 2 until quotient becomes 0
► Step 4 : First reminder will be LSB and the last reminder is MSB. The equivalent binary number
is written from left to right (i.e MSB to LSB)
36.
(25)10—----->(?)2
Division by 2Quotient Remainder
25 ÷ 2 12 1
12 ÷ 2 6 0
3 ÷ 2 1 1
1 ÷ 2 0 1
LSB =1(TOP)
MSB=1(BOTTOM) collect the remainder from bottom to top
WRITE THE REMAINDER FROM MSB TO LSB ANSWER IS 1101
37.
1.1 Decimal Fractionto Binary
Step 1 : Multiply the decimal fraction by 2, note the carry and the product
Step 2 : Repeat the step 1 until fractional product becomes 0
Step 3 : The first carry will be the MSB and last carry is LSB
Convert Decimal Fraction 4.625 to Binary
Step 1: Convert the integer part (4) to binary by repeated division
● 4 ÷ 2 = 2 remainder 0
● 2 ÷ 2 = 1 remainder 0
● 1 ÷ 2 = 0 remainder 1
● Read remainders bottom to top → 100
next slide…..
38.
step2,step 3
Step 2:Convert the fractional part (0.625) to binary
Multiply the fraction by 2 and note the integer part each time:
Step Multiply Result(Intger part) New Fractional Part
1 0.625 × 2 1 0.25
2 0.25 × 2 0 0.5
3 0.5 × 2 1 0
Write down the integer parts in order: 101
Step 3: Combine integer and fractional binary parts
4.625 (decimal) = 100.101 (binary)
4. Binary toDecimal Conversion
Step 1 : Multiply each bit of the binary number by its positional weight
Step 2 : Add all the product
Ex: 11011.101(2)
The positional values are expressed as
1*2 4
+ 1*2 3
+ 0*2 2
+ 1*21
+ 1*20
+ 1*2-1
+ 0*2-2
+ 1*2-3
42.
5. Binary toOctal
The given binary number are grouped into groups of 3 bits starting from right to
left.
1 ) Eg: 1010111 (2)
1 010 111
1 2 7
Ans: Therefore 1010111 (2)= 127 (8)
2) eg: 0.110111 (2)
110 111
6 7
Ans: Therefore 0.110111 (2)= 0.67 (2)
Adding a leading 0 as the MSB into the whole number and adding 0 trailing 0 as
the LSB into the fraction binary number does not change the value of number
43.
6. Binary toHexadecimal conversion
The given binary number are grouped into groups of 4 bits starting from
right to left.
1) Eg : 1011001 (2)
0101 1001
5 9
Therefore 1011001 (2) = 59 (16)
2) Eg : 0.11010111 (2)
1101 0111
D 7
Therefore 0.11010111 (2) = 0.D7 (16)
10. Hexadecimal toBinary
Replace each digit with 4 BIT binary (Refer table)
Eg : CEBA base (16)
Ans:
C 12 1100
E 14 1110
B 11 1011
A 10 1010
Therefore CEBA (16) = 1100 1110 1011 1010 (2)
50.
11. Hexadecimal toDecimal
Repeated multiplication of a hexadecimal number by its weight
Add all the products
1) Eg : 5AF.D (16)
5*162
+ A*161
+ F*160
+ D*16-1
Therefore 5AF.D (16) = 1455.8125(10)