Dale - Computer Science Illuminated 325

Dale - Computer Science Illuminated 325 - different...

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

View Full Document Right Arrow Icon
298 Chapter 9 Abstract Data Types and Algorithms Table 9.1 Average Number of Comparisons Length 10 100 1,000 10,000 Sequential search 5.5 50.5 500.5 5000.5 Binary search 2.9 5.8 9.0 12.0 Is the binary search algorithm really faster than the sequential search algorithm? Table 9.1 shows the number of comparisons required on average to find an item using a sequential search and using a binary search. If the binary search is so much faster, why don’t we always use it? More computation is required for each comparison because we must calculate the middle index. Also, the list must be in sorted order and must be imple- mented using an array-based implementation. If the list is already sorted, uses an array-based implementation, and the number of items is over 20, use a binary search algorithm. 9.6 Stacks and Queues Stacks and queues are abstract data types that are often thought of as a pair—like peanut butter and jelly or horse and carriage. Why this is so must be more historical than anything else, because they have quite
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: different behaviors. Stacks A stack is an abstract data type in which accesses are made at only one end. You can insert an item as the first one and you can remove the first one. This ADT models many things in real life. Accountants call it LIFO, which stands for Last In First Out. The plate holder in a cafeteria has this property. We can only take the top plate. When we do, the one below rises to the top so the next person can take one. Canned goods on a grocers shelf exhibit this property. When we take the first can in a row, we are taking the last can put in that row. Another way of stating the accessing behavior of a stack is that the item removed is the item that has been in the stack the shortest time. Viewing a stack from this perspective is more abstract. The insert has no constraints, the entire LIFO behavior is specified in the removal operation....
View Full Document

Ask a homework question - tutors are online