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

8a Algorithms Intro

An algorithm is defined as a step-by-step method for solving a problem, consisting of an ordered set of unambiguous steps that produces a result and terminates in finite time. Common algorithms include summation, product calculation, and sorting techniques such as selection sort, bubble sort, and insertion sort, as well as searching methods like sequential and binary search. Each algorithm serves specific purposes in computer science, with varying implementations depending on the programming language used.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

8a Algorithms Intro

An algorithm is defined as a step-by-step method for solving a problem, consisting of an ordered set of unambiguous steps that produces a result and terminates in finite time. Common algorithms include summation, product calculation, and sorting techniques such as selection sort, bubble sort, and insertion sort, as well as searching methods like sequential and binary search. Each algorithm serves specific purposes in computer science, with varying implementations depending on the programming language used.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

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
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.

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).

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.

5
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.

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.

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.
7
Summation algorithm
8
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.

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.

• 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.

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.

• 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.

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.

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.

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

You might also like