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

Algorithms and Programming Techniques IGCSE

The document provides an overview of algorithms, including their definition, characteristics, and representations such as flowcharts and pseudocode. It covers sorting algorithms like Bubble Sort and Insertion Sort, as well as searching algorithms like Linear Search and Binary Search, detailing their time complexities and best use cases. Additionally, it discusses programming techniques, algorithm efficiency, and recursion, highlighting key concepts like variables, data types, operators, conditional statements, and loops.

Uploaded by

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

Algorithms and Programming Techniques IGCSE

The document provides an overview of algorithms, including their definition, characteristics, and representations such as flowcharts and pseudocode. It covers sorting algorithms like Bubble Sort and Insertion Sort, as well as searching algorithms like Linear Search and Binary Search, detailing their time complexities and best use cases. Additionally, it discusses programming techniques, algorithm efficiency, and recursion, highlighting key concepts like variables, data types, operators, conditional statements, and loops.

Uploaded by

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

7.

Algorithms and Programming Techniques


7.1 What is an Algorithm?
An algorithm is a step-by-step procedure or set of rules to solve a problem or
perform a task. It is a finite sequence of well-defined instructions used to solve
computational problems.
 Characteristics of a Good Algorithm:
o Finiteness: It should terminate after a finite number of steps.

o Definiteness: Every step must be clear and unambiguous.

o Effectiveness: The steps should be basic enough to be performed.

o Input: The algorithm should receive inputs (if applicable).

o Output: The algorithm should produce outputs.

7.2 Representation of Algorithms


7.2.1 Flowcharts
A flowchart is a diagram that represents an algorithm or process. It uses
different shapes to denote different types of actions or steps.
 Common Flowchart Symbols:
o Oval: Represents the start or end of a process.

o Rectangle: Represents a process or operation (e.g., calculation or


assignment).
o Parallelogram: Represents input or output.

o Diamond: Represents a decision (yes/no or true/false).

o Arrow: Indicates the flow of the algorithm.

Example:
Start
|
Read Input
|
If number > 10
/ \
Yes No
| |
Print End
7.2.2 Pseudocode
Pseudocode is a method of designing algorithms using a structured, yet simple,
language that resembles programming syntax. It does not require strict rules but
should be understandable by humans.
 Example of Pseudocode:
Algorithm to find the largest number
Start
Read number1, number2, number3
If number1 > number2 AND number1 > number3
Print "number1 is largest"
Else if number2 > number1 AND number2 > number3
Print "number2 is largest"
Else
Print "number3 is largest"
End

7.3 Sorting Algorithms


Sorting algorithms are used to arrange data in a specific order, such as
ascending or descending. Here are two commonly used sorting algorithms:
7.3.1 Bubble Sort
Bubble Sort repeatedly steps through the list, compares adjacent elements,
and swaps them if they are in the wrong order. This process repeats until the list
is sorted.
 Time Complexity: O(n²)
 Best Use: Small datasets.
Example:
 Compare 3 and 1. Since 3 > 1, swap them.
 Compare 3 and 4. No swap needed.
 Continue this process until the entire list is sorted.
7.3.2 Insertion Sort
Insertion Sort builds the final sorted array one item at a time. It picks elements
from the unsorted part of the array and inserts them in their correct position.
 Time Complexity: O(n²) in the worst case, O(n) in the best case.
 Best Use: Small datasets or nearly sorted data.
Example:
 Start with the second element and compare it to the first. If it is smaller,
shift the first element and place the second element in the correct
position.
 Repeat this process for the remaining elements.

7.4 Searching Algorithms


Searching algorithms are used to find a specific element within a data structure.
The two common searching algorithms are:
7.4.1 Linear Search
Linear Search checks each element of the list sequentially until the target
element is found or the list ends.
 Time Complexity: O(n)
 Best Use: Unsorted or small datasets.
Example:
 Start from the first element, compare it with the target. If it matches,
return the index. Otherwise, continue with the next element.
7.4.2 Binary Search
Binary Search works on sorted lists. It repeatedly divides the search interval in
half. If the target is smaller than the middle element, it narrows the search to the
left half; otherwise, to the right half.
 Time Complexity: O(log n)
 Best Use: Sorted datasets.
Example:
 If the target is less than the middle value, search the left half of the list; if
it's greater, search the right half.

7.5 Programming Techniques


7.5.1 Variables and Data Types
In programming, variables are used to store data. Different data types specify
the type of data a variable can hold.
 Common Data Types:
o Integer (int): Stores whole numbers.

o Float: Stores decimal numbers.


o String: Stores text.

o Boolean (bool): Stores true or false values.

7.5.2 Input and Output


Programs need to accept input from users and display output.
 Input: Data can be read from the user using commands like input() in
Python.
 Output: Data can be printed to the screen using commands like print().
7.5.3 Operators
Operators perform operations on variables and values.
 Arithmetic Operators: +, -, *, /, %
 Relational Operators: ==, !=, <, >, <=, >=
 Logical Operators: AND, OR, NOT
7.5.4 Conditional Statements
Conditional statements are used to make decisions in a program.
 If-Else Statements:
If condition is true:
Execute this block
Else:
Execute that block

 Example:
If age >= 18:
Print "Adult"
Else:
Print "Minor"

7.5.5 Loops
Loops allow a block of code to be executed repeatedly.
 For Loop: Used to iterate over a range or a collection.
o Example: for i in range(5): print(i)

 While Loop: Used to repeat a block of code as long as a condition is true.


o Example: while x < 10: print(x); x += 1
7.6 Algorithm Efficiency
The efficiency of an algorithm is measured in terms of the time and space it
takes to complete its task.
 Time Complexity: Describes how the runtime of an algorithm changes
with the size of the input (e.g., O(n), O(log n)).
 Space Complexity: Describes how much memory is used by an
algorithm relative to the input size.
Big O Notation:
Big O notation is used to describe the upper bound of an algorithm’s
performance.
 O(1): Constant time, does not change with input size.
 O(n): Linear time, increases directly with input size.
 O(log n): Logarithmic time, increases slowly with input size.
 O(n²): Quadratic time, increases exponentially with input size.

7.7 Recursion
Recursion is when a function calls itself in order to solve a problem. It is useful
for problems that can be broken down into smaller, similar problems.
 Base Case: The condition that stops the recursion.
 Recursive Case: The condition that leads to another function call.
Example:
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)

You might also like