Algorithms and Algorithmic Strategies

Chapter 4 · Tamil Nadu State Board

  • 15 min read
  • 5-question quiz

Every key point below shows the sentence from your textbook it comes from. Book back answers are solved and checked against the book; answers we could not prove are left out. How we check content

In short

This lesson explains how algorithms give clear steps for solving problems and how their efficiency is judged using time and space. It introduces searching and sorting methods, including linear search, binary search, and three sorting techniques. It also shows how dynamic programming reuses results from smaller sub-problems, with Fibonacci numbers as an example. These ideas help students design and compare ways to solve problems.

Written by AI from this lesson. How we check it

Key points

  • An algorithm is a finite sequence of instructions for completing a particular task.
    From your textbook
    “An algorithm is a finite set of instructions to accomplish a particular task.”
  • Algorithms should be clear, correct, effective, and able to finish after a finite number of steps.
    From your textbook
    “Algorithms must terminate after finite number of steps.”
  • Algorithm efficiency is considered in terms of the time and memory space it uses.
    From your textbook
    “the time and space used by the algorithm A are the two main factors”
  • Linear search checks items in order and does not require the list to be ordered.
    From your textbook
    “In this searching algorithm, list need not be ordered.”
  • Binary search repeatedly narrows the search to one side of the middle element, and requires a sorted array.
    From your textbook
    “List of elements in an array must be sorted first for Binary search.”
  • Bubble sort compares adjacent items and swaps them when they are in the wrong order.
    From your textbook
    “It compares each pair of adjacent elements and swaps them if they are in the unsorted order.”
  • Dynamic programming solves problems through smaller overlapping sub-problems whose results can be reused.
    From your textbook
    “The given problem will be divided into smaller overlapping sub-problems.”
Test yourself: 5 questions

Formulas in this chapter

Algorithms and Algorithmic Strategies

Definition

Algorithm
An algorithm is a finite set of step-by-step instructions for completing a particular task.

An algorithm can be implemented in any suitable programming language. The way an algorithm is defined is called an algorithmic strategy. Algorithms can be used to store, manipulate, and retrieve data in data structures.

Common operations on data

  • Search: Find an item in a data structure using methods such as linear or binary search.
  • Sort: Arrange items in a certain order using methods such as bubble sort, insertion sort, or selection sort.
  • Insert: Add item(s) to a data structure.
  • Update: Change existing item(s) in a data structure.
  • Delete: Remove existing item(s) from a data structure.

An algorithm can be used for everyday tasks as well as computer problems. For example, preparing coffee takes inputs, follows a process, and produces coffee as the output.

Coffee preparation algorithm

  • Take a bowl with coffee powder.
  • Boil water and pour it into the bowl.
  • Filter it.
  • Boil milk.
  • Mix sugar and filtered coffee with the boiled milk.
  • Pour the coffee into a cup to serve.

Worked example

Design an algorithm to find the square of a given number and display the result.

Try it first, then show the solution
  1. Start the process.
  2. Get the input x.
  3. Calculate square ← x* x.
  4. Display the result square.
  5. Stop.

Answer: The algorithm finds and displays the square of the input.

Characteristics of an Algorithm

What makes a good algorithm?

  • Input: Zero or more quantities are supplied.
  • Output: At least one quantity is produced.
  • Finiteness: The algorithm ends after a finite number of steps.
  • Definiteness: Every operation is clearly defined. Division by zero and taking the square root of a negative number are unacceptable.
  • Effectiveness: Every instruction can be carried out effectively.
  • Correctness: The algorithm is error free.
  • Simplicity: The algorithm is easy to implement.
  • Unambiguous: Each step and its inputs and outputs are clear and have only one meaning.
  • Feasibility: The algorithm can be carried out with available resources.
  • Portable: The algorithm is generic and independent of a programming language or operating system, and can handle all ranges of inputs.

Remember: For an algorithm to be useful, its steps must be clear, effective, correct, and able to finish.

Algorithm and Program

How an algorithm differs from a program

AlgorithmProgram
Helps solve a problem logically.Expresses an algorithm in a programming language.
Can be implemented using structured or object oriented programming approaches.Is written for a selected language using its specific syntax.
Has no specific writing rules, but guidelines should be followed.Resembles pseudo code and can be implemented in any language.
Is generic and independent of a programming language.Is more specific to a programming language.

Algorithm Analysis and Complexity

Definition

Algorithm analysis
Algorithm analysis estimates the time and space complexities of an algorithm for different input sizes.

Computer resources are limited, so an algorithm's efficiency depends on how it uses time and space. Running time is examined by counting the programming instructions executed for operations.

Two phases of performance analysis

A Priori estimatesA Posteriori testing
Theoretical analysis of an algorithm's performance.Performance measurement using actual statistics, such as running time and resources required for execution.
Efficiency is measured by assuming external factors.Execution statistics are collected after running the algorithm.

Factors that decide algorithm efficiency

  • Time factor: Measured by counting key operations, such as comparisons in a sorting algorithm.
  • Space factor: Measured by the maximum memory space required by the algorithm.
Complexity of an algorithm

f (n)

f (n) gives the running time and/or storage space required by the algorithm; n is the size of the input data.

Remember: Time complexity concerns running time, while space complexity concerns the memory required.

Time and Space Complexity

Definition

Time complexity
Time complexity is the number of steps an algorithm takes to complete a process.

Definition

Space complexity
Space complexity is the amount of memory an algorithm needs to finish running.

Parts of space complexity

PartWhat it includes
Fixed partSpace for data and variables whose sizes do not depend on the problem, such as simple variables and constants.
Variable partSpace for variables whose sizes depend on the problem and its iterations, such as recursion used to calculate factorial of a given value n.

Remember: Time complexity describes steps; space complexity describes memory.

Algorithm Efficiency and Asymptotic Notations

Algorithm efficiency describes the computational resources an algorithm uses. Its efficiency depends on how well it uses time and memory space. To improve efficiency, we try to reduce resource use.

Factors that can affect measured execution time

  • Speed of the machine
  • Compiler and other system Software tools
  • Operating System
  • Programming language used
  • Volume of data required

Definition

Algorithmic strategy
A way of designing an algorithm is called an algorithmic strategy.

A space-time tradeoff solves a problem faster by using more storage, or uses less storage by taking more time. Different algorithms for the same problem can use time and space differently. The preferred algorithm uses less memory and takes less time to produce output.

Asymptotic notations

NotationWhat it describes
Big OThe upper bound, often used for the worst-case.
Big ΩThe lower bound, used for the best-case.
Big ΘA complexity where the lower and upper bounds are equal.

Remember: Big O is the upper bound, Big Ω is the lower bound, and Big Θ applies when both bounds are equal.

Best, Worst, and Average Search Cases

For a search through n values, the algorithm compares the target with list elements one at a time. If the first value matches, the best case needs one comparison and is expressed as O(1). If the search reaches the end, or the target is absent, the worst case needs n comparisons and is expressed as O(n).

Average number of comparisons

(n + 1)/2

The average number of comparisons, using a minimum of 1 and a maximum of n.

The average case efficiency is expressed as O(n).

Linear Search and Binary Search

How the search methods work

Linear searchBinary search
Checks list elements in sequence until it finds the target or reaches the end.Checks the middle element, then continues in the left or right part of the list.
The list need not be ordered.The array must be sorted first.
Also called sequential search.Also called half-interval search; it uses divide-and-conquer and runs in logarithmic time.

Linear search procedure

  • Traverse the array using a for loop.
  • Compare the target with the current value.
  • If they match, display the current index and value. If they do not, move to the next element.
  • If no match is found, display that the search element was not found.

Worked example

Search for 25 in an array with values 10, 12, 20, 25, 30.

Try it first, then show the solution
  1. Check values in sequence from the first element.
  2. The value 25 is at index 3.

Answer: The search returns index 3.

Worked example

Input: values[] = {5, 34, 65, 12, 77, 35}; target = 77

Try it first, then show the solution
  1. Compare the target with values in sequence.
  2. The target 77 is at index 4.

Answer: Output: 4

Worked example

Input: values[] = {101, 392, 1, 54, 32, 22, 90, 93}; target = 200

Try it first, then show the solution
  1. Compare the target with each value in sequence.
  2. No value matches 200.

Answer: Output: -1 (not found)

Middle index for binary search

mid = low + (high - low) / 2

low is the lower index and high is the higher index of the current search range.

Binary search procedure

  • Find the middle element of the sorted array.
  • If it matches the target, return its index.
  • If the target is greater, continue in the right part. If it is less, continue in the left part.
  • Repeat with the smaller range until a match is found or no elements remain.

Worked example

Find 60 in the sorted array 10 20 30 40 50 60 70 80 90 99, with indices 0 to 9.

Try it first, then show the solution
  1. Start with low = 0 and high = 9. Calculate mid = 0 + (9 - 0 ) / 2 = 4 (fractional part ignored). The value at index 4 is 50.
  2. Since 60 is greater than 50, set low = mid + 1. The new mid is 7, and the value there is 80.
  3. Since 60 is less than 80, set high = mid -1. The new mid is 5.
  4. The value at index 5 is 60, so the search finds a match.

Answer: The target 60 is at index 5.

Remember: Linear search works on an unordered list; binary search requires a sorted array.

Bubble Sort

Bubble sort compares neighbouring elements in an array. If a pair is in the wrong order, it swaps them. It keeps making passes until no swaps are needed. Smaller elements move towards the top of the list, but bubble sort is slow compared with insertion sort and other sorting methods.

How bubble sort works

  • Begin at index 0 and compare the current element with the next one.
  • Swap them if the current element is greater than the next element.
  • Otherwise, move to the next pair.
  • Continue through the array and repeat passes until it is sorted.

Worked example

Sort {15, 11, 16, 12, 14, 13} using bubble sort.

Try it first, then show the solution
  1. Compare adjacent elements and swap them when the left element is greater.
  2. Repeat the passes until the elements are in ascending order.

Answer: 11 12 13 14 15 16

Remember: Bubble sort swaps adjacent elements that are in the wrong order.

Selection Sort

Selection sort finds the smallest element that is not yet in its correct position. It swaps that element into the next position in the array. It makes one exchange for each pass through the list.

How selection sort works

  • Search the whole array for the smallest element and place it first.
  • Search the remaining part of the array for the next-smallest element and place it second.
  • Continue with the remaining elements until the array is sorted.

Worked example

Sort {13, 16, 11, 18, 14, 15} using selection sort.

Try it first, then show the solution
  1. Find the smallest element and place it in the first position.
  2. Find the next-smallest element in the remaining part and place it in the second position.
  3. Repeat for the remaining positions.

Answer: The array is sorted in ascending order.

Remember: Selection sort repeatedly selects the next-smallest element and puts it in its correct position.

Insertion Sort

Insertion sort takes elements one at a time and places each in the correct position within the sorted part of the array. It builds the sorted array as it goes and uses n-1 passes.

Insertion sort steps

  • Treat the first element as already sorted.
  • Pick the next element and compare it with elements in the sorted part.
  • Shift larger elements to make room.
  • Insert the picked value in its correct position.
  • Repeat until the list is sorted.

Worked example

Sort 44 16 83 07 67 21 34 45 10 using insertion sort.

Try it first, then show the solution
  1. Take each next value and insert it into the correct position among the values already sorted.
  2. Continue until every value has been inserted.

Answer: 07 10 16 21 34 44 45 67 83

Remember: Insertion sort keeps a sorted part and inserts each new value into its correct place.

Dynamic Programming and Memoization

Definition

Dynamic programming
An algorithmic design method that solves a problem through smaller, overlapping sub-problems and reuses their results.

Dynamic programming is useful when a problem can be divided into similar, overlapping sub-problems. It combines their solutions to find an optimized solution. Before solving a sub-problem again, the algorithm can check whether its result has already been found.

Definition

Memoization
An optimization technique that stores results from earlier function calls and returns a stored result when the same inputs occur again.

Main ideas in dynamic programming

  • Divide the problem into smaller, overlapping sub-problems.
  • Use the results of smaller sub-problems to reach an optimum solution.
  • Use memoization to store and reuse results.

Remember: Dynamic programming saves time by reusing results from overlapping sub-problems.

Fibonacci Series

A Fibonacci series starts with Fib 0 and Fib 1. Each later number is the sum of the two numbers before it.

Fibonacci rule

Fibn = Fibn-1 + Fibn-2

Fibn is the current number, and Fibn-1 and Fibn-2 are the two numbers before it.

Worked example

Generate a Fibonacci series for n value 8.

Try it first, then show the solution
  1. Start with 0 and 1.
  2. Add the two previous numbers to get each next number.

Answer: Fib8 = 0 1 1 2 3 5 8 13

Fibonacci Iterative Algorithm

Steps to generate the series

  • Initialize f0=0 and f1 =1.
  • Print the initial values f0 and f1.
  • Calculate fib ← f0 + f1.
  • Assign f0← f1 and f1← fib.
  • Print the next consecutive value fib.
  • Repeat from the calculation step until the specified number of terms is generated.

Worked example

Generate the Fibonacci series upto 10 digits.

Try it first, then show the solution
  1. Begin with 0 and 1.
  2. Keep adding the two previous values to generate the next value.

Answer: The Fibonacci series is : 0 1 1 2 3 5 8 13 21 34 55

Remember: The next Fibonacci value is always found by adding the two previous values.

Algorithms and Their Efficiency

Definition

Algorithm
An algorithm is a finite set of instructions for completing a particular task.

An algorithm gives step-by-step instructions and helps a programmer develop a program. A program expresses an algorithm in a programming language.

Time and space measures

MeasureMeaning
Time complexityThe number of steps taken to complete the process.
Space complexityThe amount of memory needed to run the algorithm to completion.

Efficiency and analysis terms

  • Algorithm analysis examines the execution or running time of operations.
  • Algorithm efficiency is the number of computational resources used.
  • Big Oh is often used to describe the worst-case of an algorithm.
  • Big Omega is used to describe the lower bound, described as the best way to solve the space complexity.
  • Asymptotic Notations use meaningful statements about time and space complexity.
  • A space-time or time-memory tradeoff solves a problem in less time by using more storage space.

Definition

Algorithmic strategy
An algorithmic strategy is a way of designing an algorithm.

Sorting and Dynamic Programming

Sorting algorithms

AlgorithmHow it works
Bubble sortCompares adjacent items and swaps them when they are in the unsorted order.
Selection sortImproves on bubble sort by making only one exchange for each pass through the list.
Insertion sortBuilds the sorted list one item at a time and maintains a sorted sublist in the lower positions.

Definition

Dynamic programming
Dynamic programming is used when a problem's solutions can be viewed as the result of a sequence of decisions.

Remember: In the Fibonacci algorithm, the current values are updated and reused to generate the next value.

Questions students ask

Short, direct answers to what students search for in this chapter.

What is an algorithm?

An algorithm is a finite set of step-by-step instructions for completing a particular task. It has input and output, and it can be implemented in any suitable programming language.

What are the characteristics of an algorithm?

An algorithm should have input and output. It should be finite, definite, effective, correct, simple, unambiguous, feasible, portable, and independent of programming code.

What is the difference between A Priori estimates and A Posteriori testing?

A Priori estimates are theoretical performance analyses that measure efficiency while assuming external factors. A Posteriori testing measures performance using actual statistics collected during execution, such as running time and resources required.

What is the time and space complexity of an algorithm?

Time complexity describes the running time of an algorithm in relation to the input size. Space complexity describes the storage space it requires. Time can be measured by counting key operations, and space by the maximum memory needed.

What is the difference between time complexity and space complexity?

Time complexity is the number of steps an algorithm takes to finish a process. Space complexity is the amount of memory it needs to finish. One describes steps, while the other describes memory use.

What is a space-time tradeoff in an algorithm?

A space-time tradeoff means choosing between time and memory. An algorithm may use more storage to solve a problem faster, or use less storage while taking more time to run.

Show 11 more questions

What do Big O, Big Ω, and Big Θ mean?

Big O describes an upper bound and is often used for the worst-case. Big Ω describes a lower bound and is used for the best-case. Big Θ describes a complexity where the lower and upper bounds are equal.

What is the difference between linear search and binary search?

Linear search checks values one at a time and does not require an ordered list. Binary search checks the middle value and continues in the left or right part, so its array must be sorted first.

What is the average number of comparisons when searching n values?

The average number of comparisons is (n + 1)/2, using a minimum of 1 comparison and a maximum of n. The average case efficiency is expressed as O(n).

How does bubble sort work?

Bubble sort compares each pair of adjacent elements in an array. It swaps a pair when the left element is greater than the right one. It repeats passes through the array until no swaps are needed and the values are sorted.

What is the difference between bubble sort and selection sort?

Bubble sort compares adjacent elements and swaps them when they are in the wrong order. Selection sort searches the unsorted part for the smallest element and swaps it into the next correct position. Selection sort makes one exchange for each pass through the list.

How does insertion sort place elements in order?

Insertion sort starts with the first element as a sorted part. It picks each next element, shifts larger values in the sorted part, and inserts the picked value in its correct position. It repeats until the whole array is sorted.

What is dynamic programming?

Dynamic programming is a way to solve problems by dividing them into smaller, overlapping sub-problems. It reuses the results of those sub-problems and combines them to find an optimized solution.

What is memoization in dynamic programming?

Memoization stores the results of previous function calls. If the same inputs occur again, the stored result is returned instead of finding it again. This helps speed up computer programs.

What is the rule for generating a Fibonacci series?

A Fibonacci series starts with Fib 0 and Fib 1. Each later number is found by adding the two numbers before it. The rule is Fibn = Fibn-1 + Fibn-2.

How does the iterative Fibonacci algorithm work?

It initializes f0=0 and f1 =1, then prints both values. It adds them to calculate fib, assigns f0← f1 and f1← fib, and prints the new value. It repeats these steps until the specified number of terms is generated.

How do bubble sort, selection sort and insertion sort work?

Bubble sort compares adjacent items and swaps them when they are in the unsorted order. Selection sort makes one exchange for each pass. Insertion sort builds the sorted list one item at a time while keeping a sorted sublist in the lower positions.

Book back answers

18 textbook exercises, solved and checked against the book. Questions we could not prove are left out.

Choose the best answer: (1 Marks)

  1. The word comes from the name of a Persian mathematician Abu Ja’far Mohammed ibn-i Musa al Khowarizmi is called?

    • (A) Flowchart
    • (B) Flow
    • (C) Algorithm
    • (D) Syntax
    Try it, then show the answer

    (C) Algorithm

    The chapter says the word Algorithm comes from the name of the Persian author Abu Jafar Mohammed ibn Musa al Khowarizmi.

    From the book: “The word Algorithm comes from the name of a Persian author”

  2. From the following sorting algorithms which algorithm needs the minimum number of swaps?

    • (A) Bubble sort
    • (B) Insertion sort
    • (C) Selection sort
    • (D) All the above
    Try it, then show the answer

    (C) Selection sort

    Selection sort makes only one exchange for every pass through the list, improving on bubble sort. The chapter does not give a swap comparison with insertion sort, but describes selection sort's minimum exchanges.

    From the book: “making only one exchange for every pass through the list”

  3. Two main measures for the efficiency of an algorithm are

    • (A) Processor and memory
    • (B) Complexity and capacity
    • (C) Time and space
    • (D) Data and space
    Try it, then show the answer

    (C) Time and space

    The chapter identifies time and space complexity as the two main factors that decide algorithm efficiency.

    From the book: “the time and space used by the algorithm A are the two main factors”

  4. The algorithm that yields expected output for a valid input in called as

    • (A) Algorithmic solution
    • (B) Algorithmic outcomes
    • (C) Algorithmic problem
    • (D) Algorithmic coding
    Try it, then show the answer

    (A) Algorithmic solution

    The chapter defines an algorithm yielding expected output for valid input as an algorithmic solution.

    From the book: “An algorithm that yields expected output for a valid input is called an algorithmic solution.”

All 18 book back answers →

Practice

Question 1 of 5Score 0

What does time complexity describe?

Exam questions

Practice questions with model answers. These are not from an official paper.

What is an algorithm, and name two characteristics it should have.2 marks

An algorithm is a finite set of instructions for accomplishing a task. It should, for example, be definite and correct.

How does linear search find an item, and must the list be ordered?2 marks

It compares the search item with each list item in sequence until it finds a match or reaches the end. The list does not need to be ordered.

What is the main difference between linear search and binary search?2 marks

Linear search checks items one by one and can work on an unordered list. Binary search uses the middle item to narrow the search and requires a sorted array.

Explain how dynamic programming uses smaller sub-problems and memoization.5 marks

Dynamic programming is an algorithmic design method used when a problem’s solution can be viewed as the result of a sequence of decisions. It divides a problem into smaller sub-problems and reuses their results.

  1. Divide into sub-problems: The given problem is divided into smaller, overlapping sub-problems.
  2. Solve smaller problems: An optimum solution to the original problem can be achieved using the results of the smaller sub-problems.
  3. Reuse previous results: For each inner sub-problem, the method checks results of previously solved sub-problems.
  4. Combine overlapping solutions: Solutions to overlapping sub-problems are combined to obtain a better solution.
  5. Use memoization: Dynamic algorithms use memoization, which stores results of earlier function calls and returns a cached result when the same inputs occur again.
  6. Avoid repeated work: By reusing stored results, memoization speeds up programs when the same inputs occur again.

Example: In the Fibonacci series, each subsequent number is calculated by adding the two previous numbers: Fibₙ = Fibₙ₋₁ + Fibₙ₋₂.

Thus, dynamic programming solves a problem efficiently by solving smaller overlapping sub-problems and reusing their stored results.

Watch out

  • Do not confuse the search methods: linear search does not require an ordered list, but binary search requires a sorted array.
  • Do not treat time complexity and space complexity as the same measure: one concerns steps, and the other concerns memory.

Still stuck on this lesson?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →