Sorting and Overloading Operators - Lab Report 10 | CPSC 220, Lab Reports of Computer Science

Material Type: Lab; Class: Computer Science I; Subject: Computer Science; University: University of Mary Washington; Term: Unknown 1989;

Typology: Lab Reports

Pre 2010

Uploaded on 02/24/2010

koofers-user-dzj
koofers-user-dzj 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lab 10
Sorting + Overloading Operators
In a previous lab you examined a function named sortArray() that used a sorting
algorithm known as bubble sort. In class we’ve looked at selection sort to sort a list. In
this lab you’ll do some analysis of those sorts and one other called insertion sort.
Copy ~/ernie/220/lab10 to your directory.
Sorting Algorithms
Each sorting algorithm has its own strengths and weaknesses. The three sorting
algorithms implemented in lab10/sorts.h are easy to understand; their understandability
is one of their strengths. An algorithm’s strength can also be related to its performance in
a particular situation. In other words if the input to the function has a particular
characteristic or property, the algorithm will require less work than other sorting
algorithms. Let’s look at each of the algorithms using the array:
3 5 7 8 10
Question 1: Using the bubble sort algorithm, how many times do items change positions
(how many swaps occur) when the algorithm is executed on the array shown above?
Show your work as you trace the algorithm.
Question 2: Answer the same question using the selection sort algorithm. Show your
work as you trace the algorithm.
Question 3: Answer the same question using the insertion sort algorithm. Show your
work as you trace the algorithm.
You answered those questions when the array already was in ascending order. Now
supposed the items are reversed as
10 8 7 5 3
Question 4: Using the bubble sort algorithm, how many times do items change positions
(how many swaps occur) when the algorithm is executed on the array shown above?
Show your work as you trace the algorithm.
Question 5: Answer the same question using the selection sort algorithm. Show your
work as you trace the algorithm.
Question 6: Answer the same question using the insertion sort algorithm. Show your
work as you trace the algorithm.
An Empirical Study of the Sorting Algorithms
Modify each of the sort functions so that they include another parameter of type int that is
passed by reference. The value of that argument is to be the number of times items
change position in the sort. Modify the functions in sorts.h , and complete the
implementation of testSorts.cpp so that the program displays the number of items
exchanged or moved by the each sort. Create a script file that shows that running the
program using the data file sortTest1.data, and then another run using the data file
sortTest2.data confirms your answers to Questions 1 - 6 above. Provide a link to the
sorts.h, testSorts.cpp, and the script file for this test on the Web page that has your lab
reports.
Now we’ll try the same tests with larger collections of data. Once again create a script
file that show the results of running your program three times. Use the files
pf2

Partial preview of the text

Download Sorting and Overloading Operators - Lab Report 10 | CPSC 220 and more Lab Reports Computer Science in PDF only on Docsity!

Lab 10 Sorting + Overloading Operators In a previous lab you examined a function named sortArray() that used a sorting algorithm known as bubble sort. In class we’ve looked at selection sort to sort a list. In this lab you’ll do some analysis of those sorts and one other called insertion sort. Copy ~/ernie/220/lab10 to your directory. Sorting Algorithms Each sorting algorithm has its own strengths and weaknesses. The three sorting algorithms implemented in lab10/sorts.h are easy to understand; their understandability is one of their strengths. An algorithm’s strength can also be related to its performance in a particular situation. In other words if the input to the function has a particular characteristic or property, the algorithm will require less work than other sorting algorithms. Let’s look at each of the algorithms using the array: 3 5 7 8 10 Question 1: Using the bubble sort algorithm, how many times do items change positions (how many swaps occur) when the algorithm is executed on the array shown above? Show your work as you trace the algorithm. Question 2: Answer the same question using the selection sort algorithm. Show your work as you trace the algorithm. Question 3: Answer the same question using the insertion sort algorithm. Show your work as you trace the algorithm. You answered those questions when the array already was in ascending order. Now supposed the items are reversed as 10 8 7 5 3 Question 4: Using the bubble sort algorithm, how many times do items change positions (how many swaps occur) when the algorithm is executed on the array shown above? Show your work as you trace the algorithm. Question 5: Answer the same question using the selection sort algorithm. Show your work as you trace the algorithm. Question 6: Answer the same question using the insertion sort algorithm. Show your work as you trace the algorithm. An Empirical Study of the Sorting Algorithms Modify each of the sort functions so that they include another parameter of type int that is passed by reference. The value of that argument is to be the number of times items change position in the sort. Modify the functions in sorts.h , and complete the implementation of testSorts.cpp so that the program displays the number of items exchanged or moved by the each sort. Create a script file that shows that running the program using the data file sortTest1.data , and then another run using the data file sortTest2.data confirms your answers to Questions 1 - 6 above. Provide a link to the sorts.h, testSorts.cpp, and the script file for this test on the Web page that has your lab reports. Now we’ll try the same tests with larger collections of data. Once again create a script file that show the results of running your program three times. Use the files

sortTest3.data, sortTest4.data, and sortTest5.data. Include a link on the page of lab results to this script file. Question 7. The data in sortTest3.data was produced in random order, while the data in sortTest4.data and sortTest5.data are in descending and ascending order, respectively. Use the results of your testing to come up with one statement for each sort that describes the number of exchanges with a date in random, descending, or ascending order. Overloading Operators Its possible to overload operators in C++. By that we mean it’s possible to write a method for a class that will be represented by an operator such as +,-,,/,>,<. and others. See “Overloading Opreators,” http://www.cplusplus.com/doc/tutorial/tut4-2.html, or “Operator Overloading”, http://msdn.microsoft.com/library/default.asp?url=/library/en- us/vccelng/htm/overl_9.asp for more information. For example, suppose we define a Date class as and include an operation LessThan defined as bool LessThan(Date thatDate); // returns true only if thisDate.JDN < thatDate.JDN where thieDate is the object // activating the LessThan method and implemented as bool Date::LessThan(Date thatDate) // returns true only if JDN < thatDate.JDN { return JDN < thatDate.JDN } (See /users/ernie/220/Date.h and /users/ernie/Date.cpp for more details about this class.) We could do away with the method LessThan(Date thatDate) and replace it with bool Date::operator:< (Date thatDate) { return JDN < thatDate.getJDN(); } Then if we have an application with two date objects, T and U, we can write the expression T < U or T.operator< (U). Add a function to the List class, modify list.h and list.cpp , to overload the less than operator ( < ). Suppose we consider the expression a < b, where a and b are list. We’ll want that to be true if a[0] < b[0] and false if a[0] > b[0]. If a[0] = = b[0], then we apply the same criteria to a[1] and b[1], and so on. Here’s a more precise statements of what we mean by a list named a being less than another list named b, a < b: Let x be the minimum of a.length and b.length. Let j be the first nonnegative integer such that either j = = x-1 or a[j] != b[j]. If j < x-1 then a < b has the same value as a[j] < b[j]. If j = = x-1 then a < b has the same value as a.length < b.length Now complete the application testLisp.cpp to test the operator <. Places where you are to add statements in the application are marked with /* … **/ Deliverables. Links to :sorts.h, testSorts.cpp, list.h, list.cpp and testlist.cpp  script files that show the results of using sortTest1.data, sortTest2.data , sortTest3.data, sortTest4.data , and sortTest5.data.  script file showing the results of running testList.cpp  Answers to all questions