This document discusses algorithms and sorting. It begins by defining an algorithm as a precise set of instructions for solving a problem or completing a task. Algorithms can be expressed as flowcharts or pseudocode. Sorting is introduced as arranging elements in an array in a relevant order like ascending or descending. Selection sort is provided as an example sorting algorithm. It works by iterating through the array, finding the minimum value, and swapping it into the sorted position. The example shows selection sort sorting an array of numbers in ascending order over three iterations.
2. CONTENTS
1. What is Algorithm?
2. Usage of Algorithm.
3. Example of Algorithm with flow chart diagram.
4. What is sorting?
5. One of sorting algorithm(Selection Sort)
3. Previously…
A program MUST be systematically and
properly designed before coding begins.
This design process results in the
construction of an ALGORITHM
4. An algorithm is a precise set of instructions that you
follow to solve a problem or complete a task.
We can say that algorithm is:
● A step‐by‐step problem‐solving procedure.
● A sequence of instruction that tell how to solve
a particular problem.
● A set of instruction for solving a problem,
especially on a computer.
● A computable set of steps to achieve a desired
result.
However, most agree that algorithm has something
to do with defining generalized processes to get
“output” from the “input”.
What is an
Algorithm?
5. Recipes
When you follow a recipe, you follow instructions to complete a task.
The instructions are a step-by-step procedure that need to be followed for your recipe to be
successful.
6. From Algorithm to Program
● Both are sets of instructions on how to do a task;
● Algorithm:
○ talking to humans, easy to understand
○ in plain (English) language
● Program:
○ talking to computer (compiler)
○ can be regarded as a “formal expression” of an algorithm
7. From Algorithm to Program
PROGRAM
ALGORITHM
a sequence of instructions
describing how to do a task (or
process)
a set of instructions which
are commands to
computers to perform
specific operations on data
8. Algorithms in Technology
You use an algorithm to tell a computer what to do.
Everything you can do on a computer is able to happen because an
algorithm has been written for it.
Clicking a mouse, opening a word document and playing a game are
all examples of things that have algorithms.
Digital devices all follow algorithms in order for them to work.
9. Flowchart Symbol and Description
Name Symbol Description
Lozenge Indicates the starting or ending of flowchart
(start / stop : terminal )
Connector Used to connect break in the flowchart
Parallelelogram Use for input and output operation (data)
Rectangle Indicates any interval operation (process)
Diamond Use for asking questions that can have
either TRUE of FALSE / YES or NO
(decision / selection)
Module Sub solution to solve a part of entire problem
Control flow Shows flow direction
10. Flowchart Example
Design an algorithm to find the area of a
rectangle
The formulas: area = length * width
Input Process Output
Input variable:
length
width
Processing item:
area
Formula:
area = length x width
Step / Solution algorithm:
get input
calculate area
display output
Output:
area
Start
length, width
calculate area
area
End
11. Pseudo Code Example
Start
Read length
Read width
Calculate area of a rectangle
Display area of a rectangle
End
Design an algorithm to find the area of a rectangle
The formulas: area = length * width
Input Process Output
Input
variable:
length
width
Processing item:
area
Formula:
area = length x width
Step / Solution
algorithm:
get input
calculate area
display output
Output:
area
Start
Prompt the user to enter a length of a rectangle
Prompt the user to enter a width of a rectangle
Calculate the area of a rectangle
Display the area of a rectangle
End
Start
Input length
Input width
Calculate area of a rectangle
Output area of a rectangle
End
OR
OR
13. Sorting
● Sorting means arranging the elements of an array so that they are placed in
some relevant order which may be either ascending or descending.
● Can be used as a first step for searching the data.
● That is, if A is an array, then the elements of A are arranged in a sorted order
(ascending order) in such a way that A[0] < A[1] < A[2] < … < A[N].
● The practical considerations for different sorting techniques would be:
○ Number of sort key comparisons that will be performed.
○ Number of times the records in the list will be moved.
○ Best, average and worst case performance.
○ Stability of the sorting algorithm where stability means that equivalent
elements or records retain their relative positions even after sorting is
15. • Selection sort is one of the easiest approaches to sorting.
• It is inspired from the way in which we sort things out in day to
day life.
• It is an in-place sorting algorithm because it uses no auxiliary
data structures while sorting.
Selection Sort
18. Selection Sort
● Suppose the name of the array is A and it has four
elements with the following values:
4 19 1 3
To sort this array in ascending order, n-1,
i.e. three iterations will be required.
19. Selection Sort
● Iteration-1
The array is scanned starting from the first to the last element and the element that has the
smallest value is selected. The smallest value is 1 at location 3. The address of element that
has the smallest value is noted and the selected value is interchanged with the first element
i.e.
A[1] and A[3] are swapped
4 19 1 3
1 19 4 3
20. Selection Sort
● Iteration-2
The array is scanned starting from the second to the last element and the element that has
the smallest value is selected. The smallest value is 3 at location 4. The address of element
that has the smallest value is noted. The selected value is interchanged with the second
element i.e.
A[2] and A[4] are swapped
1 19 4 3
1 3 4 19
21. Selection Sort
● Iteration-3
The array is scanned starting from the third to the last element and the element that has the
smallest value is selected. The smallest value is 4 at location 3. The address of element that
has the smallest value is noted. The selected value is interchanged with the third element
i.e.
A[3] and A[3] are swapped
1 3 4 19
1 3 4 19