Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Overview ● Searching – 8.1 ● Linear search – 8.2 ● Binary search ● Sorting – 8.3 ● Presents the insertion sort algorithm.
Overview ● 8.4 – Introduce an interface that allows us to search and sort arrays of other things. ● 8.5 – Sorting linked lists
Linear Search ● Linear search – Start at the beginning and examine each element in turn. – It takes linear time in both the worst and average cases.
Linear Search ● Algorithm is more efficient if we know in advance that the array is sorted. – The number we encounter during a search increases as we move from left to right across the array. – If we ever encounter a number which is larger than the target, we can stop searching. ● The target can't possibly appear later in the array.
Linear Search
● Worst case – No faster than the old version. – Average still in Θ (n) reduced by a factor of 2. – Let event i be the event that the target, if it were present, would belong right before element i.
Linear Search ● Assume that the n + 1 events are equally likely. ● On average look at between n/2 and (n/2) + 1 elements.
Binary Search ● Starting our search in the middle with a sorted array. – A constant amount of work allows us to divide the data in half.
Binary Search
● The running time of binary search is proportional to the number of times the loop runs. – The number of times we have to divide the data in half before we run out of data. – In the worst case, we always have to look in the larger piece. – The number of passes through the loop is p + 1, where 2 p = n.
Binary Search ● If n = 8, we have one pass where there are 8 candidate elements, one where there are 4, one where there are 2, and one where there is 1. – This is four passes. – Notice that 2³ = 8. – If n were 2 4 = 16, we would need 5 passes. – 1 + log 2 n Θ (log n)
Binary Search
● We do not generally expect n to be a power of two, but for most running-time functions this shortcut will not change the order of our result. – Theorem: Let f(n) and g(n) be two monotonically nondecreasing functions. If f(n) = g(n) for exact powers of two, and cg(n) > g(2n) for some constant c, then f(n) Θ (g(n)) in general. – cg(n) > g(2n) indicates that this theorem does not work for very quickly growing functions like 2 n. – It does work for any function in a polynomial or lower order.
Binary Search
● cg(2) > g(4), cg(4) > g(8) ● cg(n) is always above the boundary boxes, and therefore greater than f(n) – f(n) O(cg(n)) = O(g(n)) – f(n) Ω (g(n)) – f(n) Θ (g(n)) Binary Search
● Binary search – Case where n is not a power of 2. – The number of passes is. – Since log 2 n is an integer for exact powers of 2 assuming that n is a power of 2 allows us to ignore floors. Binary Search
Insertion Sort ● There are many algorithms for getting an array into this sorted state. – Insertion sort: ● One of the simplest sorting algorithms.
Insertion Sort
● “in use” portion of the input array is always exactly the same size as the “not in use”. – We can use the same array for both purposes.
Insertion Sort ● Element 0 would always be inserted at position 0. It's already there. – It will move if any smaller numbers are later inserted.
Insertion Sort
● Running time of insertion sort depends on the running time of this inner loop. – Best case for inner loop:
Insertion Sort ● Worst case data was in reverse order.
Insertion Sort ● Average case
Insertion Sort ● Since the average-case time can't be worse- than the worst-case time, the average-case time must also be in O(n 2 ), and therefore in Θ (n 2 ).
The Comparable Interface ● We could use polymorphism to write methods to search and sort structures containing Integers, Cards, or anything else. – “greater than” and “less than” don't make sense for all classes. – It only makes sense to sort things which are comparable. – Java provides a built-in interface Comparable. – All of the wrapper classes, as well as String, implement Comparable.
The Comparable Interface
● Comparable is generic because we can't compare instances of different subclasses. – The type parameter specifies the class in question. – If we want a generic class to hold only of comparable classes ● > – “some class E that implements Comparable. – In this context, Java does not distinguish between implementing an interface and extending a class; the keyword extends covers both cases.
The Comparable Interface
● compareTo() – Given two objects a and b, a.compareTo(b) returns a negative number if a is less than b, zero if a equals b, and a positive number if a is greater than b.
The Comparable Interface
● compareTo() method in the String class uses lexicographical order. – All upper-case letters to be earlier than all lower- case ones.
The Comparable Interface
● If we want to make one of our own classes Comparable, we have to declare that it implements Comparable and provide the compareTo() method. – In some classes, this amounts to a simple subtraction.
The Comparable Interface
● The compareTo() method should be consistent with the equals() method. – a.equals(b) should be true for exactly those values of a and b for which a.compareTo(b) returns 0.
Sorting Linked Lists ● Binary search algorithm depends on random access. – Most sorting algorithms also depend on random access, some of them can be adapted to work with linked lists. – For our insertion sort to run efficiently, we must be careful to avoid the methods get(), set(), and size(), all take linear time on linked lists.
Sorting Linked Lists
Summary ● Two useful searching algorithms are linear search and binary search. ● The insertion sort algorithm inserts each element in turn into a sequence of already sorted elements.
Summary