This preview shows pages 1–3. Sign up to view the full content.
CMPS 10  Homework Assignment 3
Problems:
Chapter 3 (p.120):
1ab, 6, 8abc, 9, 10, 11, 13, 14, 18, 22, 27, 28ab
1.)
a.)
Use Gauss’s approach to find the sum
2 + 4 + 6 + … + 100
b.)
Use Gauss’s approach to find the formula for the sum of the even numbers from 2 to
2
n
.
2 + 4 + 6 + … + 2
n
Your formula will be an expression involving
n
.
Solution:
a.)
Group them into pairs of the form:
100 + 2 = 102, 98 + 4 = 102, etc.
There are (50 /
2) = 25 of these pairs, so the sum equals 25 * 102 = 2550.
b.)
Similar to the last method, calculate 2
n
+ 2 = 2
n
+ 2, (2
n
– 2) + 4 = 2
n
+ 2, etc.
There are (
n
/ 2) elements, so the sum will be (
n
/ 2) * (2
n
+ 2) =
n
(
n
+ 1)
6.)
Perform a selection sort on the list 7, 4, 2, 9, 6.
Show the list after each exchange that
has an effect on the list ordering.
Solution:
Initial List:
7, 4, 2, 9, 6
Exchange 6 and 9:
7, 4, 2,
6
,
9
Exchange 7 and 6:
6
, 4, 2,
7
, 9
Exchange 6 and 2:
2
, 4,
6
, 7, 9
List sorted:
2, 4, 6, 7, 9
8.)
For each of the following lists, perform a bubble sort, and show the list after each
exchange.
Compare the number of exchanges done here, and in the Practice Problem at
the end of Section 3.3.3.
a.)
4, 8, 2, 6
b.)
12, 3, 6, 8, 2, 5, 7
c.)
D, B, G, F, A, C, E
Solution
:
a.)
Initial List:
4, 8, 2, 6
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Exchange 8 and 2:
4,
2
,
8
, 6
Exchange 8 and 6:
4, 2,
6
,
8
Exchange 4 and 2:
2
,
4
, 6, 8
List sorted:
2, 4, 6, 8
b.)
Initial List:
12, 3, 6, 8, 2, 5, 7
Exchange 12 and 3:
3
,
12
, 6, 8, 2, 5, 7
Exchange 12 and 6:
3,
6
,
12
, 8, 2, 5, 7
Exchange 12 and 8:
3, 6,
8
,
12
, 2, 5, 7
Exchange 12 and 2:
3, 6, 8,
2
,
12
, 5, 7
Exchange 12 and 5:
3, 6, 8, 2,
5
,
12
, 7
Exchange 12 and 7:
3, 6, 8, 2, 5,
7
,
12
Exchange 8 and 2:
3, 6,
2
,
8
, 5, 7, 12
Exchange 8 and 5:
3, 6, 2,
5
,
8
, 7, 12
Exchange 8 and 7:
3, 6, 2, 5,
7
,
8
, 12
Exchange 6 and 2:
3,
2
,
6
, 5, 7, 8, 12
Exchange 6 and 5:
3, 2,
5
,
6
, 7, 8, 12
Exchange 3 and 2:
2
,
3
, 5, 6, 7, 8, 12
List sorted:
2, 3, 5, 6, 7, 8, 12
c.)
Initial List:
D, B, G, F, A, C, E
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 09/15/2011 for the course CMPS 10 taught by Professor Tantalo during the Fall '08 term at University of California, Santa Cruz.
 Fall '08
 TANTALO

Click to edit the document details