Professional Documents
Culture Documents
lEC - 10 - Sorting - Part1
lEC - 10 - Sorting - Part1
we want to sort 60
from smallest to 50
largest 40
30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
1- The Selection Sort 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]
[4] [6]
[5]
The Selection Sort 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]
[4] [6]
[5]
The Selection Sort Algorithm
Sorted side Unsorted side
• Part of the array
is now sorted. 70
60
50
40
30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
size of the 60
sorted side by 50
one element. 40
30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
Sorted side Unsorted side
70
• The process
60
continues... Smallest
50 from
40 unsorted
30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
Sorted side Unsorted side
• The process 70
continues... 60
50
40
30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
Sorted side
is bigger Sorted side Unsorted side
70
60
• The process 50
continues... 40
30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
• The process
Sorted side Unsorted side
keeps adding
one more 70
number to the 60
sorted side. 50
• The sorted side 40
has the smallest 30
numbers,
20
arranged from
small to large. 10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
Sorted side Unsorted side
• We can stop
when the 70
unsorted side 60
has just one 50
number, since 40
that number
30
must be the
largest number. 20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
• The array is now sorted.
• We repeatedly selected 70
the smallest element,
60
and moved this element
to the front of the 50
unsorted side. 40
30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
template <class Item>
void selection_sort(Item data[ ], size_t n)
{
size_t i, j, smallest;
Item temp;
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Largest
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Largest
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Largest
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Largest
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Largest
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
DONE!
Comparison
Data Movement
Sorted
Selection Time Sort 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 O(n)
find smallest key in unsorted part of array O(n)
swap smallest item to front of unsorted array
decrease size of unsorted array by 1
• Selection sort analysis: O(n2)
The best case of sorting is like:
1,2,3,4,5,6,7
The worst case of sorting is like:
7,6,5,4,3,2,1
But with the selection sort method, the time complexity for
each case is the same, because whatever the array is looks like,
the function will go through all the values in it. so the number
of Comparisons is independent of the data.
The time complexity of selection sort
• When there is n value is the array, then during the first time, the
function executes n-1 times, at the second time, it executes n-2
times…
so the time need to sort is n-1+n-2+…+1=n^2
So , the best, average and worst case time complexities of the selection
sort are all O(n^2)
template <class Item>
void selection_sort(Item data[ ], size_t n)
{
size_t i, j, smallest;
Item temp;
keeps the 50
sorted side 40
arranged from 30
small to large.
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Insertion Sort Algorithm
Sorted side Unsorted side
70
60
50
40
30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Insertion Sort Algorithm
Sorted side Unsorted side
• Sometimes 70
we are lucky 60
and the new
50
inserted item
doesn't need 40
to move at 30
all. 20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Insertionsort Algorithm
Sorted side Unsorted side
70
• Sometimes
60
we are lucky
50
twice in a
row. 40
30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
How to Insert One Element
Copy the new Sorted side Unsorted side
element to a
70
separate location.
60
50
70
40
60
50 30
40
20
30
20 10
10
0
0
[1] [2] [3] [4] [5] [6] [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
How to Insert One Element
Shift
elements in 70
the sorted
60
side, creating
an open 50
70
60
space for the 40
50
new element. 30
40
20
30
20 10
10
0
0
[1] [2] [3] [4] [5] [6] [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
How to Insert One Element
Shift
elements in 70
the sorted 60
side, creating
50
70
an open
40
60 space for the
50 new element. 30
40
20
30
20 10
10
0
0
[1] [2] [3] [4] [5] [6] [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
How to Insert One Element
Continue 70
shifting
60
elements...
50
70
40
60
50 30
40
20
30
20 10
10
0
0
[1] [2] [3] [4] [5] [6] [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
How to Insert One Element
Continue
shifting 70
elements... 60
50
70
40
60
50 30
40
20
30
20 10
10
0
0
[1] [2] [3] [4] [5] [6] [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
How to Insert One Element
...until you
reach the
70
location for
60
the new
element. 50
70
40
60
50 30
40
20
30
20 10
10
0
0
[1] [2] [3] [4] [5] [6] [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
How to Insert One Element
Copy the new Sorted side Unsorted side
element back
into the 70
array, at the 60
correct 50
70 location. 40
60
50 30
40
20
30
20 10
10
0
0
[1] [2] [3] [4] [5] [6] [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
How to Insert One Element
20 10
10
0
0
[1] [2] [3] [4] [5] [6] [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
Sorted Result
70
60
50
40
30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
template <class Item>
void insertion_sort(Item data[ ], size_t n)
{
size_t i, j;
Item temp;
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?
3- The Bubble Sort Algorithm
Idea:
• The Bubble Sort algorithm looks at pairs of entries in
the array, and swaps their order if needed.
• Repeatedly pass through the array
• Swaps adjacent elements that are out of order
• Easier to implement, but slower than Insertion sort
An Animated Example
N 8 did_swap true
to_do 7
index
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap false
to_do 7
index 1
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap false
to_do 7
index 1
Swap
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 1
Swap
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 2
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 2
Swap
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 2
Swap
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 3
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 3
Swap
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 3
Swap
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 4
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 4
Swap
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 4
Swap
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 5
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 5
Swap
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 5
Swap
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 6
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 6
Swap
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 6
Swap
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 7
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 7
Swap
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 7
Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
After First Pass of Outer Loop
N 8 did_swap true
to_do 7
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap false
to_do 6
index 1
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap false
to_do 6
index 1
No Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap false
to_do 6
index 2
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap false
to_do 6
index 2
Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 2
Swap
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 3
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 3
Swap
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 3
Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 4
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 4
No Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 5
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 5
Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 5
Swap
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 6
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 6
Swap
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 6
Swap
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
After Second Pass of Outer Loop
N 8 did_swap true
to_do 6
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap false
to_do 5
index 1
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap false
to_do 5
index 1
Swap
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 1
Swap
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 2
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 2
Swap
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 2
Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 3
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 3
No Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 4
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 4
Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 4
Swap
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 5
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 5
Swap
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 5
Swap
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Third Pass of Outer Loop
N 8 did_swap true
to_do 5
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap false
to_do 4
index 1
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap false
to_do 4
index 1
Swap
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 1
Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 2
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 2
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 3
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 4
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 4
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Fourth Pass of Outer Loop
N 8 did_swap true
to_do 4
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 1
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 1
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 2
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 2
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 3
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Fifth Pass of Outer Loop
N 8 did_swap false
to_do 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
Finished “Early”
N 8 did_swap false
to_do 3
We didn’t do any swapping,
index 4 so all of the other elements
must be correctly placed.
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
• “Bubble Up” algorithm will move largest value to its
correct location (to the right)
• Repeat “Bubble Up” until all elements are correctly
placed:
• Maximum of N-1 times
• Can finish early if no swapping occurs
• We reduce the number of elements we compare each
time one is correctly placed
template <class Item>
void bubble_sort(Item data[ ], size_t n)
{
size_t i, j;
Item temp;