0% found this document useful (0 votes)
6 views13 pages

COE211 Lecture17

The document discusses searching and sorting algorithms, specifically linear and binary search methods, as well as selection and insertion sort techniques. Linear search examines each element sequentially, while binary search efficiently narrows down the search pool by comparing middle elements in a sorted list. Selection sort finds the smallest value and places it correctly, while insertion sort builds a sorted list by inserting elements into their proper positions.

Uploaded by

L A A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

COE211 Lecture17

The document discusses searching and sorting algorithms, specifically linear and binary search methods, as well as selection and insertion sort techniques. Linear search examines each element sequentially, while binary search efficiently narrows down the search pool by comparing middle elements in a sorted list. Selection sort finds the smallest value and places it correctly, while insertion sort builds a sorted list by inserting elements into their proper positions.

Uploaded by

L A A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Outline Polymorphic References

Polymorphism via Inheritance


Polymorphism via Interfaces
Sorting
Searching
Event Processing Revisited
File Choosers and Color Choosers
Sliders
Searching
 Searching is the process
 of finding a target element within a search pool
 The target may or may not be in the search pool
 We want to perform the search efficiently
 by minimizing the number of comparisons
 Let's look at two classic searching approaches:
 linear search and
 binary search
Linear Search
 Linear search
 begins at one end of a list and
 examines each element in turn

 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

 Each comparison eliminates


 approximately half of the remaining data

 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

 The sorting process is based on specific value(s)


 sorting a list of test scores in ascending numeric order
 sorting a list of people alphabetically by last name

 There are many algorithms,


 which vary in efficiency, for sorting a list of items

 We will examine two specific algorithms:


 Selection Sort
 Insertion Sort
Selection Sort

 The approach of Selection Sort:


 select a value and put it in its final place into the list
 repeat for all other values

 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

 Each time, the smallest


 remaining value is found and
 exchanged
 with the element in the "next" position to be filled
Swapping
 The processing of the selection
 sort algorithm includes the swapping of two values
 Swapping requires
 three assignment statements

 and a temporary storage location:


temp = first;
first = second;
second = temp;
Insertion Sort

 The approach of Insertion Sort:


 pick any item and
 insert it into its proper place in a sorted sublist

 repeat until all items have been inserted


Insertion Sort
 In more detail:
 consider the first item to be a sorted sublist (of one item)

 insert the second item into the sorted sublist,


 shifting the first item to make room to insert the new addition

 insert the third item into the sorted sublist (of two items),
 shifting items as necessary

 repeat until all values


 are inserted into their proper positions
Insertion Sort

 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

You might also like