By:
M.Sultan Zia
Assistant Professor
(DCS),
TODAY
What is an Algorithm?
And
How do we analyze one?
CIIT, Sahiwal
CIIT, Sahiwal
ALGORITHMS
Informally,
A tool for solving a well-specified computational
problem.
Input
Algorithm
Output
Example: sorting
input: A sequence of numbers.
output: An ordered permutation of the input.
issues: correctness, efficiency, storage, etc.
CIIT, Sahiwal
Selection sort
Insertion sort
Merge sort
(many others)
CIIT, Sahiwal
Sorting
Searching
Shortest paths in a graph
Minimum spanning tree
Primality testing
Traveling salesman problem
Knapsack problem
Chess
Towers of Hanoi
Program termination
ANALYSIS OF ALGORITHMS
ANALYSIS OF ALGORITHMS
CIIT, Sahiwal
10
11
Sequential Search
O (n)
CIIT, Sahiwal
12
Sequential Search
bool LinSearch(double x[ ], int n, double item){
for(int i=0;i<n;i++){
if(x[i]==item) return true;
else return false;
}
return false;
}
CIIT, Sahiwal
13
Search Algorithms
Suppose that there are n elements in the array. The following expression
gives the average number of comparisons:
It is known that
CIIT, Sahiwal
14
Search Algorithms
CIIT, Sahiwal
15
Binary Search
O(log2n)
CIIT, Sahiwal
16
Binary Search
Binary search algorithm assumes that the
items in the array being searched are
sorted
The algorithm begins at the middle of
the array in a binary search
If the item for which we are searching is
less than the item in the middle, we
know that the item wont be in the second
half of the array
Once again we examine the middle
element
The process continues with each
comparison cutting in half the portion of
CIIT, Sahiwal
17
Binary Search
CIIT, Sahiwal
18
mid =
left + right
2
CIIT, Sahiwal
19
Binary Search
20
CIIT, Sahiwal
21
Optimality
CIIT, Sahiwal
22
for i = 1 to n
for j = 1 to n-1
if A[j] < A[j+1]
swap(A,i,j)
23
SELECTION SORT
Input: array a[1],,a[n]
Output: array a sorted in non-decreasing order
Algorithm:
for i =1 to n
swap a[ i ] with smallest of a[ i ],,a[ n ]
CIIT, Sahiwal
24
INSERTION SORT
void insertionSort(int numbers[], int array_size)
{
int i, j, index;
for(i=1; i < array_size; i++)
{
index = numbers[i]; j = i;
while ((j > 0) && (numbers[j-1] > index))
{
numbers[j] = numbers[j-1]; j = j - 1; }
numbers[j] = index;
}
}
}
CIIT, Sahiwal
25
Optimality
CIIT, Sahiwal
26
ALGORITHM ANALYSIS
Determining performance characteristics.
(Predicting the resource requirements.)
Time, memory, communication bandwidth etc.
Computation time (running time) is of primary
concern.
27
EFFICIENCY OF ALGORITHMS
What resources are required to accomplish the
task
How one algorithm compares with other
algorithms
CIIT, Sahiwal
28
Complexity
How efficiency varies with the size of the task
Expressed in terms of standard functions of n
E.g. O(n), O(n2), O(log n), O(n log n)
CIIT, Sahiwal
29
EUCLIDS ALGORITHM
Problem: Find gcd(m,n), the greatest common divisor of two
nonnegative, not both zero integers m and n
Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ?
Euclids algorithm is based on repeated application of
equality
gcd(m,n) = gcd(n, m mod n)
until the second number becomes 0, which makes the
problem trivial.
Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12
CIIT, Sahiwal
30
(((
)))
CIIT, Sahiwal
31