Lec 37 Java Fundamental of Computing
Lec 37 Java Fundamental of Computing
I Semester 2008-09
Lecture 37
1
Problem 1 : Searching
2
Comparing Searching algorithms
• Sequential Search
• Binary search
Experimental observation : Binary search has been found to be much faster
than sequential search. (given as assignment during Lab 11 for Thursday and
Friday)
3
Problem 2 : Computing GCD of two positive numbers
4
Comparing Two GCD algorithms
We gave two algorithms for GCD computation long back in this course.
5
Why is GCD fast faster than GCD slow ?
//Assume m>=n
int GCD_fast(m,n)
{ while(n!=0)
{ rem = m%n;
m = n;
n = rem;
}
return m;
}
--------------------------------
int GCD_slow(m,n)
{ while(n!=0)
{ diff = m-n;
if(diff>=n) m = diff
else {m=n; n = diff;}
}
return m;
}
6
Problem 3 : Sorting
7
Comparing Three sorting algorithms
Experimental Observations
8
What is the reason for different running times?
9
Algorithm design is incomplete until you analyze its running time
10
How many steps/instructions are executed by the following loop ?
11
How many steps/instructions are executed by the following loop ?
12
How many steps/instructions are executed by the following loop ?
13
How many steps/instructions are executed by the following loop
for(int n=1;n<=m;n=n+1)
{
for(int i=1; i<=n; i=i+1)
{
sum = sum + i;
}
}
14
How many steps/instructions are executed by the following loop
for(int n=1;n<=m;n=n+1)
{
for(int i=1; i<=n; i=i+1)
{
sum = sum + i;
}
}
15
How many steps/instructions are executed by the following loop
for(int n=1;n<=m;n=n+1)
{
for(int i=1; i<=n; i=i+1)
{
sum = sum + i;
}
}
For sake of simplicity, we can say that
No. of Steps = am2 + bm + c, for some constants a,b,c
16
Analysis of Number of instructions of an algorithm
17
Analysis of Number of instructions of an algorithm
18
No. of Instructions executed during Sequential Search on n numbers
• For sequential search, you can write a for loop for the sequential search
which iterates n times in the worst case.
19
No. of Instructions executed during Binary Search
Given that array A is sorted.
20
Analysis of Binary Search
21
Hence Binary Search is exponentially faster than sequential search
22
Why is GCD fast faster than GCD slow ?
//Assume m>=n
int GCD_fast(m,n)
{ while(n!=0)
{ rem = m%n;
m = n;
n = rem;
}
return m;
}
--------------------------------
int GCD_slow(m,n)
{ while(n!=0)
{ diff = m-n;
if(diff>=n) m = diff
else {m=n; n = diff;}
}
return m;
}
23
Analysis of GCD fast
We shall bound the number of iterations of the while loop.
//Assume m>=n
int GCD_fast(m,n)
{ while(n!=0)
{ rem = m%n;
m = n;
n = rem;
}
return m;
}
24
Analysis of GCD fast
• Case 1 : m > 32 n
Inference : after the iteration
the new value of m < 23 times old value of m
• Case 2 : m ≤ 32 n
Inference : after the iteration
The new value of n ≤ 12 times old value of n
25
Analysis of GCD fast
• Case 1 : m > 32 n
Inference : after the iteration
2
the new value of m < 3 times old value of m
• Case 2 : m ≤ 32 n
Inference : after the iteration
The new value of n ≤ 12 times old value of n
26
Analysis of GCD fast
• Case 1 : m > 32 n
Inference : after the iteration
2
the new value of m < 3 times old value of m
• Case 2 : m ≤ 32 n
Inference : after the iteration
The new value of n ≤ 1
2 times old value of n
27
Analysis of GCD fast
• It can be seen that once m or n becomes less than or equal to 2, at most one
more iteration will be executed.
28
Analysis of GCD slow
//Assume m>=n
int GCD_slow(m,n)
{ while(n!=0)
{ diff = m-n;
if(diff>=n) m = diff
else {m=n; n = diff;}
}
return m;
}
29
Comparing GCD fast and GCD slow
30
Comparing Selection Sort and Merge Sort
31
No. of instructions executed in Selection Sort on n numbers
32
Number of instructions taken in Selection Sort
int index_of_smallest_value(int[] A,int i)
//returns integer j such that A[j] is smallest among A[i], A[i+1],...
SelectionSort(int [] A)
{
for(int count=0;count<A.length;count=count+1)
{
int j = index_of_smallest_value(A, count);
if(j != count)
swap_values_at(A,j,count);
}
}
33
Number of instructions taken in Selection Sort on n numbers
34
Number of instructions taken in Selection Sort on n numbers
35
Number of instructions taken in Selection Sort on n numbers
36
Number of instructions taken in Selection Sort on n numbers
X n(n − 1)
c(n − i) = c
2
0≤i<n−1
37
Let us analyse Merge Sort on n numbers
38
number of instructions for merging two sorted arrays of size n
start scanning A and B from left, compare two elements of A and B , copy the
smaller one into C and continue ...
39
Merging two sorted arrays
A 5 9 28 49 89 101 ...
B 3 34 40 53 66 92 ...
C 3 5 9 28 34
40
Number of instructions taken in Merge Sort
public static void mergesort(int[] A, int left, int right)
{ if(left!=right)
{ int mid = (left+right)/2;
mergesort(A, left, mid);
mergesort(A, mid+1, right);
merge(A,left,mid,right);
}
}
41
Number of instructions taken in Merge Sort
MSort(A,0,7)
Level 3
MSort(A,4,7)
MSort(A,0,3)
Level 2
Level 1
Level 0
MSort(A,0,0) MSort(A,1,1) MSort(A,2,2) MSort(A,3,3) MSort(A,4,4) MSort(A,5,5) MSort(A,6,6) MSort(A,7,7)
A 99 7 5 1 67 11 4 2
42
Number of instructions taken in Merge Sort
43
Alternate analysis of Merge Sort
Here cn is the no. of instructions for merging two halves of the array.
44
Alternate analysis of Merge Sort
45
Efficiency of Quick Sort to be analysed in next lecture class
46