NAME
NAME
NAME
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
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 5 .
C = 10/100
Now, when n = 500
T =?
C = 10/ 100
T = cn
Putting values
T = (10/100) X 500
T = 50 seconds.
= 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.
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
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
D. 10000111
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.
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)