chapter 14: sorting and searching
DESCRIPTION
Chapter 14: Sorting and Searching. 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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/1.jpg)
An Introduction to Programming and Object
Oriented Design using Java2nd Edition. May 2004
Jaime NiñoFrederick Hosch
Chapter 14: Sorting and Searching
![Page 2: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/2.jpg)
2May 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.
![Page 3: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/3.jpg)
3May 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.
![Page 4: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/4.jpg)
4May 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.
![Page 5: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/5.jpg)
5May 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<Student> need
public boolean inOrder (Student first, Student second)
![Page 6: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/6.jpg)
6May 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.
![Page 7: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/7.jpg)
7May 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.
![Page 8: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/8.jpg)
8May 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.
![Page 9: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/9.jpg)
9May 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)).
![Page 10: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/10.jpg)
10May 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
![Page 11: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/11.jpg)
11May 2004 NH-Chapter 14
Selection Sort (cont.) Find the smallest.
Interchange it with the first.
Find the next smallest.
Interchange it with the second.
![Page 12: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/12.jpg)
12May 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.
![Page 13: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/13.jpg)
13May 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).
![Page 14: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/14.jpg)
14May 2004 NH-Chapter 14
Selection sort algorithm/** * Sort the specified List<Student> using selection sort. * @ensure * for all indexes i, j: * inOrder(list.get(i),list.get(j)) implies i < j. */public void sort (List<Student> list) {
int first; // index of first element to consider on this stepint last; // index of last element to consider on this stepint 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;
}}
![Page 15: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/15.jpg)
15May 2004 NH-Chapter 14
Selection sort algorithm/** * Index of the smallest of * list.get(first) through list.get(last)*/private int smallestOf (List<Student> 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;
}
![Page 16: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/16.jpg)
16May 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<Student> list,
int i, int j) {Student temp = list.get(i);list.set(i, list.get(j));list.set(j, temp);
}
![Page 17: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/17.jpg)
17May 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 = (n2-n)/2 As n increases, the time to sort the list goes up by this factor
(order n2).
Analysis of Selection sort
![Page 18: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/18.jpg)
18May 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.
![Page 19: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/19.jpg)
19May 2004 NH-Chapter 14
Pass 1
![Page 20: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/20.jpg)
20May 2004 NH-Chapter 14
Pass 2
![Page 21: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/21.jpg)
21May 2004 NH-Chapter 14
Pass 3
Pass 4
![Page 22: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/22.jpg)
22May 2004 NH-Chapter 14
Bubble sort algorithm// Sort specified List<Student> using bubble sort.public void sort (List<Student> list) {
int last; // index of last element to position on this passlast = 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<Student> 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;}
}
![Page 23: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/23.jpg)
23May 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.
![Page 24: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/24.jpg)
24May 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.
![Page 25: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/25.jpg)
25May 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<Element> instances with the following specification:
public <Element> void selectionSort (List<Element> list, Order<Element> order)
![Page 26: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/26.jpg)
26May 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.
![Page 27: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/27.jpg)
27May 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 <Element> void swap (List<Element> list, int i, int j) {Element temp = list.get(i);list.set(i,list.get(j));list.set(j,temp);
}
![Page 28: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/28.jpg)
28May 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.
![Page 29: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/29.jpg)
29May 2004 NH-Chapter 14
Generic swap For example, if roll is a List<Student>,
List<Student> 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.
![Page 30: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/30.jpg)
30May 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<Element> {
boolean inOrder (Element e1, Element e2);}
![Page 31: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/31.jpg)
31May 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 finalGradeclass GradeOrder implements Order<Student> {
public boolean inOrder (Student s1, Student s2) {return s1.finalGrade() > s2.finalGrade();
}}
![Page 32: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/32.jpg)
32May 2004 NH-Chapter 14
Anonymous classes
This expression defines an anonymous class implementing interface
Order<Student>, and creates an instance of the class.
Define the class and instantiate it in one expression. For example, new Order<Student>() {
boolean inOrder(Student s1, Student s2) {return s1.finalGrade() > s2.finalGrade();
}}
![Page 33: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/33.jpg)
33May 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 <Element> void selectionSort (
List<Element> list, Order<Element> order) {…}
public static <Element> void bubbleSort (List<Element> list, Order<Element> order) {… }
}
![Page 34: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/34.jpg)
34May 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 <Element> int smallestOf (List<Element> list, int first, int last,Order<Element> order ) {…}
![Page 35: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/35.jpg)
35May 2004 NH-Chapter 14
Sorting a roll by grade
Or, using anonymous classes:
If roll is a List<Student>, to sort it invoke:
Sorts.selectionSort(roll, new GradeOrder());
Sorts.selectionSort(roll,new Order<Student>() {
boolean inOrder(Student s1, Student s2) {return s1.finalGrade() > s2.finalGrade();
}}
);
![Page 36: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/36.jpg)
36May 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<Element>.public interface Sorter<Element> {
//e1 precedes e2 in the sort ordering.public boolean inOrder (Element e1, Element e2);
//Sort specified List<Element> according to this.inOrder.public void sort (List<Element> list);
}
![Page 37: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/37.jpg)
37May 2004 NH-Chapter 14
Sorts as generic objectsProvide specific sort algorithms in abstract classes, leaving the ordering abstract.
public abstract class SelectionSorter<Element> implements Sorter<Element> { // Sort the specified List<Element> using selection sort.public void sort (List<Element> list) { … }
}Selection sort algorithm
![Page 38: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/38.jpg)
38May 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<Student> {public boolean inOrder (Student s1, Student s2){
return s1.finalGrade() > s2.finalGrade();}
}
![Page 39: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/39.jpg)
39May 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<Student> gradeSorter =new SelectionSorter<Student>() {
public boolean inOrder (Student s1, Student s2){return s1.finalGrade() > s2.finalGrade();
}};
gradeSorter.sort(roll);
![Page 40: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/40.jpg)
40May 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<Element>
A finite ordered list.
![Page 41: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/41.jpg)
41May 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> 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.
![Page 42: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/42.jpg)
42May 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.
![Page 43: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/43.jpg)
43May 2004 NH-Chapter 14
Binary Search for 42list.get(7) < 42No need to look below 8
list.get(11) > 42No need to look above 10
list.get(9)<42No 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.
![Page 44: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/44.jpg)
44May 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 <Element> int itemIndex (Element item, List<Element> list, Order<Element> order)
Proper place for item on list found using binary search.
require:list is sorted according to order.
ensure: 0 <= result && result <= list.size() for all indexes i: i < result implies order.inOrder(list.get(i),item) for all indexes i: i >= result implies !order.inOrder(list.get(i),item)
![Page 45: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/45.jpg)
45May 2004 NH-Chapter 14
Implementation of itemIndexprivate <Element> int itemIndex (Element item, List<Element> list, Order<Element> order) {
int low; // the lowest index being examinedint 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;
}
![Page 46: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/46.jpg)
46May 2004 NH-Chapter 14
Searching for 42 in
42item
14high0low
?mid
? ?? ?
(5) (6) (8)(7)
?
(9)
? ?? ?
(10) (11) (13)(12)
?
(14)
? ?? ?
(0) (1) (3)(2)
?
(4)
low high
![Page 47: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/47.jpg)
47May 2004 NH-Chapter 14
Searching for 42
42item
14high8low
7?mid
s ?s 28
(5) (6) (8)(7)
?
(9)
? ?? ?
(10) (11) (13)(12)
?
(14)
s ss s
(0) (1) (3)(2)
s
(4)
low high
![Page 48: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/48.jpg)
48May 2004 NH-Chapter 14
Searching for 42
42item
10high8low
11mid
s ?s 28
(5) (6) (8)(7)
?
(9)
? g56 g
(10) (11) (13)(12)
g
(14)
s ss s
(0) (1) (3)(2)
s
(4)
low high
![Page 49: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/49.jpg)
49May 2004 NH-Chapter 14
Searching for 42
42item
10high8low
10mid
s ss 28
(5) (6) (8)(7)
33
(9)
? g56 g
(10) (11) (13)(12)
g
(14)
s ss s
(0) (1) (3)(2)
s
(4)
low high
![Page 50: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/50.jpg)
50May 2004 NH-Chapter 14
Searching for 42
42item
10high11low
10mid
s ss 28
(5) (6) (8)(7)
33
(9)
40 g56 g
(10) (11) (13)(12)
g
(14)
s ss s
(0) (1) (3)(2)
s
(4)
lowhigh
42 is not found using itemIndex algorithm
![Page 51: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/51.jpg)
51May 2004 NH-Chapter 14
Searching for 12
12item
14high0low
?mid
? ?? ?
(5) (6) (8)(7)
?
(9)
? ?? ?
(10) (11) (13)(12)
?
(14)
? ?? ?
(0) (1) (3)(2)
?
(4)
low high
![Page 52: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/52.jpg)
52May 2004 NH-Chapter 14
Searching for 12
12item
6high0low
7mid
? g? 28
(5) (6) (8)(7)
g
(9)
g gg g
(10) (11) (13)(12)
g
(14)
? ?? ?
(0) (1) (3)(2)
?
(4)
low high
![Page 53: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/53.jpg)
53May 2004 NH-Chapter 14
Searching for 12
12item
2high0low
3mid
g gg 28
(5) (6) (8)(7)
g
(9)
g gg g
(10) (11) (13)(12)
g
(14)
? 12? ?
(0) (1) (3)(2)
g
(4)
low high
![Page 54: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/54.jpg)
54May 2004 NH-Chapter 14
Searching for 12
12item
2high2low
1mid
g gg 28
(5) (6) (8)(7)
g
(9)
g gg g
(10) (11) (13)(12)
g
(14)
s 125 ?
(0) (1) (3)(2)
g
(4)
low high
![Page 55: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/55.jpg)
55May 2004 NH-Chapter 14
Searching for 12
12item
2high3low
2mid
g gg 28
(5) (6) (8)(7)
g
(9)
g gg g
(10) (11) (13)(12)
g
(14)
s 125 ?
(0) (1) (3)(2)
g
(4)
lowhigh
12 found in list at index 3
![Page 56: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/56.jpg)
56May 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 <Element> int indexOf (Element item, List<Element> list, Order<Element> order) {int i = itemIndex(item, list, order);if (i < list.size() && list.get(i).equals(item))
return i;else
return -1;}
![Page 57: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/57.jpg)
57May 2004 NH-Chapter 14
Recall sequential (linear) searchpublic int indexOf (Element element) {
int i = 0; // index of the next element to examinewhile (i < this.size() && !this.get(i).equals(element))i = i+1;
if (i < this.size())return i;elsereturn -1;
}
![Page 58: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/58.jpg)
58May 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: n2
Bubble sort: n2
Linear search: n
Binary search: log2n
![Page 59: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/59.jpg)
59May 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.
![Page 60: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/60.jpg)
60May 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.
![Page 61: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/61.jpg)
61May 2004 NH-Chapter 14
Correctness of itemIndex algorithm1. private <Element> int itemIndex (Element item,
List<Element> list, Order<Element> 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. else9. high = mid-1;10. }11. return low;12.}
![Page 62: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/62.jpg)
62May 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 impliesorder.inOrder(list.get(i),item)
for all indexes i: i >= low implies!order.inOrder(list.get(i),item)
![Page 63: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/63.jpg)
63May 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).
![Page 64: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/64.jpg)
64May 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 sofor 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)
![Page 65: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/65.jpg)
65May 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.
![Page 66: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/66.jpg)
66May 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)
![Page 67: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/67.jpg)
67May 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.
![Page 68: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/68.jpg)
68May 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 n2 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.
![Page 69: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/69.jpg)
69May 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.
![Page 70: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/70.jpg)
70May 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 log2n elements to find an item on a list of length n.
![Page 71: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/71.jpg)
71May 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.
![Page 72: Chapter 14: Sorting and Searching](https://reader035.vdocument.in/reader035/viewer/2022062315/56815c21550346895dc9f71a/html5/thumbnails/72.jpg)
72May 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.