You are on page 1of 66

COMPUTER SCIENCE 40S

Today: Searching and Sorting


Searching and Sorting
Most programs work with lists of data
These may be kept in arrays, linked lists
(future topic), files, or other data structures
Searching through these lists of data is one of
the most important tasks that programs
perform
For example, you need to perform calculations
on individual entries, or calculations based on
a subset of the list
Searching and Sorting (cont’d)
Scanning through a list and searching for
an item are perhaps the most common
and simplest programming tasks
The use of algorithms for searching and
sorting involves a few tricks
Some more sophisticated algorithms
consider efficiency in their operations
Searching and Sorting (cont’d)
Consider the following array…
0 1 2 3 4

51 75 6 48 88

How can we implement an algorithm to


search for the element 48
Searching and Sorting (cont’d)
Or sort the array from lowest to highest

0 1 2 3 4

51
6 48
75 51
6 75
48 88

Searching and sorting algorithms are used in


many applications for manipulating and
analyzing data
Students let us examine ...

THE LINEAR SEARCH


The Linear Search
The simplest of the search algorithms
is the linear, or sequential search
This works by proceeding from one
element to the next through the
array until the desired element is
found or the entire array has been
searched
Linear Search (cont’d)
For example (search for 48)…

0 1 2 3 4

51 75 6 48 88

Not Not Not Found


Stop
Found Found Found Search

The algorithm could be implemented as:


However, there are several ways to implement the linear search,
for example, we could instead find the last occurrence of the item.
Using the simplicity of java coding, notice how this second version
of the algorithm uses less lines of code
Students let us examine ...

THE BUBBLE SORT


Searching and Sorting
Recall that sorting is the process of putting
items into a designated order either from low
to high or high to low
Many applications require keeping various
lists in order (e.g. alphabetical, numerical,
etc)
We will also see that sorting a list of items is
important for searching through a list of items
Bubble Sort (cont’d)
The bubble sort is a basic sorting algorithm that starts
at the first element of a list of items and proceeds
sequentially to the last
As it moves through the list, the bubble sort compares
each item with the next
If the first item is greater than the next, they are
swapped
Then the second item is swapped with the third (if
necessary) and so on until the end of the list is reached
This allows the items to “bubble up” to its proper
position
Bubble Sort (cont’d)
Examine the array (first pass)…
0 1 2 3 4

17 4 8 24 1

Check the first two, if the second is greater, swap


0 1 2 3 4

4 17 8 24 1
Bubble Sort (cont’d)
Examine the next two
0 1 2 3 4

4 17 8 24 1

If the second is greater, swap


0 1 2 3 4

4 8 17 24 1
Bubble Sort (cont’d)
Examine the next two
0 1 2 3 4

4 8 17 24 1

If the second is greater, swap


0 1 2 3 4

4 8 17 24 1
Bubble Sort (cont’d)
Examine the next two
0 1 2 3 4

4 8 17 24 1

If the second is greater, swap


0 1 2 3 4

4 8 17 1 24
Bubble Sort (cont’d)
Next pass…
0 1 2 3 4

4 8 17 1 24

0 1 2 3 4

4 8 1 17 24
Bubble Sort (cont’d)

0 1 2 3 4

4 8 1 17 24
Bubble Sort (cont’d)
Next pass…
0 1 2 3 4

4 8 1 17 24

0 1 2 3 4

4 1 8 17 24
Bubble Sort (cont’d)

0 1 2 3 4

4 1 8 17 24
Bubble Sort (cont’d)
Next pass…
0 1 2 3 4

4 1 8 17 24

0 1 2 3 4

1 4 8 17 24
Bubble Sort (cont’d)

0 1 2 3 4

1 4 8 17 24

The array is now sorted

The simplest implementation could be


However, there is also other implementations of the bubble sort algorithm to increase
efficiency – such as stopping the loops once the array is sorted, and not checking a value
once it has ‘bubbled’ to its proper location
This is an example of more optimized bubble sort code
Students let us examine ...

THE SELECTION SORT


The Selection Sort
The basic idea behind the selection sort is
similar to how one might sort a sort list of
numbers
You scan the list for the lowest number, then
this is put into a new list and removed from
the original list
This process is repeated until all the items
from the original list are moved to the new list
(in order)
The Selection Sort (cont’d)
For example…
The list is
0 1 2 3 4 copied

51 75 6 48 88

Lowest Item is
found and
0 1 2 3 4
moved to the
front of the new
list 6
The Selection Sort (cont’d)
Continued…
The original item is
0 1 2 3 deleted, and this
process continues on
the original list, moving
51 75 48 88 to the new list

0 1 2 3 4

6 48
The Selection Sort (cont’d)
Continued…
0 1 2

51 75 88

0 1 2 3 4

6 48 51
The Selection Sort (cont’d)
Continued…
0 1

75 88

0 1 2 3 4

6 48 51 75
The Selection Sort (cont’d)
Continued…
0

88

0 1 2 3 4

6 48 51 75 88
The Selection Sort (cont’d)
Continued…
0 The original list
is destroyed

The new list is a copy of the


original but now sorted

0 1 2 3 4

6 48 51 75 88
The Selection Sort (cont’d)
The Selection sort algorithm in pseudo code

An implementation of a selection sort algorithm (and


the utility functions it might need) might look like this…
And now the utility (helper) private methods that this
method relies on…
Of course, this
code can also
be optimized
to not use any
helper methods
and use swaps
to reduce the
storage needed
significantly
This is the more optimized selection sort method (with no utility methods)
Students let us examine ...

THE BINARY SEARCH


The Binary Search
We often sort lists to implement a more
efficient search known as the binary
search
The binary search only works on a
sorted list
The binary search uses a divide and
conquer approach to searching
The Binary Search (cont’d)
This search works by examining the
middle item in a list, if it is the item
sought you’re done
Otherwise, if the number is lower (or
higher) the binary search through the
lower (or higher) half of the list
Keep searching until the item is found
The Binary Search (cont’d)
For example, search for 12…

1 5 8 9 12 20 34

Start at the middle, is it the


search key?
If not, the key is > so look in
the upper half
The Binary Search (cont’d)
Now examine this section

1 5 8 9 12 20 34

Start at the middle (of the new


list), is it the search key?
No, and the key is < so look
in the upper half
The Binary Search (cont’d)
Now examine this section

1 5 8 9 12 20 34

The list now, only has the one item


And it is the one we’re looking for
The Binary Search (cont’d)
The binary search is very efficient
For example, in an array of 100 elements no
more than 8 elements would have to be checked
to find the search item
In an array of 1 million elements no more than 20
elements would have to be checked
In a list of the entire world’s population no more
than 40 checks would have to be made to find
any one person
Although the calculation for midpoint could
be optimized and the need for a 3 part else if
statement is also unnecessary, and the
algorithm could be implemented recursively,
as seem on the next slide

In this algorithm, the private recursive method


is ‘hidden’ by the public method and it uses
no loops
Note that the Selection sort uses a type of
Linear search and the sort algorithm sets
up for the binary search, one of the most
efficient searches there is!

Please consider all the possibilities with


searches and sorts, including
preconditions, speed and efficiency
Students let us examine ...

ADDING TO OUR LARGE EXAMPLE


SEARCHING AND SORTING EXAMPLE
In addition to the previous examples which used
simple integer arrays, we can also incorporate our
search/sort algorithms with ADT structures like
our LinkedList and Generic data types
For this, we will add classes to perform search
and sort functions
To do this we will have to do a comparison on
Node data to determine less than <, greater than
>, and equal to ==
We start by modifying our Node class
SEARCHING AND SORTING EXAMPLE (CONT’D)
We change the signature line of the Node class to:

The code that adds this:


Is being used to enforce the Comparable interface with the
generic data type (T)
Remember that interfaces are used to ensure that classes that
“implements” them ensure that certain methods are overridden
In this case, it ensures that any class used as a generic with the Node
data has enforced this and has overridden the compareTo() method
The compareTo() method is a method that can see if two of the
same objects are greater than >, less than <, or equal to == one another
If you write a custom class and want to use this Tree ADT, you will have
to write a compareTo() method and define how it works with your
class
SEARCHING AND SORTING EXAMPLE (CONT’D)
We also change the signature line of the
LinkedList class so it can also be used for
comparison:

If you are taking the I.B. Computer Science 40S course,


you may also have to modify the signature lines of you
Stack and Queue classes as well
Now we add a Searcher and a Sorter class to a
searchsort package in our projects
And add the same methods…
SEARCHING AND SORTING EXAMPLE (CONT’D)
SEARCHING AND SORTING EXAMPLE (CONT’D)
SEARCHING AND SORTING EXAMPLE (CONT’D)
SEARCHING AND SORTING EXAMPLE (CONT’D)
SEARCHING AND SORTING EXAMPLE (CONT’D)
SEARCHING AND SORTING EXAMPLE (CONT’D)
SEARCHING AND SORTING EXAMPLE (CONT’D)
COMPUTER SCIENCE 40S
Please move
on to today’s
assignment

Wednesday, March 8, 2017

You might also like