Algorithm Analysis
Algorithm Analysis
Algorithm
• An algorithm is a set of instructions to be followed to
solve a problem.
– There can be more than one solution (more than one
algorithm) to solve a given problem.
– An algorithm can be implemented using different
programming languages on different platforms.
• An algorithm must be correct. It should correctly solve
the problem.
– e.g. For sorting, this means even if (1) the input is already
sorted, or (2) it contains repeated elements.
• Once we have a correct algorithm for a problem, we
have to determine the efficiency of that algorithm.
Algorithmic Performance
There are two aspects of algorithmic performance:
• Time
• Instructions take time.
• How fast does the algorithm perform?
• What affects its runtime?
• Space
• Data structures take space
• What kind of data structures can be used?
• How does choice of data structure affect the runtime?
We will focus on time:
– How to estimate the time required for an algorithm
– How to reduce the time required
Data Structures
• Introduction about different Data Structures
• Array
• Stack
• Queue
• Linked List
• Tree
• Graph
Standard Operations
• Operations to access and modify data
– Traversing
– Insertion
– Deletion
– Updation
– Sorting
– Searching
– Merging
Algorithms
• To analyze algorithms:
– First, we start to count the number of significant
operations in a particular solution to assess its
efficiency.
– Then, we will express the efficiency of algorithms
using growth functions.
The Execution Time of Algorithms
• Each operation in an algorithm (or a program) has a cost.
Each operation takes a certain of time.
A sequence of operations:
Total Cost = c1 + c2
The Execution Time of Algorithms (cont.)
Example: Simple If-Statement
Cost Times
if (n < 0) c1 1
absval = -n c2 1
else
absval = n; c3 1
Successful Search:
Best-Case: item is in the first location of the array O(1)
Worst-Case: item is in the last location of the array O(n)
Average-Case: The number of key comparisons 1, 2, ..., n
n
i ( n 2 n) / 2
i 1
O(n)
n n
Binary Search
int binarySearch(int a[], int size, int x) {
int low =0;
int high = size –1;
int mid; // mid will be the index of
// target when it’s found.
while (low <= high) {
mid = (low + high)/2;
if (a[mid] < x)
low = mid + 1;
else if (a[mid] > x)
high = mid – 1;
else
return mid;
}
return –1;
}
Binary Search – Analysis
For an unsuccessful search:
The number of iterations in the loop is log2n + 1
O(log2n)
For a successful search:
Best-Case: The number of iterations is 1. O(1)
Worst-Case: The number of iterations is log2n +1 O(log2n)
Average-Case: The avg. # of iterations < log 2n O(log2n)
Insertion sort
Step 1 − If it is the first element, it is already sorted.
return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that
is greater than the value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted
Insertion sort(A)
For j=2 to A.length
Key = A[j]
i=j-1
While i>0 and A[i]>key
A[i+1] = A[i]
I = i-1
A[i+1] = key
Proof of correctness(Loop invariants method)
Initialization: It is true prior to the 1st iteration of loop
Maintenance: If it is true before an iteration of the
loop, it remains true before the next iteration
Termination: When the loop terminates, the invariant
gives us a property that helps show that the algorithm
is correct