Lecture 08
Lecture 08
30
20
10
0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Selection
Selection Sort
Sort Algorithm
Algorithm
• Swap the
smallest 70
entry with 60
the first 50
entry. 40
30
20
10
0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Selection
Selection Sort
Sort Algorithm
Algorithm
• Swap the
smallest 70
entry with 60
the first 50
entry. 40
30
20
10
0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Selection
Selection Sort
Sort Algorithm
Algorithm
Sorted side Unsorted side
70
• Part of the 60
array is now 50
sorted. 40
30
20
10
0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Selection
Selection Sort
Sort Algorithm
Algorithm
Sorted side Unsorted side
• Find the
smallest
element in
the unsorted
side.
• Swap with
the front of
the unsorted
side.
• We have
increased the
size of the
sorted side
by one
element.
• The process
continues... Smallest
Smallest
from
from
unsorted
unsorted
• The process
continues... w aapp
SSw th
wwi ith t
nt
frfroon
• The process
continues...
10
0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Insertion
Insertion Sort
Sort Algorithm
Algorithm
Sorted side Unsorted side
• The sorted 70
side starts 60
• The sorted 70
side grows 60
by taking the 50
front 40
element 30
from the 20
unsorted 10
side... 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Insertion
Insertion Sort
Sort Algorithm
Algorithm
Sorted side Unsorted side
• ...and 70
inserting it 60
in the place 50
that keeps 40
the sorted 30
side 20
arranged 10
from small 0
[1] [2][1] [3] [2] [4] [3][5] [6]
to large. [0] [4]
[5]
The
The Insertion
Insertion Sort
Sort Algorithm
Algorithm
Sorted side Unsorted side
70
60
50
40
30
20
10
0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Insertion
Insertion Sort
Sort Algorithm
Algorithm
Sorted side Unsorted side
• Sometimes 70
we are lucky 60
inserted item 40
doesn't need 30
to move at 20
all. 10
0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Insertionsort
Insertionsort Algorithm
Algorithm
Sorted side Unsorted side
• Sometimes 70
we are lucky 60
twice in a 50
row. 40
30
20
10
0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
How
How to
to Insert
Insert One
One Element
Element
Copy the Sorted side Unsorted side
new element 70
to a separate
60
location.
50
40
30
20
10
0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
How
How to
to Insert
Insert One
One Element
Element
Shift
elements in 70
the sorted
60
side,
50
creating an
40
open space
30
for the new
20
element.
10
0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
How
How to
to Insert
Insert One
One Element
Element
Shift
elements in 70
the sorted
60
side,
50
creating an
40
open space
30
for the new
20
element.
10
0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
How
How to
to Insert
Insert One
One Element
Element
Continue
shifting 70
elements...
60
50
40
30
20
10
0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
How
How to
to Insert
Insert One
One Element
Element
Continue
shifting 70
elements...
60
50
40
30
20
10
0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
How
How to
to Insert
Insert One
One Element
Element
...until you
reach the 70
location for
60
the new
50
element.
40
30
20
10
0
[1]
[0] [2]
[1] [3] [2] [4] [3] [5] [6]
[4]
[5]
How
How to
to Insert
Insert One
One Element
Element
Copy the Sorted side Unsorted sid
new element 70
back into the
60
array, at the
50
correct
40
location.
30
20
10
0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
How
How to
to Insert
Insert One
One Element
Element
• The last Sorted side Unsorted sid
element
must also be
inserted.
Start by
copying it...
data[j] = temp;
}
}
Insertion Sort Time Analysis
• In O-notation, what is:
– Worst case running time for n items?
– Average case running time for n items?
• Steps of algorithm:
for i = 1 to n-1
take next key from unsorted part of array
data[j] = temp;
}
}
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• The Bubble
Sort algorithm 70
looks at pairs 60
of entries in 50
the array, and 40
swaps their 30
order if 20
needed.
10
0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• The Bubble
Sort algorithm 70
looks at pairs 60
of entries in 50
the array, and 40
swaps their 30
order if 20
needed.
10
Swap? 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• The Bubble
Sort algorithm 70
looks at pairs 60
of entries in 50
the array, and 40
swaps their 30
order if 20
needed.
10
Yes! 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• The Bubble
Sort algorithm 70
looks at pairs 60
of entries in 50
the array, and 40
swaps their 30
order if 20
needed.
10
Swap? 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• The Bubble
Sort algorithm 70
looks at pairs 60
of entries in 50
the array, and 40
swaps their 30
order if 20
needed.
10
No. 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• The Bubble
Sort algorithm 70
looks at pairs 60
of entries in 50
the array, and 40
swaps their 30
order if 20
needed.
10
Swap? 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• The Bubble
Sort algorithm 70
looks at pairs 60
of entries in 50
the array, and 40
swaps their 30
order if 20
needed.
10
No. 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• The Bubble
Sort algorithm 70
looks at pairs 60
of entries in 50
the array, and 40
swaps their 30
order if 20
needed.
10
Swap? 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• The Bubble
Sort algorithm 70
looks at pairs 60
of entries in 50
the array, and 40
swaps their 30
order if 20
needed.
10
Yes! 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• The Bubble
Sort algorithm 70
looks at pairs 60
of entries in 50
the array, and 40
swaps their 30
order if 20
needed.
10
Swap? 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• The Bubble
Sort algorithm 70
looks at pairs 60
of entries in 50
the array, and 40
swaps their 30
order if 20
needed.
10
Yes! 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• Repeat.
70
60
50
40
30
20
10
Swap? No. 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• Repeat.
70
60
50
40
30
20
10
Swap? No. 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• Repeat.
70
60
50
40
30
20
10
Swap? Yes. 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• Repeat.
70
60
50
40
30
20
10
Swap? Yes. 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• Repeat.
70
60
50
40
30
20
10
Swap? Yes. 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• Repeat.
70
60
50
40
30
20
10
Swap? Yes. 0
[1]
[0] [2][1] [3] [2] [4] [3][5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• Repeat.
70
60
50
40
30
20
10
Swap? No. 0
[1]
[0] [2]
[1] [3] [2] [4] [3] [5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• Loop over
array n-1 70
times, 60
swapping pairs 50
of entries as 40
needed. 30
20
10
Swap? No. 0
[1]
[0] [2]
[1] [3] [2] [4] [3] [5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• Loop over
array n-1 70
times, 60
swapping pairs 50
of entries as 40
needed. 30
20
10
Swap? Yes. 0
[1]
[0] [2]
[1] [3] [2] [4] [3] [5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• Loop over
array n-1 70
times, 60
swapping pairs 50
of entries as 40
needed. 30
20
10
Swap? Yes. 0
[1]
[0] [2]
[1] [3] [2] [4] [3] [5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• Loop over
array n-1 70
times, 60
swapping pairs 50
of entries as 40
needed. 30
20
10
Swap? Yes. 0
[1]
[0] [2]
[1] [3] [2] [4] [3] [5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• Loop over
array n-1 70
times, 60
swapping pairs 50
of entries as 40
needed. 30
20
10
Swap? Yes. 0
[1]
[0] [2]
[1] [3] [2] [4] [3] [5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• Loop over
array n-1 70
times, 60
swapping pairs 50
of entries as 40
needed. 30
20
10
Swap? No. 0
[1]
[0] [2]
[1] [3] [2] [4] [3] [5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• Loop over
array n-1 70
times, 60
swapping pairs 50
of entries as 40
needed. 30
20
10
Swap? No. 0
[1]
[0] [2]
[1] [3] [2] [4] [3] [5] [6]
[4]
[5]
The
The Bubble
Bubble Sort
Sort Algorithm
Algorithm
• Continue
looping, until 70
done. 60
50
40
30
20
10
Swap? Yes. 0
[1]
[0] [2]
[1] [3] [2] [4] [3] [5] [6]
[4]
[5]
template <class Item>
void bubble_sort(Item data[ ], size_t n)
{
size_t i, j;
Item temp;