Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

NAME

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Page 1 of 8

QUESTION 1. Perform a selection sort on the list 29, 69, 37, 14, 31, 10.
Show the list after each exchange that has an effect on the list ordering.

ANSWER – Given list = 29 , 69 , 37 , 14 , 31 , 10


1. To select and sort the list, first of all we have to pick the smallest
number from the list.
Here smallest number is 10. Now swap it with the first element of list
The new list will be 10 , 69 , 37 , 14 , 31, 29.
2. Now , similarly pick the second small element and replace it with the
second element of the list.
Second small element = 14
List = 10, 14 ,37, 69, 31, 29.
3. After that, choose the third small element and replace it with
the third element of the list.
Third small element = 29
List = 10, 14, 29, 69, 31,37.
4. Now , choose the fourth small element and it with fourth element.
Fourth small element = 31
List = 10,14,29,31,69,37.
5. At last choose the fifth small element and replace it with fifth
element .
Fifth small element = 37.
List = 10,14,29,31,37,69.
Here is the sorted list.
Question 2. Exercise 12 chapter 3 ( invitation to computer science )

Answer
Bubble sort is a simple sorting algorithm that repeatedly steps
through the list, compares adjacent elements, and swaps them if
they are in the wrong order. The pass through the list is repeated
until the list is sorted.
The reason bubble sort has a time complexity of O(n2) is because of
the way it operates:
Page 2 of 8

bubble sort would need to make n−1 comparisons for the first
element, n−2 comparisons for the second element, n−3
comparisons for the third element, and so on, until it makes 1
comparison for the last element.
If you add up all these comparisons, you get a total of 2n∗(n−1)
comparisons, which simplifies to
2n^2 – 2n.
In Big O notation, we only care about the highest-order term and
ignore the coefficients, because
they don’t matter as much for large inputs. So, 2n^2 – n^2
simplifies to O(n2).

Question 3 . Write the resulting data list, give the ending value of legit,
and find the exact number of copies done by the converging pointers
algorithm when executed on this set of data: 63 0 0 35 41 0 13
23 27 0.
Answer—The converging point algorithm is a technique for removing
zeroes from a list and compacting the non–zero elements.
Given list: 63 0 0 35 41 0 13 23 27 0
1. In the first step, we will swap the 0 at position 2 with 27 at position 9
So, the new list will be 63 27 0 35 41 0 13 23 0 0.
2. In the next step, 0 at position 3 will be swapped with 23 at position 8.
So, the new list will be 63 27 23 35 41 0 13 0 0 0.
3. In the last step , we will swap 0 at position 6 with 13 at position 7.
The final list will be 63 27 23 35 41 13 0 0 0 0.
The ending value of the legit( number of non – zero elements in the list) =
6
The exact number of copies done by the algorithm = 3.

Question 4. If the pattern-matching problem was changed to only find the


first instance of a pattern of length m in the text of length n.
(a) Describe the data that would cause the worst-case
performance and the performance in terms of n. Use “Big-O”
notation, e.g. Θ(n), Θ(n2), etc.
Page 3 of 8

(b) Describe the data that would cause the best-case


and performance in terms of n. Use “Big-O” notation, e.g. Θ(n),
Θ(n2), etc.

Answer a. worst-case performance – The worst-case scenario for a


pattern–matching algorithm occurs when the pattern is not in the text or
when the pattern is at very end of the text. In this case, the algorithm has
to transverse the entire algorithm to check if the

pattern is present or if the pattern is at the end.


For example – if text is “aaaaaaaaac” and the pattern is “ac”. Here,
the algorithm has to check the entire text before finding the pattern. In
this case, the performance is O(n), n refers to the length of the pattern.

Answer b. Best case- performance – The best-case scenario occurs


when the pattern is present at the beginning of the text. In this case, the
algorithm is able to identify the pattern without even transversing till
the end of the text.
For example – let us suppose if the text is “acaaaaaaaa” and the pattern is
“ac”, the algorithm finds the pattern right away. In this case, the
performance is O(1) but still, we will say that it is O(n) where n is the
length of the text because in terms of big O notation, the performance of
the algorithm is dependent on the size of the text.

Question 5 .

Answer a. When n = 100 it takes 10 seconds to execute.


We know that time taken to done work = cn
Here t = 10 , n = 100
C = t/n
Page 4 of 8

C = 10/100
Now, when n = 500
T =?
C = 10/ 100
T = cn
Putting values
T = (10/100) X 500
T = 50 seconds.

Answer b. when n = 100, it takes 10 seconds.


We know that in this case,
T = cn^2
Putting values
10 = c(100)^2
C = 10/100 X 100
Now when n = 500, T =?
Substituting values
T = cn^2
T = (10/100 x100) x 500 x 500
T = 250 seconds

Question 6. Calculate the decimal value of the following numbers.


A) . The base 5 number 342.
Answer – To calculate the decimal value of the number 342 with
base 5.
We can break down the digits according to their position value In
number 342
position value of 2 is zero
position value of 4 is one
position value of 3 is two
These position values will be the powers of 5 and can be written
as
3 x 5^2 + 4 x 5^1 + 2 x 5^0 (5^0 = 1)
= 3 x 25 + 4 x 5 + 2 x 1
Page 5 of 8

= 75 + 20 + 2
= 97
The decimal value of 342 with base 5 is 97.
B). The base 6 number 415
Answer – To find the decimal value of the number 415 with
Base 6.
We can break down the digits according to their position value in
415.
Position value of 5 is zero.
Position value of 1 is one.
Position value of 4 is two.
These position values will be the powers of 6 and can be written as
4 x 6^2 + 1 x 6^1 + 5 x 6^0
= 4 x 36 + 1 x 6 + 5 x1
= 144 + 6 + 5
= 155
The decimal value of the base 6 with number 415 is 155.

C). The base 16 hexadecimal number 7EAh ( h indicates hexa


decimal)
Answer – breaking down the digits according to their position value.
Position of A is 0
Position of E is 1
Position of 7 is 2
These position values will be the powers of the base 16 and can
be written as
7 x 16^2 + E x 16^1 + A x 16^0 (In hexadecimal we know
that E=14 , A =10)
= 7 x 256 + 14 x 16 + 10 x 1
= 1792 + 224 + 10
= 2026
The decimal value of base 6 with number 7EA is 2026.
Page 6 of 8

Question 7.Give the decimal value of the following numbers). Show


your work.
A) The 2s complement number 01010101

Answer – here the MSB is zero that is positive hence, we can


simply convert from binary to decimal
We know that when we convert to decimal the base value is two
and we start from right to left and position the binary numbers
from 0 to 7.

After that, we will multiply the binary number with base 2 and
power as assigned positions to the numbers from right to left
The last step is the calculations that are mentioned below.
01010101 = 0*(2^7) + 1*(2^6) + 0*(2^5) + 1*(2^4) + 0*(2^3)
+1*(2^2)+ 0*(2^1)+ 1*(2^0)
= 0+64+0+16+0+4+0+1
= 85

B . The 2s complement number 10101101

Answer – in this question, we can see that MSB (leftmost element


is negative)
So first we have to flip the bits to get 1’s compliment and then we
have to add 1 to get 2’s compliment, then convert to decimal and
add a negative sign

Flip the bits = 01010010


Add 1 = 01010011
Calculation = 0*(2^7) + 1*(2^6) + 0*(2^5) + 1*(2^4) + 0*(2^3)
+ 0*(2^2) + 1*(2^1) + 0*(2^0) = 64 + 16 + 2 + 1 = 83
The negative sign = -83.

C. The 2s complement number 01111111

Answer - here the MSB is zero that is positive hence, we can


simply convert from binary to decimal
We know that when we convert to decimal the base value is two
and we start from right to left and position the binary numbers
from 0 to 7.

After that, we will multiply the binary number with base 2 and
power as assigned positions to the numbers from right to left
Page 7 of 8

Calculations = 0*(2^7) + 1*(2^6) + 1*(2^5) + 1*(2^4) +


1*(2^3) + 1*(2^2) + 1*(2^1) + 1*(2^0)
= 64 + 32 + 16 + 8 + 4 + 2 +1 = 127

D. 10000111

Answer - we can see that MSB (leftmost element is negative)


So first we have to flip the bits to get 1’s compliment and then we
have to add 1 to get 2’s compliment, then convert to decimal and
add a negative sign

Flip the bits = 01111000


Add 1 = 01111001
Calculation = 0*(2^7) + 1*(2^6) + 1*(2^5) + 1*(2^4) + 1*(2^3)
+ 0*(2^2) + 0*(2^1) + 0*(2^0)
= 64 + 32 + 16 +8 +1 = 121
The negative sign = -121

E. The sign/magnitude number 10001101.

Answer - In the magnitude number, the leftmost bit represents


the sign, and the rest of the elements represent the magnitude
0001101. Here, leftmost element is 1 so the sign is negative.

Concerting magnitude to decimal = 0*(2^6) + 0*(2^5) + 0*(2^4)


+ 1*(2^3) + 1*(2^2) + 0*(2^1) + 1*(2^0)
= 8 + 4 + 1 = 13
Add a negative sign = -13

Question 8. Using the ASCII code given in figure 4.3 on page 165 gives the
binary representation for the phrase “Hi Paul!”, ignoring the quotation
marks. Make sure to space each 8-bit ASCII code apart from the next so
they are easy to understand.

Answer – As per the reference to figure 4.3 of the book


The ASCII code for each alphabet is mentioned below –

ALPHABET BINARY REPRESENTATION


H 01001000
i 01101001
(blank) 00100000
Page 8 of 8

P 01010000
a 01100001
u 01110101
l 01101100
! 00100001
Binary representation –
“Hi Paul!” – 01001000 011001001 00100000 01010000 01100001
01110101 01101100 00100001.

REFERENCES
 QUESTION 1. (CHAPTER 3, TOPIC SELECTION SORT ALGORITHM,
INVITATION TO COMPUTER SCIENCE 8TH EDITION, MICHAEL
SCHNEIDER, L. GERSTING)
 QUESTION 2. (CHAPTER 3, INVITATION TO COMPUTER SCIENCE 8TH
EDITION, MICHAEL SCHNEIDER, L. GERSTING)
 QUESTION 3. (PAGE 120, CHAPTER 3, INVITATION TO COMPUTER
SCIENCE 8TH EDITION, MICHAEL SCHNEIDER, L. GERSTING)
 QUESTION 4. (PAGE 131, CHAPTER 3, INVITATION TO COMPUTER
SCIENCE 8TH EDITION, MICHAEL SCHNEIDER, L. GERSTING)
 QUESTION 5. PART A (PAGE 101, CHAPTER 3, INVITATION TO
COMPUTER SCIENCE 8TH EDITION, MICHAEL SCHNEIDER, L.
GERSTING)
 QUESTION 5 PART B (PAGE 112, CHAPTER 3, INVITATION TO
COMPUTER SCIENCE 8TH EDITION, MICHAEL SCHNEIDER, L.
GERSTING)
 QUESTION 6,7(TOPIC BINARY NUMBER REPRESENTATION, CHAPTER
4, INVITATION TO COMPUTER SCIENCE 8TH EDITION,MICHAEL
SCHNEIDER, L.GERSTING).
 QUESTION 8 (PAGE 165, CHAPTER 4, INVITATION TO COMPUTER
SCIENCE 8TH EDITION, MICHAEL SCHNEIDER, L.GERSTING)

You might also like