Algorithms
Algorithms
Performance Analysis
Searching Techniques
Sorting Techniques
Algo Design Techniques
Algorithm
Algorithm arrayMax(A,n):
Input: An array A storing n>=1 integers.
Output: The maximum element in A.
currentMax ←A[0]
for i ← 0 to n – 1 do
If currentMax < A[i] then
currentMax ←A[i]
return currentMax
Methodologies for Analyzing Algorithms
Running time
depends on size, distinct input, hardware
environment( processor, clock speed, memory, disk
type), software environment ( operating system,
programming language, compiler, interpreter)
Methodologies for Analyzing Algorithms
1. Pseudo-Code
It is a mixture of natural language and high-level
programming construct that describe the main ideas
behind a generic implementation of the data structure or
algorithm.
Methodologies for Analyzing Algorithms
Algorithm recursiveMax(A,n):
Input: An array A storing n>=1 integers.
Output: The maximum element in A.
If n=1 then
return A[0]
return max { A[n-1], recursiveMax(A,n-1) }
Methodologies for Analyzing Algorithms
Recurrence equation
T(n)= ( 3 if n=1 )
( T(n-1) + 7 otherwise )
Asymptotic Notation
3 , 2, 6, 1, 9, 0, 8, 7, 5, 4
Linear Search
3 , 2, 6, 1, 9, 0, 8, 7, 5, 4
3 , 2, 6, 1, 9, 0, 8, 7, 5, 4
return -1;
}
Binary Search
int binarySearch(int arr[], int start, int end, int x)
{
if (end >= start)
{
int mid = start + (end - start)/2;
Examples:
Bubble Sort
Selection Sort
Insertion Sort
Quick Sort
Heap Sort
Bucket Sort or Bin Sort
Radix Sort
Sorting Techniques
External Sorting : In external sorting data is stored
outside main memory (like disk) and only loaded in to
memory in small chunks
Examples:
Merge Sort
Multiway Merge Sort
Polyphase Sorting
Bubble sort
3 , 2, 6, 1, 9, 8
Bubble sort
3 , 2, 6, 1, 9, 8
3 , 2, 6, 1, 9, 8
Selection sort
3 , 2, 6, 1, 9, 8
3 , 2, 6, 1, 9, 4
Insertion sort
3 , 2, 6, 1, 9, 4
1 , 2, 3, 6, 9 0, 4, 5, 7, 8
Merge Sort
3 , 2, 6, 1, 9, 0, 8, 7,
Merge Sort
3 , 2, 6, 1, 9, 0, 8, 7,
void partition(int arr[],int low,int high){
int mid;
if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
Merge Sort
3 , 2, 6, 1, 9, 0, 8, 7
3 , 2, 6, 1, 9, 4
Quick sort
3 , 2, 6, 1, 9, 4
Complexity:
Time Complexity : O(n 2) worst case
Θ(nlogn) Average case
Ω(nlogn) best case
Space complexity: In-place
Bin sort or Bucket sort
.70, .53, .23, .42, .21, .29, .61, .51, .64, .91, .22
Bin sort or Bucket sort
.70, .53, .23, .42, .21, .29, .61, .51, .64, .91, .22
Complexity:
Space Complexity: O(n)
Time Complexity: O(n+k) ( average case )
Θ(n2) ( worst case )
Ω(n+k) (best case )
Radix Sort
3 , 2, 6, 1, 9, 4
Heap Sort
3 , 2, 6, 1, 9, 4
Insertion sort
Binary Tree traversals: inorder, preorder and postorder
(recursion)
Computing the length of the longest path in a binary tree
(recursion)
Computing Fibonacci numbers (recursion)
Reversing a queue (recursion)
Warshall’s algorithm (recursion)
Algorithm Design Paradigms
Dynamic Programming:Dynamic Programming is a
Bottom-Up Technique in which the smallest sub-
instances are explicitly solved first and the results of
these used to construct solutions to progressively larger
sub-instances.
3. Problem reduction
Example: least common multiple
lcm(m,n) = (m*n)/ gcd(m,n)
Algorithm Design Paradigms
Backtracking:The method is used for state-space
search problems. State-space search problems are
problems, where the problem representation consists of:
-initial state
-goal state(s)
-a set of intermediate states
-a set of operators that transform one state into
another. Each operator has preconditions and
postconditions.
-a cost function – evaluates the cost of the operations
(optional)
-a utility function – evaluates how close is a given state
to the goal state (optional)
Algorithm Design Paradigms
Problem 2:
You are given two jugs, a 4-gallon one and a 3-gallon
one. Neither has any measuring markers on it. There is a
tap that can be used to fill the jugs with water. How can
you get exactly 2 gallons of water into the 4-gallon jug?
Algorithm Design Paradigms