0% found this document useful (0 votes)
2 views

Chapter 4 Decrease and Conquer Homework

n
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter 4 Decrease and Conquer Homework

n
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Name: Ricardo Vaccaro Padron

ID #: 129074
CECS 6010 Winter 2022
Advanced Design and Analysis of
Algorithms
Chapter 4 Homework
Professor: Dr. Jeffrey Duffany
Instructions:

Read chapter 4 and do the following exercises:


Section 4.1 exercise 7
Section 4.4 exercises 3 & 4

Also please write and test an R language program


to implement the binary search algorithm. use it
to solve section 4.4 exercise 3

I. Section 4.1 Exercise 7.


7. Apply insertion sort in the list EXAMPLE in alphabetical order.

The list is separated with vertical line as sorted and unsorted list. From left to right, each key
from the unsorted list is compared to the elements in the sorted list. If the element is smaller than
any element in the sorted list. Then it is placed at the appropriate position. The element being
inserted is shown un bold.

The sorted list is A, E, E, L, M, P, X.

function ()
{
print("insertion sort")
print("test program")
n <- 7
a <- c("E","X","A","M","P","L","E")

print("original list")
print(a)
for (i in 2:n) {
v <- a[i]
j <- i - 1
while (j >= 1) {
if (a[j] > v) {
a[j + 1] <- a[j]
a[j] <- v
}
j <- j - 1
}
}
print("sorted list")
print(a)
}
II. Section 4.4 Exercise 3 and 4.

3. a. What is the largest number of key comparisons made by binary search in searching
for a key in the following array?

3 14 27 31 39 42 55 70 74 81 85 93 98

b. List all the keys of this array that will require the largest number of key comparisons
when searched for by binary search.

c. Find the average number of key comparisons made by binary search in a successful
search in this array. Assume that each key is searched for with the same probability.

d. Find the average number of key comparisons made by binary search in an unsuccessful
search in this array. Assume that searches for keys in each of the 14 intervals formed by
the array’s elements are equally likely.

Answerers:

Number of elements in the array: 13 thus n = 13.

a. The largest number of key comparisons perform by a binary search is log2 (n+1).
Which is equal to log2 (13+1) which simplifies to log2 (14). Log of base 2 (14) is equal to 3.807
which can be rounded up to 4. Based in this information the largest key comparisons will be 4.

b. One method to solve this part of the problem is to construct a binary search tree to have a
visual representation of the search. Thus, helping to list the keys that will require the largest
number of key comparisons when performing the binary search because the elements at the end
of the binary search tree are the elements that require the largest number of key comparisons.
The binary three that represents the array given at the beginning of the problem is the following:
The binary tree above, is a representation of the array provided in problem 3. The tree is
constructed such as the first number represents the position and the second number, the number
inside the parentheses, represents the value. Thus, the searches for the elements on the last level
of the binary search tree will be 1(14), 3(31), 5(42), 8(74), 10(85), 12(98). This means that the
largest key comparison is for element 98 which takes 12 comparisons followed by element 85
which takes 10 comparisons.

c. The formula for the average number of key comparisons made by binary search in a successful
search in a given array is C bin
avg (n) = log2 (n-1). Which is equal to log2(13-1) that simplifies to
1og212 which is 3.58. This means that the average number of key comparisons made by binary
search in a successful search in the array is 3.58, if we round up would be 4 and if we round
down it would be 3.
d. The average number of key comparisons made by binary search in an unsuccessful search in
this specific array is Cbin
avg
(n) = log2 (n+1). This equation after substituting the value of n is equal
to log2 (13+1) which equals log2 14 which is equal to 3.807.
We can conclude that the average number of key comparisons made by binary search in an
unsuccessful search in this array is 3.807.

II. Section 4.4 exercises 3 & 4

4. Estimate how many times faster an average successful search will be in a sorted array of
one million elements if it is done by binary search versus sequential search.

Given the number of elements in the sorted array n = 1,000,000. The average number of key
comparisons made by binary search is Cavg (n)≈ log2 n. In a binary search, the average number of
comparisons is equal to log2 n. We substitute the value of n in the equation which would then
give up log2 (1,000,000) which can be simplified to log2 105 which is equal to 16.60.
Now, the average number of key comparisons made by sequential search is n/2. This means that
the average number of comparisons in successful search is n/2, and we substitute the value of n
which then is 1,000,000/2 which can be simplified to 105/2 which is equal to 5,000.
The ratio of the average number of key comparisons made by sequential search and binary
search is which means that is 3010 times faster an average successful search.
III. Also please write and test an R language program to implement the binary search
algorithm. Use it to solve section 4.4 exercise 3.

binary_search <- function(){


print("binary search analysis")
print("for all keys in list")
print("levitin homework 4.4 (3)")
print("sorted list of 13 elements")
a<-c(3,14,27,31,39,42,55,70,74,81,85,93,98,100,150,155,265)
print(a)
print("number of key comparisons")
sum=0
for (k in a){
l<-1
r<-13
count<-0
while (l<=r) {
m<-floor((l+r)/2)
count<-count+1
if (k==a[m])
l<-r+1
if (k<a[m])
r<-m-1
if(k>a[m])
l<-m+1
}
print(c(k,m,count))
sum<-sum+count
}
print("average number of key comparisons")
print(sum/13)
}
binarysearch2 <- function(){
print("binary search analysis")
print("for keys not in list")
print("levitin homework 4.4 (3)")
print("sorted list of 13 elements")
a<-c(3,14,27,31,39,42,55,70,74,81,85,93,98)
b<-a+1
print(a)
b<-c(2,b)
print("list of search keys not in list")
print(b)
sum=0
print("number of key comparisons")
for (k in b){
l<-1
r<-13
count<-0
while (l<=r) {
m<-floor((l+r)/2)
count<-count+1
if (k==a[m])
l<-r+1
if (k<a[m])
r<-m-1
if(k>a[m])
l<-m+1
}
print(c(k,count))
sum<-sum+count
}
print("average number of of key comparisons")
print(sum/(length(b)))
}

You might also like