Download presentation

Presentation is loading. Please wait.

Published byRachelle Enzor Modified over 2 years ago

1
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 14: Sorting and Searching

2
2 May 2004 NH-Chapter 14 Objectives ñAfter studying this chapter you should understand the following: ñorderings and the ordering of list elements; ñthe simple sorting algorithms selection sort and bubble sort; ñhow to generalize sort methods. ñthe binary search algorithm. ñthe notion of a loop invariant, and its role in reasoning about methods.

3
3 May 2004 NH-Chapter 14 Objectives ñAlso, you should be able to: ñtrace a selection sort and bubble sort with a specific list of values; ñsort a list by instantiating an ordering and using a predefined sort method; ñtrace a binary search with a specific list of values; ñstate and verify key loop invariants.

4
4 May 2004 NH-Chapter 14 Ordering lists ñTo order a list, there must be an order on the element class. ñWe’ll assume There is a boolean method inOrder defined for the class whose instances we want to order.

5
5 May 2004 NH-Chapter 14 Ordering lists Thus if s1 and s2 are Student objects, inOrder(s1,s2) true: s1 comes before s2. inOrder(s1,s2) false: s1 need not come before s2. ñExample: to order a List need public boolean inOrder (Student first, Student second)

6
6 May 2004 NH-Chapter 14 Ordering lists Ordering alphabetically by name, inOrder(s1,s2) is true if s1 ’s name preceded s2 ’s name lexicographically. Ordering by decreasing grade, inOrder(s1,s2) is true if s1 ’s grade was greater than s2 ’s.

7
7 May 2004 NH-Chapter 14 Order properties ñWe write s1 < s2 when inOrder(s1,s2) == true ñs1 >= s2 when inOrder(s1,s2)== false An ordering is antisymmetric: it cannot be the case that both s1 < s2 and s2 < s1. An ordering is transitive. That is, if s1 < s2 and s2 < s3 for objects s1, s2, and s3, then s1 < s3.

8
8 May 2004 NH-Chapter 14 Order properties Equivalence of objects: neither inOrder(s1,s2) nor inOrder(s2,s1) is true for objects s1 and s2. ñ Two equivalent objects do not have to be equal.

9
9 May 2004 NH-Chapter 14 Ordered list ñOr ñA list is ordered: s1 < s2, then s1 comes before s2 on the list: for all indexes i, j : inOrder(list.get(i),list.get(j)) implies i < j. for all indexes i and j, i < j implies !inOrder(list.get(j),list.get(i)).

10
10 May 2004 NH-Chapter 14 ñDesign: ñFind the smallest element in the list, and put it in as first. ñFind the second smallest and put it as second, etc. Selection Sort

11
11 May 2004 NH-Chapter 14 Selection Sort (cont.) ñFind the smallest. ñInterchange it with the first. ñFind the next smallest. ñInterchange it with the second.

12
12 May 2004 NH-Chapter 14 Selection Sort (cont.) ñFind the next smallest. ñInterchange it with the third. ñFind the next smallest. ñInterchange it with the fourth.

13
13 May 2004 NH-Chapter 14 ñTo interchange items, we must store one of the variables temporarily. Selection sort ñWhile making list.get(0) refer to list.get(2), loose reference to original entry referenced by list.get(0).

14
14 May 2004 NH-Chapter 14 Selection sort algorithm /** * Sort the specified List using selection sort. * @ensure *for all indexes i, j: *inOrder(list.get(i),list.get(j)) implies i < j. */ public void sort (List list) { int first;// index of first element to consider on this step int last;// index of last element to consider on this step int small;// index of smallest of list.get(first)...list.get(last) last = list.size() - 1; first = 0; while (first < last) { small = smallestOf(list,first,last); interchange(list,first,small); first = first+1; }

15
15 May 2004 NH-Chapter 14 Selection sort algorithm /** * Index of the smallest of * list.get(first) through list.get(last) */ private int smallestOf (List list, int first, int last) { int next;// index of next element to examine. int small;// index of the smallest of get(first)...get(next-1) small = first; next = first+1; while (next <= last) { if (inOrder(list.get(next),list.get(small))) small = next; next = next+1; } return small; }

16
16 May 2004 NH-Chapter 14 Selection sort algorithm /** * Interchange list.get(i) and list.get(j) *require * 0 <= i < list.size() && 0 <= j < list.size() *ensure * list.old.get(i) == list.get(j) * list.old.get(j) == list.get(i) */ private void interchange (List list, int i, int j) { Student temp = list.get(i); list.set(i, list.get(j)); list.set(j, temp); }

17
17 May 2004 NH-Chapter 14 ñIf there are n elements in the list, the outer loop is performed n-1 times. The inner loop is performed n-first times. i.e. time= 1, n-1 times; time=2, n-2 times; … time=n-2, 1 times. ñ(n-1)x(n-first) = (n-1)+(n-2)+…+2+1 = (n 2 -n)/2 ñAs n increases, the time to sort the list goes up by this factor (order n 2 ). Analysis of Selection sort

18
18 May 2004 NH-Chapter 14 Bubble sort ñMake a pass through the list comparing pairs of adjacent elements. ñIf the pair is not properly ordered, interchange them. ñAt the end of the first pass, the last element will be in its proper place. ñContinue making passes through the list until all the elements are in place.

19
19 May 2004 NH-Chapter 14 Pass 1

20
20 May 2004 NH-Chapter 14 Pass 2

21
21 May 2004 NH-Chapter 14 Pass 3 Pass 4

22
22 May 2004 NH-Chapter 14 Bubble sort algorithm // Sort specified List using bubble sort. public void sort (List list) { int last;// index of last element to position on this pass last = list.size() - 1; while (last > 0) { makePassTo(list, last); last = last-1; } // Make a pass through the list, bubbling an element to position last. private void makePassTo (List list, int last) { int next; // index of next pair to examine. next = 0; while (next < last) { if (inOrder(list.get(next+1),list.get(next))) interchange(list, next, next+1); next = next+1; }

23
23 May 2004 NH-Chapter 14 Fine-tuning bubble sort algorithm ñMaking pass through list no elements interchanged then the list is ordered. If list is ordered or nearly so to start with, can complete sort in fewer than n - 1 passes. ñWith mostly ordered lists, keep track of whether or not any elements have been interchanged in a pass.

24
24 May 2004 NH-Chapter 14 Generalizing the sort methods ñSorting algorithms are independent of: the method inOrder, as long as it satisfies ordering requirements. ñThe elements in the list being sorted.

25
25 May 2004 NH-Chapter 14 Generalizing the sort methods ñThus: ñNeed to learn about generic methods. ñNeed to make the inOrder method part of a class. ñWant to generalize the sort to List instances with the following specification: public void selectionSort ( List list, Order order)

26
26 May 2004 NH-Chapter 14 Generic methods ñCan define a method with types as parameters. ñMethod type parameters are enclosed in angles and appear before the return type in the method heading.

27
27 May 2004 NH-Chapter 14 Generic methods swap is now a generic method: it can swap to list entries of any given type. ñIn the method definition: Method type parameter public void swap (List list, int i, int j) { Element temp = list.get(i); list.set(i,list.get(j)); list.set(j,temp); }

28
28 May 2004 NH-Chapter 14 Generic swap When swap is invoked, first argument will be a List of some type of element, and local variable temp will be of that type. ñNo special syntax required to invoke a generic method. ñWhen swap is invoked, the type to be used for the type parameter is inferred from the arguments.

29
29 May 2004 NH-Chapter 14 Generic swap For example, if roll is a List, List roll = … And the method swap is invoked as swap(roll,0,1); Type parameter Element is Student, inferred from roll. The local variable temp will be of type Student.

30
30 May 2004 NH-Chapter 14 inOrder as function object ñA concrete order will implement this interface for some particular Element. Wrap up method inOrder in an object to pass it as an argument to sort. ñDefine an interface /** * transitive, and anti-symmetric order on Element instances */ public interface Order { boolean inOrder (Element e1, Element e2); }

31
31 May 2004 NH-Chapter 14 Implementing Order interface ñTo sort a list of Student by grade, define a class (GradeOrder) implementing the interface, and then instantiated the class to obtain the required object. //Order Students by decreasing finalGrade class GradeOrder implements Order { public boolean inOrder (Student s1, Student s2) { return s1.finalGrade() > s2.finalGrade(); }

32
32 May 2004 NH-Chapter 14 Anonymous classes ñThis expression ñdefines an anonymous class implementing interface Order, and ñcreates an instance of the class. ñDefine the class and instantiate it in one expression. ñFor example, new Order () { boolean inOrder(Student s1, Student s2) { return s1.finalGrade() > s2.finalGrade(); }

33
33 May 2004 NH-Chapter 14 Generalizing sort using generic methods ñGeneralized sort methods have both a list and an order as parameters. public class Sorts { public static void selectionSort ( List list, Order order) {…} public static void bubbleSort ( List list, Order order) {… } }

34
34 May 2004 NH-Chapter 14 Generalizing sort using generic methods The order also gets passed to auxiliary methods. The selection sort auxiliary method smallestOf will be defined as follows: private static int smallestOf ( List list, int first, int last, Order order ) {…}

35
35 May 2004 NH-Chapter 14 Sorting a roll by grade Or, using anonymous classes: If roll is a List, to sort it invoke: Sorts.selectionSort(roll, new GradeOrder()); Sorts.selectionSort(roll, new Order () { boolean inOrder(Student s1, Student s2) { return s1.finalGrade() > s2.finalGrade(); } );

36
36 May 2004 NH-Chapter 14 Sorts as generic objects ñwrap sort algorithm and ordering in the same object. ñDefine interface Sorter : //A sorter for a List. public interface Sorter { //e1 precedes e2 in the sort ordering. public boolean inOrder (Element e1, Element e2); //Sort specified List according to this.inOrder. public void sort (List list); }

37
37 May 2004 NH-Chapter 14 Sorts as generic objects ñProvide specific sort algorithms in abstract classes, leaving the ordering abstract. public abstract class SelectionSorter implements Sorter { // Sort the specified List using selection sort. public void sort (List list) { … } } Selection sort algorithm

38
38 May 2004 NH-Chapter 14 Sorts as generic objects ñTo create a concrete Sorter, we extend the abstract class and furnish the order: class GradeSorter extends SelectionSorter { public boolean inOrder (Student s1, Student s2){ return s1.finalGrade() > s2.finalGrade(); }

39
39 May 2004 NH-Chapter 14 Sorts as generic objects ñUsing an anonymous class, ñInstantiate the class to get an object that can sort: GradeSorter gradeSorter = new GradeSorter(); gradeSorter.sort(roll); SelectionSorter gradeSorter = new SelectionSorter () { public boolean inOrder (Student s1, Student s2){ return s1.finalGrade() > s2.finalGrade(); } }; gradeSorter.sort(roll);

40
40 May 2004 NH-Chapter 14 Ordered Lists ñTypically need to maintain lists in specific order. ñWe treat ordered and unordered lists in different ways. ñmay add an element to the end of an unordered list but want to put the element in the “right place” when adding to an ordered list. ñInterface OrderedList ( does not extend List) public interface OrderedList A finite ordered list.

41
41 May 2004 NH-Chapter 14 Ordered Lists ñOrderedList shares features from List, but does not include those that may break the ordering, such as ñpublic void add(int index, Element element); ñpublic void set( List element, int i, int j); ñOrderedList invariant: for all indexes i, j : ordering().inOrder(get(i),get(j)) implies i < j. ñOrderedList add method is specified as: public void add (Element element) Add the specified element to the proper place in this OrderedList.

42
42 May 2004 NH-Chapter 14 Binary Search ñAssumes an ordered list. ñLook for an item in a list by first looking at the middle element of the list. ñEliminate half the list. ñRepeat the process.

43
43 May 2004 NH-Chapter 14 Binary Search for 42 list.get(7) < 42 No need to look below 8 list.get(11) > 42 No need to look above 10 list.get(9)<42 No need to look below 10 Down to one element, at position 10; this isn’t what we’re looking for, so we can conclude that 42 is not in the list.

44
44 May 2004 NH-Chapter 14 Generic search method itemIndex It returns an index such that all elements prior to that index are smaller than item searched for, and all of items from the index to end of list are not. private int itemIndex (Element item, List list, Order order) Proper place for item on list found using binary search. require: list is sorted according to order. ensure: 0 = result implies !order.inOrder(list.get(i),item)

45
45 May 2004 NH-Chapter 14 Implementation of itemIndex private int itemIndex (Element item, List list, Order order) { int low; // the lowest index being examined int high; // the highest index begin examined // for all indexes i: i < low implies order.inOrder(list.get(i),item) // for all indexes i: i > high implies !order.inOrder(list.get(i),item) int mid; // the middle item between low and high. mid == (low+high)/2 low = 0; high = list.size() - 1; while (low <= high) { mid = (low+high)/2; if (order.inOrder(list.get(mid),item)) low = mid+1; else high = mid-1; } return low; }

46
46 May 2004 NH-Chapter 14 Searching for 42 in 42item 14high 0low ?mid ???? (5)(6)(8)(7) ? (9) ???? (10)(11)(13)(12) ? (14) ???? (0)(1)(3)(2) ? (4) low high

47
47 May 2004 NH-Chapter 14 Searching for 42 42item 14high 8low 7?mid s?s28 (5)(6)(8)(7) ? (9) ???? (10)(11)(13)(12) ? (14) ssss (0)(1)(3)(2) s (4) lowhigh

48
48 May 2004 NH-Chapter 14 Searching for 42 42item 10high 8low 11mid s?s28 (5)(6)(8)(7) ? (9) ?g56g (10)(11)(13)(12) g (14) ssss (0)(1)(3)(2) s (4) lowhigh

49
49 May 2004 NH-Chapter 14 Searching for 42 42item 10high 8low 10mid sss28 (5)(6)(8)(7) 33 (9) ?g56g (10)(11)(13)(12) g (14) ssss (0)(1)(3)(2) s (4) lowhigh

50
50 May 2004 NH-Chapter 14 Searching for 42 42item 10high 11low 10mid sss28 (5)(6)(8)(7) 33 (9) 40g56g (10)(11)(13)(12) g (14) ssss (0)(1)(3)(2) s (4) lowhigh 42 is not found using itemIndex algorithm

51
51 May 2004 NH-Chapter 14 Searching for 12 12item 14high 0low ?mid ???? (5)(6)(8)(7) ? (9) ???? (10)(11)(13)(12) ? (14) ???? (0)(1)(3)(2) ? (4) low high

52
52 May 2004 NH-Chapter 14 Searching for 12 12item 6high 0low 7mid ?g?28 (5)(6)(8)(7) g (9) gggg (10)(11)(13)(12) g (14) ???? (0)(1)(3)(2) ? (4) low high

53
53 May 2004 NH-Chapter 14 Searching for 12 12item 2high 0low 3mid ggg28 (5)(6)(8)(7) g (9) gggg (10)(11)(13)(12) g (14) ?12?? (0)(1)(3)(2) g (4) lowhigh

54
54 May 2004 NH-Chapter 14 Searching for 12 12item 2high 2low 1mid ggg28 (5)(6)(8)(7) g (9) gggg (10)(11)(13)(12) g (14) s125? (0)(1)(3)(2) g (4) lowhigh

55
55 May 2004 NH-Chapter 14 Searching for 12 12item 2high 3low 2mid ggg28 (5)(6)(8)(7) g (9) gggg (10)(11)(13)(12) g (14) s125? (0)(1)(3)(2) g (4) lowhigh 12 found in list at index 3

56
56 May 2004 NH-Chapter 14 indexOf using binary search /** * Uses binary search to find where and if an element is in a list. * require: item != null * ensure: *if item == no element of list indexOf(item, list) == -1 *else item == list.get(indexOf(item, list)), * and indexOf(item, list) is the smallest value for which this is true */ public int indexOf (Element item, List list, Order order) { int i = itemIndex(item, list, order); if (i < list.size() && list.get(i).equals(item)) return i; else return -1; }

57
57 May 2004 NH-Chapter 14 Recall sequential (linear) search public int indexOf (Element element) { int i = 0;// index of the next element to examine while (i < this.size() && !this.get(i).equals(element)) i = i+1; if (i < this.size()) return i; else return -1; }

58
58 May 2004 NH-Chapter 14 Relative algorithm efficiency ñNumber of steps required by the algorithm with a list of length n grows in proportion to ñSelection sort:n 2 ñ Bubble sort:n2 ñ Linear search:n ñ Binary search:log 2 n

59
59 May 2004 NH-Chapter 14 Loop invariant ñLoop invariant: condition that remains true as we repeatedly execute loop body; it captures the fundamental intent in iteration. ñPartial correctness: assertion that loop is correct if it terminates. ñTotal correctness: assertion that loop is both partially correct, and terminates.

60
60 May 2004 NH-Chapter 14 Loop invariant ñloop invariant: ñit is true at the start of execution of a loop; ñ remains true no matter how many times loop body is executed.

61
61 May 2004 NH-Chapter 14 Correctness of itemIndex algorithm 1.private int itemIndex (Element item, List list, Order order) { 2.int low = 0; 3.int high = list.size() - 1; 4.while (low <= high) { 5.mid = (low+high)/2; 6.if (order.inOrder(list.get(mid),item)) 7.low = mid+1; 8.else 9.high = mid-1; 10.} 11.return low; 12.}

62
62 May 2004 NH-Chapter 14 Key invariant ñPurpose of method is to find index of first list element greater than or equal to a specified item. Since method returns value of variable low, we want low to satisfy this condition when the loop terminates for all indexes i : i < low implies order.inOrder(list.get(i),item) for all indexes i : i >= low implies !order.inOrder(list.get(i),item)

63
63 May 2004 NH-Chapter 14 Key invariant ñThis holds true at all four key places (a, b, c, d). ñIt’s vacuously true for indexes less than low or greater than high (a) ñWe assume it holds after merely testing the condition (b) and (d) ñIf condition holds before executing the if statement and list is sorted in ascending order, it will remain true after executing the if statement (condition c).

64
64 May 2004 NH-Chapter 14 Key invariant ñWe are guaranteed that for 0 <= i < mid order.inOrder(list.get(i), item) ñAfter the assignment, low equals mid+1 and so for 0 <= i < low order.inOrder( list.get(i), item) ñThis is true before the loop body is done: for high < i < list.size( !order.inOrder( list.get(i), item)

65
65 May 2004 NH-Chapter 14 Partial correctness If loop body is not executed at all, and point (d) is reached with low == 0 and high == -1. If the loop body is performed, at line 6, low <= mid <= high. low <= high becomes false only if mid == high and low is set to mid + 1 or low == mid and high is set to mid - 1 In each case, low == high + 1 when loop is exited.

66
66 May 2004 NH-Chapter 14 Partial correctness ñThe following conditions are satisfied on loop exit: ñlow == high+1 ñfor all indexes i: i < low implies ñorder.inOrder(list.get(i),item) ñfor all indexes i: i > high implies ñ!order.inOrder(list.get(i),item) ñwhich imply ñfor all indexes i: i < low implies ñorder.inOrder(list.get(i),item) ñfor all indexes i: i >= low implies ñ!order.inOrder(list.get(i),item)

67
67 May 2004 NH-Chapter 14 Loop termination When the loop is executed, mid will be set to a value between high and low. ñThe if statement will either cause low to increase or high to decrease. This can happen only a finite number of times before low becomes larger than high.

68
68 May 2004 NH-Chapter 14 Summary ñSorting and searching are two fundamental list operations. ñExamined two simple sort algorithms, selection sort and bubble sort. ñBoth of these algorithms make successive passes through the list, getting one element into position on each pass. ñThey are order n 2 algorithms: time required for the algorithm to sort a list grows as the square of the length of the list. ñWe also saw a simple modification to bubble sort that improved its performance on a list that was almost sorted.

69
69 May 2004 NH-Chapter 14 Summary ñConsidered how to generalize sorting algorithms so that they could be used for any type list and for any ordered. ñWe proposed two possible homes for sort algorithms: ñstatic generic methods, located in a utility class; ñabstract classes implementing a Sorter interface. ñWith later approach, we can dynamically create “sorter objects” to be passed to other methods. ñIntroduced Java’s anonymous class construct. ñ in a single expression we can create and instantiate a nameless class that implements an existing interface or extends an existing class.

70
70 May 2004 NH-Chapter 14 Summary ñConsidered OrderedList container. ñDeveloped binary search: search method for sorted lists. ñAt each step of the algorithm, the middle of the remaining elements is compared to the element being searched for. ñHalf the remaining elements are eliminated from consideration. ñMajor advantage of binary search: it looks at only log 2 n elements to find an item on a list of length n.

71
71 May 2004 NH-Chapter 14 Summary ñTwo steps were involved in verifying the correctness of the iteration in evaluating the correctness of binary search algorithm: ñFirst, demonstrated partial correctness: iteration is correct if it terminates. ñfound a key loop invariant that captured the essential behavior of the iteration. ñSecond, showed that iteration always terminates.

72
72 May 2004 NH-Chapter 14 Summary ñA loop invariant is a condition that remains true no matter how many times the loop body is performed. ñThe key invariant insures that when the loop terminates it has satisfied its purpose. ñVerification of the key invariant provides a demonstration of partial correctness.

Similar presentations

OK

1 Arrays … The Sequel Applications and Extensions Chapter 10.

1 Arrays … The Sequel Applications and Extensions Chapter 10.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on charge-coupled device images Ppt online training Ppt on coalition governments Accounting standard 20 earning per share ppt online Ppt on file system in unix script Ppt on pin diode spice Ppt on pre-ignition piston Ppt on bill gates leadership Ppt on creativity and innovation management syllabus Latest ppt on global warming