COE211 Lecture17
COE211 Lecture17
Eventually,
either the item is found or
the end of the list is encountered
Binary Search
A binary search assumes
the list of items in the search pool is sorted
It eliminates a large part of the search pool
with a single comparison
A binary search
examines the middle element if there is a match
the search is over
If not
only one half of the remaining elements need be searched
Binary Search
The process continues
comparing the middle element of the viable candidates
Eventually,
the target is either found or the data is exhausted
Outline Polymorphic References
Polymorphism via Inheritance
Polymorphism via Interfaces
Sorting
Searching
Event Processing Revisited
File Choosers and Color Choosers
Sliders
Sorting
Sorting is
the process of arranging a list of items in a particular order
In more detail:
find the smallest value in the list
switch it with the value in the first position
find the next smallest value in the list
switch it with the value in the second position
repeat until all values are in their proper places
Selection Sort
An example:
original: 3 9 6 1 2
smallest is 1: 1 9 6 3 2
smallest is 2: 1 2 6 3 9
smallest is 3: 1 2 3 6 9
smallest is 6: 1 2 3 6 9
insert the third item into the sorted sublist (of two items),
shifting items as necessary
An example:
original: 3 9 6 1 2
insert 9: 3 9 6 1 2
insert 6: 3 6 9 1 2
insert 1: 1 3 6 9 2
insert 2: 1 2 3 6 9
See Sorting.java
(page 501), specifically the insertionSort method