24 - CMPSCI 187: Programming With Data Structures Lecture...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPSCI 187: Programming With Data Structures Lecture #24: Introduction to Searching and Sorting 7 November 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Introduction to Searching and Sorting • L&C’s SortingAndSearching Class • Static Generic Methods • Linear Search of an Array • Binary Search of a Sorted Array • Comparing Search Algorithms • A Lower Bound for Searching • Some O(n^2) Sorting Algorithms
Background image of page 2
L&C’s SortingAndSearching Class • We’ll now spend considerable time on two basic operations for a collection. Searching is determining whether a particular object is in a collection, and sorting is transforming a collection until it is in order (for a given ordering). • How shall we reuse code when we search and sort on different classes? We don’t want to write separate methods to do this for every class, but this is a different situation from the data structures we have studied so far. • Pre-generic Java had an interface Comparable for objects that could be searched or sorted by comparing one object to another. There were algorithms to sort or search arrays of Comparable objects, but making an array so general loses some of the advantages of data typing. • We’d like generic methods that can sort objects of any suitable type, but where should these methods live? In a SortingAndSearching class.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Static Generic Methods • We could have a generic class SortingAndSearching<T extends Comparable<? super T> , like the classes we had for ordered lists. • But it would be awkward (and possibly costly) to instantiate an object of this class every time we wanted it. Instead we want old-fashioned methods that take input and produce output, without being attached to an object at all. • A
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/22/2011 for the course COMPSCI 187 taught by Professor Barrington during the Fall '11 term at UMass (Amherst).

Page1 / 10

24 - CMPSCI 187: Programming With Data Structures Lecture...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online