Dale - Computer Science Illuminated 313

Dale - Computer Science Illuminated 313 - 286 Chapter 9...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
286 Chapter 9 Abstract Data Types and Algorithms appropriate item to print. Thus, the Insert operation must place the items in the list in the proper order. The list doesn’t have to know anything about the fields of ItemClass . The compareTo method gives enough information. To see how this works, let’s look at an example. Let’s say we are keeping numbers in numeric order. The list so far contains the following values: 23, 46, 75, 1066, 1492, 2001 and we want to insert 998. Compare 998 to 23 and it is larger, so we compare 998 with the next value. 998 is larger than 46, so we compare 998 with the next value. 998 is larger than 75, so we compare 998 with the next value. 998 is less than 1066, so 998 goes in the list just before 1066. We begin with the first item in the list and compare the value to be inserted. As long as the value we are inserting is greater than the next value in the list, we keep moving down the list. When we find a place where the value to be inserted is less than the value in the list, we have found the insertion place. Find the item
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/13/2011 for the course CSE 1550 taught by Professor Marianakant during the Fall '10 term at York University.

Ask a homework question - tutors are online