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

Algorithms introduction

The document provides an overview of algorithms, defining them as step-by-step methods for solving problems, with a focus on finding the largest integer in a list. It discusses the refinement and generalization of algorithms, introduces pseudocode, and outlines basic algorithms such as summation, product, sorting, and searching methods. Additionally, it contrasts iterative and recursive approaches to algorithm design, using factorial calculation as an example.

Uploaded by

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

Algorithms introduction

The document provides an overview of algorithms, defining them as step-by-step methods for solving problems, with a focus on finding the largest integer in a list. It discusses the refinement and generalization of algorithms, introduces pseudocode, and outlines basic algorithms such as summation, product, sorting, and searching methods. Additionally, it contrasts iterative and recursive approaches to algorithm design, using factorial calculation as an example.

Uploaded by

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

Algorithms

!1
Informal definition

Algorithm: a step-by-step method for solving a


problem or doing a task.

!2
Informal definition of an algorithm used in a computer
Example
• We want to develop an algorithm for finding the largest
integer among a list of positive integers.
• The algorithm should find the largest integer among a
list of any values (for example 5, 1000, 10000,
1000000).
• The algorithm should be general and not depend on the
number of integers.

!3
• To solve this problem, we need an intuitive approach.
First use a small number of integers (for example,
five), then extend the solution to any number of
integers.
• Figure shows one way to solve this problem. We call
the algorithm FindLargest.
• The algorithm receives a list of five integers as input
and gives the largest integer as output

!4
Finding the largest integer among five integers
!5
Defining actions
Figure does not show what should be done in each step. We
can modify the figure to show more details.

!6
Defining actions in FindLargest algorithm
Refinement
• This algorithm needs refinement to be acceptable to the
programming community.
• There are two problems. First, the action in the first step is
different than those for the other steps. Second, the wording
is not the same in steps 2 to 5.
• We can easily redefine the algorithm to remove these two
inconveniences by changing the wording in steps 2 to 5 to
“If the current integer is greater than Largest, set Largest to
the current integer.”
• The reason that the first step is different than the other steps
is because Largest is not initialized. If we initialize Largest
to −∞ (minus infinity), then the first step can be the same as
the other steps, so we add a new step, calling it step 0 to
!7
show that it should be done before processing any integers.
!8
FindLargest refined
Generalization
• We want to find the largest of n positive integers, where n
can be 1000, 1,000,000, or more. Of course, we can follow
and repeat each step. But if we change the algorithm to a
program, then we need to actually type the actions for n
steps!
• There is a better way to do this. We can tell the computer to
repeat the steps n times. We now include this feature in our
pictorial algorithm.

!9
!10 Generalization of FindLargest
Pseudocode
Pseudocode is an English-language-like representation of an
algorithm. There is no standard for pseudocode—some
people use a lot of detail, others use less. Some use a code
that is close to English, while others use a syntax like the
Pascal programming language.

!11
Pseudocode for three constructs
!12
Write an algorithm in pseudocode that finds the sum of two
integers.

!13
Write an algorithm to change a numeric grade to a pass/no pass
grade.

!14
Write an algorithm to change a numeric grade (integer) to a letter
grade.

!15
Write an algorithm to find the largest of a set of integers. We do
not know the number of integers.

!16
Write an algorithm to find the largest of the first 1000 integers in a
set of integers.

!17
A MORE FORMAL DEFINITION

Now that we have discussed the concept of an algorithm


and shown its representation, here is a more formal
definition.

i
Algorithm:
An ordered set of unambiguous steps that produces a
result and terminates in a finite time.

!18
Ordered set
An algorithm must be a well-defined, ordered set of
instructions.
Unambiguous steps
Each step in an algorithm must be clearly and unambiguously
defined. If one step is to add two integers, we must define
both “integers” as well as the “add” operation: we cannot for
example use the same symbol to mean addition in one place
and multiplication somewhere else.

!19
Produce a result
An algorithm must produce a result, otherwise it is useless.
The result can be data returned to the calling algorithm, or
some other effect (for example, printing).

Terminate in a finite time


An algorithm must terminate (halt). If it does not (that is, it
has an infinite loop), we have not created an algorithm.

!20
BASIC ALGORITHMS

Several algorithms are used in computer science so


prevalently that they are considered “basic”. We discuss
the most common here. This discussion is very general:
implementation depends on the language.

!21
Summation
One commonly used algorithm in computer science is
summation. We can add two or three integers very easily, but
how can we add many integers? The solution is simple: we
use the add operator in a loop.

A summation algorithm has three logical parts:

1. Initialization of the sum at the beginning.


2. The loop, which in each iteration adds a new integer to the
sum.
3. Return of the result after exiting from the loop.
!22
Summation algorithm
!23
Product
Another common algorithm is finding the product of a list of
integers. The solution is simple: use the multiplication
operator in a loop.

A product algorithm has three logical parts:


1. Initialization of the product at the beginning.
2. The loop, which in each iteration multiplies a new integer
with the product.
3. Return of the result after exiting from the loop.

!24
Product algorithm
!25
Smallest and largest
• We discussed the algorithm for finding the largest among a
list of integers at the beginning of this chapter. The idea was
to write a decision construct to find the larger of two
integers. If we put this construct in a loop, we can find the
largest of a list of integers.

• Finding the smallest integer among a list of integers is


similar, with two minor differences. First, we use a decision
construct to find the smaller of two integers. Second, we
initialize with a very large integer instead of a very small
one.

!26
Sorting
• One of the most common applications in computer science
is sorting, which is the process by which data is arranged
according to its values.

• There are several techniques for sorting that include:


selection sort, bubble sort and insertion sort. These three
sorting algorithms are the foundation of faster sorting
algorithms used in computer science today.

!27
Selection sorts
In a selection sort, the list to be sorted is divided into two
sublists—sorted and unsorted—which are separated by an
imaginary wall. We find the smallest element from the
unsorted sublist and swap it with the element at the beginning
of the unsorted sublist. After each selection and swap, the
imaginary wall between the two sublists moves one element
ahead.

!28
Selection sort
!29
Example of selection sort
!30
Selection sort algorithm
Bubble sorts
In the bubble sort method, the list to be sorted is also divided
into two sublists—sorted and unsorted. The smallest element
is bubbled up from the unsorted sublist and moved to the
sorted sublist. After the smallest element has been moved to
the sorted list, the wall moves one element ahead.

Bubble sort
!31
Example of bubble sort
!32
Insertion sorts
The insertion sort algorithm is one of the most common
sorting techniques, and it is often used by card players. Each
card a player picks up is inserted into the proper place in their
hand of cards to maintain a particular sequence.

Insertion sort
!33
!34
Example of insertion sort
Searching
Another common algorithm in computer science is searching,
which is the process of finding the location of a target among
a list of objects. In the case of a list, searching means that
given a value, we want to find the location of the first
element in the list that contains that value. There are two
basic searches for lists: sequential search and binary
search. Sequential search can be used to locate an item in any
list, whereas binary search requires the list first to be sorted.

!35
Sequential search
Sequential search is used if the list to be searched is not
ordered. Generally, we use this technique only for small lists,
or lists that are not searched often. In other cases, the best
approach is to first sort the list and then search it using the
binary search discussed later.

In a sequential search, we start searching for the target from


the beginning of the list. We continue until we either find the
target or reach the end of the list.

!36
!37 An example of a sequential search
Binary search
The sequential search algorithm is very slow. If we have a list
of a million elements, we must do a million comparisons in
the worst case. If the list is not sorted, this is the only
solution. If the list is sorted, however, we can use a more
efficient algorithm called binary search. Generally speaking,
programmers use a binary search when a list is large.
A binary search starts by testing the data in the element
at the middle of the list. This determines whether the target is
in the first half or the second half of the list. If it is in the first
half, there is no need to further check the second half. If it is
in the second half, there is no need to further check the first
half. In other words, we eliminate half the list from further
consideration.
!38
!39 Example of a binary search
RECURSION

In general, there are two approaches to writing


algorithms for solving a problem. One uses iteration,
the other uses recursion. Recursion is a process in
which an algorithm calls itself.

!40
Iterative definition
To study a simple example, consider the calculation of a
factorial. The factorial of a integer is the product of the
integral values from 1 to the integer. The definition is
iterative (Figure 8.21). An algorithm is iterative whenever the
definition does not involve the algorithm itself.

!41
Iterative definition of factorial
Recursive definition
An algorithm is defined recursively whenever the algorithm
appears within the definition itself. For example, the factorial
function can be defined recursively as shown in Figure 8.22.

!42 Figure 8.22 Recursive definition of factorial


Tracing the recursive solution to the factorial problem
!43
Iterative solution
This solution usually involves a loop.

!44
Recursive solution
The recursive solution does not need a loop, as the recursion
concept itself involves repetition.

!45

You might also like