Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Similar presentations


Presentation on theme: "Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved."— Presentation transcript:

1 Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

2 Overview ● Searching – 8.1 ● Linear search – 8.2 ● Binary search ● Sorting – 8.3 ● Presents the insertion sort algorithm.

3 Overview ● 8.4 – Introduce an interface that allows us to search and sort arrays of other things. ● 8.5 – Sorting linked lists

4 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.

5 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.

6 Linear Search

7 ● 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.

8 Linear Search ● Assume that the n + 1 events are equally likely. ● On average look at between n/2 and (n/2) + 1 elements.

9 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.

10 Binary Search

11

12 ● 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.

13 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)

14 Binary Search

15 ● 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.

16 Binary Search

17 ● 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

18 ● 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

19 Insertion Sort ● There are many algorithms for getting an array into this sorted state. – Insertion sort: ● One of the simplest sorting algorithms.

20 Insertion Sort

21 ● “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.

22 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.

23 Insertion Sort

24 ● Running time of insertion sort depends on the running time of this inner loop. – Best case for inner loop:

25 Insertion Sort ● Worst case data was in reverse order.

26 Insertion Sort ● Average case

27 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 ).

28 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.

29 The Comparable Interface

30 ● 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.

31 The Comparable Interface

32 ● 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.

33 The Comparable Interface

34 ● compareTo() method in the String class uses lexicographical order. – All upper-case letters to be earlier than all lower- case ones.

35 The Comparable Interface

36 ● 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.

37 The Comparable Interface

38 ● 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.

39 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.

40 Sorting Linked Lists

41

42

43 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.

44 Summary

45


Download ppt "Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved."

Similar presentations


Ads by Google