You are on page 1of 162

Benha University

Electrical Engineering Department Benha Faculty of Engineering


Today’s discussion…

• Sorting Algorithms (Selection, Insertion , Bubble)


• Algorithm Analysis
• Big O Notation
Objectives
• Determine the running time of simple algorithms in the:
• Best case
• Average case
• Worst case
• Sorting algorithms
• Understand the mathematical basis of O notation
• Use O notation to measure the running time of algorithms
Algorithm Analysis
• It is important to be able to describe the efficiency of algorithms
• Time efficiency
• Space efficiency
• Choosing an appropriate algorithm can make an enormous difference in
the usability of a system e.g.
• Government and corporate databases with many millions of records, which are
accessed frequently
• Online search engines
• Real time systems (from air traffic control systems to computer games) where
near instantaneous response is required
Simple Sorting
• As an example of algorithm analysis let's look at two simple sorting
algorithms
• Selection Sort and
• Insertion Sort
• We'll calculate an approximate cost function for these sorting algorithms
by analyzing exactly how many operations are performed by each
algorithm
• Note that this will include an analysis of how many times the algorithms perform
loops
• Common problem: sort a list of values, starting from lowest to
highest.
• List of exam scores
• Words of dictionary in alphabetical order
• Students names listed alphabetically
• Student records sorted by ID#
• Generally, we are given a list of records that have keys. These keys
are used to define an ordering of the items in the list.
C++ Implementation of Sorting
• Use C++ templates to implement a generic sorting function.
• This would allow use of the same function to sort items of any class.
• However, class to be sorted must provide the following overloaded operators:
• Assignment: =
• Ordering: >, <, ==
• In this lecture, we’ll talk about sorting integers; however, the algorithms are
general and can be applied to any class as described above.
Sorting Algorithms
• Selection sort
• Insertion sort
• Bubble sort
Sorting an Array of Integers
• Example: we are
given an array of
six integers that 70

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

• Start by finding the


smallest entry. 70
60
50
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

• 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

• Find the smallest Sorted side Unsorted side


element in the 70
unsorted side. 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 Unsorted side
70

• Swap with the 60


front of the 50
unsorted side. 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
• We have
increased the 70

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;

if(n < 2) return; // nothing to sort!!

for(i = 0; i < n-1 ; ++i)


{
// find smallest in unsorted part of array
smallest = i;
for(j = i+1; j < n; ++j)
if(data[smallest] > data[j]) smallest = j;

// put it at front of unsorted part of array (swap)


temp = data[i];
data[i] = data[smallest];
data[smallest] = temp;
}
}
Selection Sort
1st. Find the index of the largest element and exchange the position with the
element at the last index.

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;

if(n < 2) return; // nothing to sort!!

for(i = 0; i < n-1 ; ++i) Outer loop:


{ O(n)
// find smallest in unsorted part of array
smallest = i;
for(j = i+1; j < n; ++j)
if(data[smallest] > data[j]) smallest = j;

// put it at front of unsorted part of array (swap)


temp = data[i];
data[i] = data[smallest];
data[smallest] = temp;
}
}
template <class Item>
void selection_sort(Item data[ ], size_t n)
{
size_t i, j, smallest;
Item temp;

if(n < 2) return; // nothing to sort!!

for(i = 0; i < n-1 ; ++i) Outer loop:


{
// find smallest in unsorted part of array O(n)
smallest = i;
for(j = i+1; j < n; ++j) Inner loop:
if(data[smallest] > data[j]) smallest = j; O(n)
// put it at front of unsorted part of array (swap)
temp = data[i];
data[i] = data[smallest];
data[smallest] = temp;
}
}
2- The Insertion Sort Algorithm
The Insertion Sort
algorithm also views the
70
array as having a sorted
60
side and an unsorted
side. 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
• The sorted
side starts 70
with just the 60
first element, 50
which is not 40
necessarily
30
the smallest
element. 20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Insertion Sort Algorithm
Sorted side Unsorted side
• The sorted
70
side grows by
60
taking the
front element 50
from the 40
unsorted 30
side... 20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Insertion Sort Algorithm
Sorted side Unsorted side
• ...and
70
inserting it in
the place that 60

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

• The last Sorted side Unsorted side


element 70
must also be 60
inserted.
50
70Start by
40
60copying it...
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]
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;

if(n < 2) return; // nothing to sort!!

for(i = 1; i < n; ++i)


{
// take next item at front of unsorted part of array
// and insert it in appropriate location in sorted part of array
temp = data[i];
for(j = i; data[j-1] > temp and j > 0; --j)
data[j] = data[j-1]; // shift element forward

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

index 8 Finished first “Bubble Up”

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

index 7 Finished second “Bubble Up”

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

index 6 Finished third “Bubble Up”

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

index 5 Finished fourth “Bubble Up”

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

index 4 Finished fifth “Bubble Up”

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.

We can “skip” the last two


passes of the outer loop.

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;

if(n < 2) return; // nothing to sort!!

for(i = 0; i < n-1; ++i)


{
for(j = 0; j < n-1;++j)
if(data[j] > data[j+1]) // if out of order, swap!
{
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
}
}
}
template <class Item>
void bubble_sort(Item data[ ], size_t n)
{
size_t i, j;
Item temp;
bool swapped = true;

if(n < 2) return; // nothing to sort!!


for(i = 0; swapped and i < n-1; ++i)
{// if no elements swapped in an iteration,
// then elements are in order: done!
for(swapped = false, j = 0; j < n-1;++j)
if(data[j] > data[j+1]) // if out of order, swap!
{
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
swapped = true;
}
}
}
Bubble 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 = 0 to n-1
for j =0 to n-2
if key[j] > key[j+1] then swap
if no elements swapped in this pass through array, done.
otherwise, continue
Timing and Other Issues
• Selection Sort, Insertion Sort, and Bubble Sort all
have a worst-case time of O(n2), making them
impractical for large arrays.
• But they are easy to program, easy to debug.
• Insertion Sort also has good performance when the
array is nearly sorted to begin with.
• But more sophisticated sorting algorithms are
needed when good performance is needed in all
cases for large arrays.
• Next time: Merge Sort, Heap and Quick Sort

You might also like