8a Algorithms Intro
8a Algorithms Intro
1
Informal definition
2
Informal definition of an algorithm used in a computer
A MORE FORMAL DEFINITION
i
Algorithm:
An ordered set of unambiguous steps that produces a
result and terminates in a finite time.
3
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.
4
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).
5
BASIC ALGORITHMS
6
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.
9
Product algorithm
10
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.
11
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.
12
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.
13
Selection sort
14
Example of selection sort
15
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
16
Example of bubble sort
17
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
18
19
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.
20
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.
21
22 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.
23
24 Example of a binary search
■ END
25