Homework 2

advertisement
CS 163
HM
DATA STRUCTURES
HW 2
CCUT
HomeWork 2, Sorting, 100 Points
Due Date:
Subject:
Total Points:
(5/16/2015)
Friday June 19, 2015, at start of class; submit as usual for prof. Yang Sun
Sort 2 different arrays using 2 different methods
100. Suitable commenting counts for a significant part of these points
General Rule: Implement your homework in C++. Write well-structured, readable, source
programs. Use helpful comments and apply them liberally, expressing ideas not immediately visible
from the source code. E.g. articulate solution ideas, list sources of algorithms, note caveats, explain
goals. Only individual work counts.
Summary: Write a C++ program that sorts the 2 integer arrays a1[] and a2[] using the bubble
sort and the insertion sort method.
Detail: Step 1: Use a C++ macro to define the symbolic integer constant MAX of value 12, to be
used to define an integer array size. Write a C++ typedef to define the type of a single-dimensional
integer array type arr_tp.
Step 2: Define and statically initialize 2 arrays a1[] and a2[] of type arr_tp as follows:
// goal will be a sort in ascending order:
// a1[] is completely unsorted
arr_tp a1 = {
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
-1, -2, -3, -4, -5, -6, -7, -8, -9 };
// goal will be a sort in ascending order:
// a2[] is almost completely sorted
arr_tp a2 = {
-9, -8, -7, -6, -5, -4, -3, -1, -2, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Step 3: You will sort the arrays a1[] and a2[] using different methods, hence it will be handy to
make copies of these arrays, to preserve or recreate their statically defined values, shown above.
Write one single C++ program that sorts a1[] and a2[] in ascending order, using the bubble sort
method. Print out the original unsorted and then the sorted arrays. In the same program, also sort
arrays a1[] and a2[] in ascending order, using the insertion sort method. Print the original unsorted
and then the sorted arrays. A sample output is shown below:
array before a1[] bubble sort = [
array after a1[] bubble sort = [
10
-9
9 8 7 6
-8 -7 -6
5 4 3 2
-5 -4 -3
1 0 -1 -2
-2 -1 0 1
-3 -4 -5
2 3 4 5
-6 -7 -8
6 7 8 9
-9 ]
10 ]
array before a2[] bubble sort = [
array after a2[] bubble sort = [
-9
-9
-8
-8
-5
-5
-1
-2
2
2
6
6
9
9
10 ]
10 ]
array before a1[] insert sort = [
array after a1[] insert sort = [
10
-8
9 8 7 6
-7 -6 -5
5 4 3 2
-4 -3 -2
1 0 -1
-1 0 1
-8
10
-9 ]
-9 ]
array before a2[] insert sort = [
array after a2[] insert sort = [
-9
-9
-8
-8
-5
-5
-1
-2
9
9
10 ]
10 ]
-7
-7
-7
-7
-6
-6
-6
-6
-4
-4
-4
-4
-3
-3
-3
-3
-2
-1
-2
-1
0
0
0
0
1
1
3
3
4
4
-2 -3 -4
2 3 4 5
1
1
2
2
3
3
4
4
5
5
7
7
8
8
-5 -6 -7
6 7 8 9
5
5
6
6
7
7
8
8
Step 4: Compute and print the number of steps each of these 4 sorts took, and print them, together
with the output for the sorted array. Each execution of an inner loop counts as 1 step, no matter how
simple or complex that inner loop body is. See he sample out with step count below:
array before a1[] bubble sort = [
array after a1[] bubble sort = [
Unsorted a1[] steps = 190
10
-9
9 8 7 6
-8 -7 -6
1
5 4 3 2
-5 -4 -3
1 0 -1 -2
-2 -1 0 1
-3 -4 -5
2 3 4 5
-6 -7 -8
6 7 8 9
HW 2
-9 ]
10 ]
CS 163
HM
DATA STRUCTURES
HW 2
-7
-7
array before a2[] bubble sort = [
array after a2[] bubble sort = [
almost sorted a2[] steps = 190
-9
-9
-8
-8
array before a1[] insert sort = [
array after a1[] insert sort = [
Unsorted a1[] steps = 171
10
-8
9 8 7 6
-7 -6 -5
5 4 3 2
-4 -3 -2
1 0 -1
-1 0 1
array before a2[] insert sort = [
array after a2[] insert sort = [
almost sorted a2[] steps = 1
-9
-9
-8
-8
-5
-5
-1
-2
-7
-7
-6
-6
-6
-6
-5
-5
-4
-4
-4
-4
-3
-3
CCUT
-3
-3
-1
-2
-2
-1
-2
-1
0
0
0
0
1
1
2
2
3
3
4
4
-2 -3 -4
2 3 4 5
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
-5 -6 -7
6 7 8 9
5
5
6
6
7
7
9
9
10 ]
10 ]
-8
10
-9 ]
-9 ]
9
9
10 ]
10 ]
8
8
Step 5: Explain in a comment, why the computed number steps for the insertion sort, sorting array
a2[], is so low.
Table 1: Distribution of points
Number
1
2
3
4
5
6
7
Credit
Define constant M<AX and arr_tp
Declare and initialize statically a1[] and a2[]
Sort a1[] and a2[] using bubble sort
Sort a1[] and a2[] using insertion sort
Track and print number of steps
Explain why 1 sort was so efficient
Use proper commenting
Total
2
Points
5
15
25
25
10
10
10
100
HW 2
Download