Professional Documents
Culture Documents
Selection
The Selection sort Algorithm
Selection
The Selection sort Algorithm
Selection
The Selection sort Algorithm
At this point, we can view
the array as being split
into two sides:
To the left of the dotted
line is the "sorted side",
and to the right of the
dotted line is the
"unsorted side".
Our goal is to push the
dotted line forward,
increasing the number of
elements in the sorted
side, until the entire array
is sorted.
Selection
The Selection sort Algorithm
Selection
The Selection sort Algorithm
Selection
The Selection sort Algorithm
As you can see, the
sorted side always
contains the smallest
numbers, and those
numbers are sorted
from small to large.
The unsorted side
contains the rest of
the numbers, and
those numbers are in
no particular order.
Selection
The Selection sort Algorithm
Selection
The Selection sort Algorithm
Selection
The Selection sort Algorithm
Selection
The Selection sort Algorithm
Selection
The Selection sort Algorithm
And now the sorted
side has five elements.
In fact, once the
unsorted side is down
to a single element, the
sort is completed.
At this point the 5
smallest elements are
in the sorted side, and
so the one largest
element is left in the
unsorted side.
We are done...
Selection
The Selection sort Algorithm
Selection
Brute-Force Sorting Algorithm
Selection Sort
Scan the array to find its smallest element
and swap it with the first element.
Then, starting with the second element,
scan the elements to the right of it to find
the smallest among them and swap it with
the second elements.
Generally, on pass i (0 i n-2), find the
smallest element in A[i..n-1] and swap it
with A[i]:
Brute-Force
(0 i n-2) i
A ] A[i..n-1]
Analysis of Selection Sort
Brute-Force Polynomial Evaluation
p 0.0
for i n downto 0 do
power 1
for j 1 to i do //compute xi
power power x
p p + a[i] power
Brute-Force
p 0.0
for i n downto 0 do
power 1
for j 1 to i do // xi
power power x
p p + a[i] power
Brute-Force Polynomial Evaluation
p a[0]
power 1
for i 1 to n do
power power x
p p + a[i] power
return p
Brute-Force
p a[0]
power 1
for i 1 to n do
power power x
p p + a[i] power
return p
Sequential Search
Sequential
Sequential Search (cont.)
Sequential
Brute-Force Strengths and Weaknesses
Brute-Force
Summary of Brute Force
Just solve it!
Improve it later.
Brute Force