Title: Computer Programming Lab 9
1Computer ProgrammingLab 9
- Shyh-Kang Jeng
- Department of Electrical Engineering/
- Graduate Institute of Communication Engineering
- National Taiwan University
2Lab Exercise 8 Bubble Sort Part A
- Modify the bubble sort implementation to produce
the following output shown in next page
3Sample Output
Data items in original order 2 6 4 8 10
12 89 68 45 37 After pass 0 2 4 6 8 10
12 68 45 37 89 After pass 1 2 4 6 8 10 12 45
37 68 89 After pass 2 2 4 6 8 10 12 37 45 68
89 After pass 3 2 4 6 8 10 12 37 45 68
89 After pass 4 2 4 6 8 10 12 37 45 68
89 After pass 5 2 4 6 8 10 12 37 45 68
89 After pass 6 2 4 6 8 10 12 37 45 68
89 After pass 7 2 4 6 8 10 12 37 45 68
89 After pass 8 2 4 6 8 10 12 37 45 68
89 Data items in ascending order 2 4 6
8 10 12 37 45 68 89 Number of comparison
81
4Template (1/2)
- // CPLab9\Main.cpp
- include ltiostreamgt
- using stdcout
- using stdendl
- include ltiomanipgt
- using stdsetw
- int main()
- const int SIZE 10
- int a SIZE 2, 6, 4, 8, 10, 12, 89, 68,
45, 37 - int hold
- int numberOfComparisons 0
5Template (2/2)
- cout ltlt "Data items in original order\n"
- for ( int i 0 i lt SIZE i )
- cout ltlt setw( 4 ) ltlt a i
- cout ltlt "\n\n"
- / Write bubble sort implementation here /
- cout ltlt "\nData items in ascending order\n"
- for ( int j 0 j lt SIZE j )
- cout ltlt setw( 4 ) ltlt a j
- cout ltlt "\nNumber of comparisons "
- ltlt numberOfComparisons ltlt endl
- return 0
- // end main
6Lab Exercise 8 Bubble Sort Part B
- After the first pass, the largest number is
guaranteed to be in the highest-numbered element
of the array, after the second pass, the two
highest numbers are in place, and so on.
Instead of making nine comparisons on every pass,
modify the bubble sort to make eight comparisons
on the second pass, seven on the third pass, and
so on. The output should look like that given in
the next page.
7Sample Output
Data items in original order 2 6 4 8 10
12 89 68 45 37 After pass 0 2 4 6 8 10
12 68 45 37 89 After pass 1 2 4 6 8 10 12 45
37 68 After pass 2 2 4 6 8 10 12 37 45 After
pass 3 2 4 6 8 10 12 37 After pass 4 2 4 6
8 10 12 After pass 5 2 4 6 8 10 After pass
6 2 4 6 8 After pass 7 2 4 6 After pass 8
2 4 Data items in ascending order 2 4 6
8 10 12 37 45 68 89 Number of comparison
45
8Lab Exercise 8 Bubble Sort Part C (1/2)
- The data in the array may already be in the
proper order or near-proper order, so why make
nine passes if fewer will suffice? Modify the
bubble sort to check at the end of each pass if
any swaps have been made. If none has been made,
then the data must be in the proper order, so the
program should terminate. If swaps have been
made, then at least one more pass is needed. Do
the following
9Lab Exercise 8 Bubble Sort Part C (2/2)
- Use the data set 2, 6, 4, 8, 10, 12, 89, 68, 45,
37 from Parts A and B. Did the program appear
to run any differently? - Now use the data shown in the output given in
next page. Did the program appear to run any
differently?
10Sample Output
Data items in original order 6 4 2 8 10
12 37 45 68 89 After pass 0 4 2 6 8 10
12 37 45 68 89 After pass 1 2 4 6 8 10 12 37
45 68 After pass 2 2 4 6 8 10 12 37 45 Data
items in ascending order 2 4 6 8 10 12
37 45 68 89 Number of comparison 24
11Follow-Up Activity
- Remove bubble sort from main and place it in a
function named bubbleSort that takes an array
argument and a constant integer representing the
arrays size. What should this function return?
Is the array passed call-by-reference or
call-by-value?