Sei sulla pagina 1di 13

Lecture 2: Introduction to Algorithms

• Review
• Linear Search
• Binary Search
• Selection Sort
• Reading
Review
Algorithm:
Textbook Definition: A well-ordered collection of
unambiguous and effectively computable operations
that when executed produces a result and halts in a finite
amount of time.
Review
Computer Science
Textbook Definition: The study of algorithms including

• Their formal and mathematical properties


• Their hardware realizations
• Their linguistic realizations
• Their applications
Pseudocode
• A programming language is a notation for specifying
instructions that a computer can execute
• Every (programming) language has a syntax and a
semantics
– Syntax specifies how something is said (grammar)
– Semantics specifies what it means
• Pseudocode is an informal programming language with
English-like constructs modeled to look like statements in
a Java-like language
• Anyone able to program in any computer language
should understand how to read pseudocode instructions.
• When in doubt just write it out in English.
Your first Algorithm
Linear Search: (also called sequential search)

Algorithm to determine whether a given name x appears


on a list.

Input: A list of n ≥1 names A[1], A[2], ... , A[n],


and a given name x.

Output. The message "Sorry, x is not on the list" if x is not


on the list.
Otherwise, the message: "x occurs at position i on the list."
Linear Search
Here are the instructions for linear search written in
pseudocode:
found = "no";
i=1;
while (found == "no" and i <= n)
{
if (A[i] == x)
{ found = "yes";
location = i;
}
i++;
}
if (found == "no")
{
print ("Sorry, " + x + " is not on the list");
}
else
{ print (x + " occurs at position " + location +
" on the list");
}
Introduction to Algorithms
• Definition of an algorithm: A well-ordered collection of unambiguous
and effectively computable operations that, when executed, produces a
result and halts in a finite amount of time.

• There are three components to an algorithm:


1. A set of inputs, where each input is a finite sequence of items.
2. A set of outputs, where each output is a finite sequence of items.
3. A method consisting of a finite sequence of instructions, each one of which
can be mechanically executed in a fixed length of time with a fixed amount
of resources. The method must produce an output for each input in the set
of possible inputs in a finite amount of time.
Introduction to Algorithms

• Input size: associated with each input is a measure of its size.


How we measure the size can vary from problem to problem.
Examples:
– Number of digits as in the number of digits in a number (Often we use
binary digits).
– Number of characters in a string.
– Number of items as in the number of items to be searched or sorted.
Binary Search

• Searching a sorted list


• How can we exploit the structure that a sorted list
provides?
• Start in the middle and determine in which half of
the list our target lives
• Repeat this procedure eliminating half of the
remaining list elements on each iteration
• What is the maximum number of comparisons
required here?
Binary Search

Input: A list of n ≥1 numbers A[1], A[2], ... , A[n],


and a target number x.

Output. The message "Sorry, x is not on the list" if x is not


on the list.
Otherwise, the message: "x occurs at position i on the list."
Pseudocode for Binary Search
found = "no";
begin = 1;
end = n;
while (found == "no" and begin  end)
{
m = begin + floor((end-begin)/2)
if(A[m] == x)
{
found = "yes";
location = m;
}
if(A[m]>x)
end = m-1;
if(A[m]<x)
begin = m+1;
}
if (found == "no")
{
print ("Sorry, " + x + " is not on the list");
}
else
{ print (x + " occurs at position " + location +
" on the list");
}
Selection Sort
Algorithm to sort the items on a list into nondecreasing
order

Input. A list of n ≥1 elements A[1], A[2], ... , A[n].


Output. The list sorted in nondecreasing order.
for (i = 1; i < length(A); i++)
{
locmin=i;
for (j=i+1;j <= length(A); j++)
{
if (A[locmin] > A[j])
locmin=j;
}
exchange(A[locmin], A[i]);
}
Reading

• Chapters 1-3 in Schneider and Gersting

Potrebbero piacerti anche