Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for...
-
date post
19-Dec-2015 -
Category
Documents
-
view
231 -
download
5
Transcript of Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for...
![Page 1: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/1.jpg)
Chapter 5
Efficiency and Analysis
![Page 2: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/2.jpg)
Algorithm selection
Algorithms are ordered lists of steps for solving a problem
Algorithms are also abstractions of the programming process
Often there are many different algorithms for solving the same problem
![Page 3: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/3.jpg)
Algorithms: main considerations
How fast will it run How much memory will it use How easy is it to implement The choice depends on the software
– It may mean your algorithm is not the most efficient
![Page 4: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/4.jpg)
Program efficiency
Computer scientists analyze algorithms for their efficiency– In a machine independent manner– By focussing on the critical operations
performed– Like the number of comparisons
(for sorting routines)
![Page 5: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/5.jpg)
Rate of growth
The most important aspect of algorithm efficiency
Two algorithms may be just as efficient for small data sets but differ greatly for large ones.
![Page 6: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/6.jpg)
Asymptotic Efficiency
The asymptotic efficiency of an algorithm – describes its relative efficiency as n gets
very large. Often called the “order of complexity” “Big O” - O() Example: Sequential search is O(n)
![Page 7: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/7.jpg)
Exercise
– Rank the asymptotic orders of the following functions, from highest to lowest.
(You may wish to graph some or all of them.)
![Page 8: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/8.jpg)
x
![Page 9: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/9.jpg)
x xlog+
![Page 10: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/10.jpg)
x xlog
![Page 11: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/11.jpg)
x2 100+
![Page 12: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/12.jpg)
2x x2+
![Page 13: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/13.jpg)
100
![Page 14: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/14.jpg)
xlog
![Page 15: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/15.jpg)
1.1
![Page 16: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/16.jpg)
Analyzing the Linear Search
Assumes a list of n elements Assumes a search key A linear search looks for the target key value
by proceeding in sequential fashion through the list
![Page 17: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/17.jpg)
Sample record format
key data
![Page 18: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/18.jpg)
Linear Search algorithm
For each item in the list if the item’s key matches the
target, stop and report “success” Report “failure”
![Page 19: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/19.jpg)
Linear Search
int linearSearch(int a[], int n, int target) { int i; for (i = 0; i < n; i++) if (a[i] == target) // key comparison return i; return -1; // use -1 to indicate failure }
![Page 20: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/20.jpg)
Analysis
Speed of an algorithm is measured by counting key comparisons
Best case is 1 comparison Worst case is n comparisons Average case is n/2 comparisons
– if the target is in the list
![Page 21: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/21.jpg)
Analysis (continued)
What if the target we are looking for has only a 50% chance of being in the list.
The complexity must account for both targets that can be found and targets that cannot.
For targets that can be found it is n/2 For targets that cannot be found it is n For targets that can be found only 50% of the time it is:
1/2*n/2 (found) + 1/2*n (not found) The order of complexity therefore is: n/4 + n/2 = 3n/4
![Page 22: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/22.jpg)
Polynomial expressions All polynomial equations are made up of a set of
terms. Usually with coefficients and exponents The fastest growing term is called the term of the
“highest order” A linear search, like the last example, has only one
term to describe it: 3n/4 This can be written as (3/4)n 3/4 is the coefficient, n is the exponent n is also the highest order term
![Page 23: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/23.jpg)
From polynomials to Big-O A linear function is one whose growth is tied to n In other words, n is the highest order polynomial The growth of the expression is tied to the highest
order term and is called the “order of magnitude” Order of magnitude is expressed as Big-O For linear functions, Big-O is n We express this as O(n) To find out what the order of an expression is, look
for the highest order term.
![Page 24: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/24.jpg)
Graphing Big-O The x axis corresponds to the number of elements in
the list The y axis is the number of critical operations
required by an algorithm The order of complexity is the highest order term in
the analysis because it is the one that increases the fastest
Constant multipliers are ignored Example: n+100 is graphed as O(n) For a linear search n/2 is also O(n) Similarly, from last example, 3n/4 is O(n)
![Page 25: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/25.jpg)
A graph of the average case performance of Linear Search
1000
75
n
f(n)
![Page 26: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/26.jpg)
Exercise
– For the following formulas, identify the high-order term and indicate the order of each using big-O notation.
![Page 27: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/27.jpg)
Binary Search Repeatedly divides the list in half and in half again The result is a great improvement on linear
searching What is the asymptotic efficiency?
– Best case is 1 comparison– Worst case is log(base 2)n
Logarithmic complexity is much faster than linear– Even if we use the worst case
![Page 28: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/28.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]
Searching for 26
![Page 29: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/29.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]
Searching for 26
![Page 30: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/30.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]
Searching for 26
![Page 31: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/31.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]
Searching for 26
![Page 32: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/32.jpg)
Equation 5-2
![Page 33: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/33.jpg)
The growth of the base-2 log function
n n (as power of 2) log2n
16 24 4
256 28 8
4,096 212 12
65,536 216 16
1,048,576 220 20
![Page 34: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/34.jpg)
Graph illustrating relative growth of linear and logarithmic functions
0 n
f(n)
log n
n
![Page 35: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/35.jpg)
Defective Binary Search
int binarySearch(int a[], int n, int target) { // Precondition: array a is sorted in ascending order from a[0] to a[n-1] int first(0), last(n - 1), int mid; while (first <= last) { mid = (first + last)/2; if (target == a[mid]) return mid; else if (target < a[mid]) last = mid; else // must be that target > a[mid] first = mid; } return -1; // use -1 to indicate item not found }
![Page 36: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/36.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]8
Searching for 26
First0
Last17
![Page 37: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/37.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]4
Searching for 26
First0
Last8
![Page 38: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/38.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]6
Searching for 26
First4
Last8
![Page 39: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/39.jpg)
Problem: bad loop invariant
This code works fine for finding an item that is contained in the list.
However, it does not work so well if the item is not in the list.
We get an infinite loop in that case. The reason for this is the bad loop
invariant
![Page 40: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/40.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]8
First0
Last17Searching for 24
![Page 41: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/41.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]4
First0
Last8 Searching for 24
![Page 42: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/42.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]6
First4
Last8 Searching for 24
![Page 43: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/43.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]5
Searching for 24First4
Last6
![Page 44: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/44.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]4
First4
Last5
Searching for 24WE ARE NOW STUCKHERE FOREVER!!
![Page 45: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/45.jpg)
Illustrated Invariant for Binary Search
a
first last
target must lie in shaded region
![Page 46: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/46.jpg)
Binary Search Invariant
a[first] <= target <= a[last]This assumes target is in the listThe correct invariant should beif target in a, then a[first] <=
target <= a[last]
![Page 47: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/47.jpg)
Importance of loop invariant
Proving program correctness– Argument values (does the loop have what it
needs to work correctly)– Termination (regardless of it’s correctness, will it
ever stop) We violated the termination requirement of the
loop invariant by not proving that it could be able to stop under all conditions
![Page 48: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/48.jpg)
Proof of correctness
We can show that the original loop invariant is incorrect using a simple trace.
Tracing skills are an important part of your programmers skill set.
The next slide contains an example of a trace for the binary search program that demonstrates the problem of the infinite loop.
![Page 49: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/49.jpg)
Trace of Code Example 5-2
![Page 50: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/50.jpg)
Verified Binary Search int binarySearch(int a[], int n, int target) { // Precondition: array a is sorted in ascending order // from a[0] to a[n-1] int first(0); int last(n - 1); int mid; while (first <= last) { // Invariant:
// if target in a, then a[first] <= target <= a[last] mid = (first + last)/2;
![Page 51: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/51.jpg)
Verified Binary Search
if (target == a[mid]) return mid; else if (target < a[mid]) last = mid - 1; else // must be that target > a[mid] first = mid + 1; } return -1; //use -1 to indicate item not found }
![Page 52: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/52.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]8
First0
Last17Searching for 24
![Page 53: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/53.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]4
First0
Last7
Searching for 24
![Page 54: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/54.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]6
First5
Last7 Searching for 24
![Page 55: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/55.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]5
Searching for 24First5
Last5
![Page 56: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/56.jpg)
Binary search strategy
10 14 15 20 23 25 26 27 31 32 34 37 41 42 44 45 46 49
A[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11][12] [13][14] [15][16] [17]
A[mid]5
Searching for 24PROGRAM TERMINATESWHEN first > last
First5
Last4
![Page 57: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/57.jpg)
Analysis of simple sorting algorithms
Selection sort Bubble sort
![Page 58: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/58.jpg)
Analysis of selection sort
Assume n element list Makes n-1 passes through the list Each pass has a sequential search of
some portion of the n elements Analysis: n * n = O(n2)
![Page 59: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/59.jpg)
int maxSelect(int a[], int n) { int maxPos(0), currentPos(1); while (currentPos < n) { // Invariant: a[maxPos] >= a[0] ... a[currentPos-1] if (a[currentPos] > a[maxPos]) maxPos = currentPos; currentPos++; } return maxPos; }
The maxSelect function
![Page 60: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/60.jpg)
The operation of Selection Sort
a
last
sorted items
n-1
all items in here less than shaded items
![Page 61: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/61.jpg)
Selection Sort
void selectionSort(int a[], int n) { int last(n-1); int maxPos; while (last > 0) { // invariant: a[last+1] ... a[n-1] is sorted && // everything in a[0] ... a[last] <= everything in a[last+1] ... a[n-1] maxPos = maxSelect(a, last+1); // last+1 is length from 0 to last swapElements(a, maxPos, last); last--; } }
![Page 62: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/62.jpg)
Selection Sort example
2
7
3
5
6
0
1
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last4
![Page 63: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/63.jpg)
Selection Sort example
2
7
3
5
6
1
1
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last4
![Page 64: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/64.jpg)
Selection Sort example
2
7
3
5
6
1
2
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last4
![Page 65: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/65.jpg)
Selection Sort example
2
7
3
5
6
1
3
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last4
![Page 66: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/66.jpg)
Selection Sort example
2
7
3
5
6
1
4
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last4
![Page 67: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/67.jpg)
Swap, after one pass
2
6
3
5
7
1
4
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last4
![Page 68: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/68.jpg)
Selection Sort
2
6
3
5
7
0
1
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last3
![Page 69: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/69.jpg)
Selection Sort
2
6
3
5
7
1
1
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last3
![Page 70: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/70.jpg)
Selection Sort
2
6
3
5
7
1
2
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last3
![Page 71: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/71.jpg)
Selection Sort
2
6
3
5
7
1
3
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last3
![Page 72: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/72.jpg)
Selection Sort
2
5
3
6
7
1
3
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last3
![Page 73: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/73.jpg)
Selection Sort
2
5
3
6
7
0
1
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last2
![Page 74: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/74.jpg)
Selection Sort
2
5
3
6
7
1
1
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last2
![Page 75: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/75.jpg)
Selection Sort
2
5
3
6
7
1
2
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last2
![Page 76: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/76.jpg)
Selection Sort
2
3
5
6
7
1
2
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last2
![Page 77: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/77.jpg)
Selection Sort
2
3
5
6
7
0
1
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last1
![Page 78: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/78.jpg)
Selection Sort
2
3
5
6
7
1
1
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last1
![Page 79: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/79.jpg)
Selection Sort
2
3
5
6
7
0
1
5maxPos
currPos
nA[0]A[1]A[2]A[3]A[4] last0
![Page 80: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/80.jpg)
Result after each pass
2
7
3
5
6
A[0]A[1]A[2]A[3]A[4]
2
6
3
5
7
![Page 81: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/81.jpg)
Result after each pass
2
7
3
5
6
A[0]A[1]A[2]A[3]A[4]
2
6
3
5
7
2
5
3
6
7
![Page 82: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/82.jpg)
Result after each pass
2
7
3
5
6
A[0]A[1]A[2]A[3]A[4]
2
6
3
5
7
2
5
3
6
7
![Page 83: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/83.jpg)
Result after each pass
2
7
3
5
6
A[0]A[1]A[2]A[3]A[4]
2
6
3
5
7
2
5
3
6
7
2
3
5
6
7
![Page 84: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/84.jpg)
Result after each pass
2
7
3
5
6
A[0]A[1]A[2]A[3]A[4]
2
6
3
5
7
2
5
3
6
7
2
3
5
6
7
![Page 85: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/85.jpg)
Result after each pass
2
7
3
5
6
A[0]A[1]A[2]A[3]A[4]
2
6
3
5
7
2
5
3
6
7
2
3
5
6
7
2
3
5
6
7
![Page 86: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/86.jpg)
Result after each pass
2
7
3
5
6
A[0]A[1]A[2]A[3]A[4]
2
6
3
5
7
2
5
3
6
7
2
3
5
6
7
2
3
5
6
7
![Page 87: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/87.jpg)
Result after each pass
2
7
3
5
6
A[0]A[1]A[2]A[3]A[4]
2
6
3
5
7
2
5
3
6
7
2
3
5
6
7
2
3
5
6
7
2
3
5
6
7
![Page 88: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/88.jpg)
Analysis
Number of passes: n-1– the first pass guaranteed to place 1 item– the second guarantees a second– the third a third, etc.– After n-1 passes we have them all in place
![Page 89: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/89.jpg)
Analysis: comparisons
In the first pass we compared n-1 pairs In the second, n-2 In the third, n-3, etc. Actual number of comparisons made
across all passes, for this example was: 4+3+2+1 = 10
![Page 90: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/90.jpg)
Order of complexity
The number of passes is O(n) The number of comparisons in each pass
is also O(n) Therefore, the order of complexity of the
sort is O(n*n) = O(n2)
![Page 91: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/91.jpg)
Analysis of selection sort (con’t)
What if the list is sorted to begin with? This selection sort is a mindless one
– it would not know that it should stop. Best case and worst case are the same
(except for the swaps) The best, worst and average cases are all
quadratic algorithms with O(n2)
![Page 92: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/92.jpg)
Bubble Sort
Also n-1 passes Also n-pass comparisons in each pass Order of complexity is therefore n-squared The same as the selection sort The main difference is that swapping may
occur as many as n-1 times on a single pass, with the selection sort it only occurs once, at the end of the pass.
![Page 93: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/93.jpg)
Example of one phase of Bubble Sort
17 9 21 6 3 32 37 41 45
179 21 6 3 32 37 41 45
179 21 6 3 32 37 41 45
179 216 3 32 37 41 45
179 216 3 32 37 41 45
compare
compare
compare
compare
These items are sorted
![Page 94: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/94.jpg)
bubbleSortPhase // void swapElements(int a[], int maxPos, int last);
void bubbleSortPhase(int a[], int last) { // Precondition: a is an array indexed from a[0] to a[last] // Move the largest element between a[0] and a[last] into a[last], // by swapping out of order pairs int pos;
![Page 95: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/95.jpg)
bubbleSortPhase
for (pos = 0; pos < last - 1; pos++) if (a[pos] > a[pos+1]) { swapElements(a, pos, pos+1); } // Postconditions: a[0] ... a[last]
// contain the same elements, // possibly reordered; a[last] >= a[0] ... a[last-1] }
![Page 96: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/96.jpg)
Bubble Sort
void bubbleSortPhase(int a[], int last); void bubbleSort(int a[], int n) { // Precondition: a is an array indexed from a[0] to a[n-1] int i; for (i = n - 1; i > 0; i--) bubbleSortPhase(a, i); // Postcondition: a is sorted }
![Page 97: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/97.jpg)
Equation 5-7
![Page 98: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/98.jpg)
Version 1 of bubble sort
This version uses n-1 passes During each pass, n-1 pairs are compared Every time a pair needs to be swapped
this is done before the pass can continue
![Page 99: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/99.jpg)
Bubble Sort: difficult example
7
6
5
3
2
0 5Pos nA[0]A[1]A[2]A[3]A[4] last4
Pos+1
![Page 100: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/100.jpg)
Bubble Sort: difficult example
6
7
5
3
2
0 5Pos nA[0]A[1]A[2]A[3]A[4] last4
Pos+1
![Page 101: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/101.jpg)
Bubble Sort: difficult example
6
5
7
3
2
0 5Pos nA[0]A[1]A[2]A[3]A[4] last4
Pos+1
![Page 102: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/102.jpg)
Bubble Sort: difficult example
6
5
3
7
2
0 5Pos nA[0]A[1]A[2]A[3]A[4] last4
Pos+1
![Page 103: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/103.jpg)
Bubble Sort: difficult example
6
5
3
2
7
0 5Pos nA[0]A[1]A[2]A[3]A[4] last4
Pos+1
![Page 104: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/104.jpg)
Bubble Sort: difficult example
6
5
3
2
7
0 5Pos nA[0]A[1]A[2]A[3]A[4] last4
Pos+1
![Page 105: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/105.jpg)
Bubble Sort: difficult example
5
6
3
2
7
1 5Pos nA[0]A[1]A[2]A[3]A[4] last4
Pos+1
![Page 106: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/106.jpg)
Bubble Sort: difficult example
5
3
6
2
7
2 5Pos nA[0]A[1]A[2]A[3]A[4] last4
Pos+1
![Page 107: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/107.jpg)
Bubble Sort: difficult example
5
3
2
6
7
3 5Pos nA[0]A[1]A[2]A[3]A[4] last4
Pos+1
![Page 108: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/108.jpg)
Bubble Sort: difficult example
5
3
2
6
7
0 5Pos nA[0]A[1]A[2]A[3]A[4] last4
Pos+1
![Page 109: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/109.jpg)
Bubble Sort: difficult example
3
5
2
6
7
1 5Pos nA[0]A[1]A[2]A[3]A[4] last4
Pos+1
![Page 110: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/110.jpg)
Bubble Sort: difficult example
3
2
5
6
7
1 5Pos nA[0]A[1]A[2]A[3]A[4] last4
Pos+1
![Page 111: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/111.jpg)
Bubble Sort: difficult example
3
2
5
6
7
0 5Pos nA[0]A[1]A[2]A[3]A[4] last4
Pos+1
![Page 112: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/112.jpg)
Bubble Sort: difficult example
2
3
5
6
7
0 5Pos nA[0]A[1]A[2]A[3]A[4] last4
Pos+1
![Page 113: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/113.jpg)
Result after each pass
7
6
5
3
2
A[0]A[1]A[2]A[3]A[4]
6
5
3
2
7
5
3
2
6
7
3
2
5
6
7
2
3
5
6
7
2
3
5
6
7
![Page 114: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/114.jpg)
Analysis
For this example, we made n-1 passes through the array
Each time we looked at n-1 pairs– (we should have only looked at n-pass pairs.
Why?) Either way, the number of passes is O(n) The number of pairs processed in each
pass is O(n) So, overall we get O(n*n) = O(n2)
![Page 115: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/115.jpg)
Selection and Bubblesort comparison
Both are O(n2) sorts If we count up the number of critical
operations for both sorts, handling n-1, n-2, etc. pairs for each pass, using the data in the last example, we get
Selection sort: 10 comparisons Bubble sort: 10 comparisons
![Page 116: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/116.jpg)
What about swaps?
The same swap function can be used for both programs.
Let’s say it takes 3 operations, then the actual number of operations is
Selection sort: 10 + 3(n-1) = 22 Bubble sort: 10 + 3(10) = 40 The selection sort uses roughly half the
number of operations as the bubble sort
![Page 117: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/117.jpg)
Moral
Two sorts of the same order O(n2) may not be the same speed.
It depends on the data sets they are sorting.
It also depends on the way they are implemented.
![Page 118: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/118.jpg)
Data sets
The data set we chose for the bubble sort was the worst one possible.
What happens if we run it on the data set we originally used for the selection sort?
![Page 119: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/119.jpg)
Result after pass 1
2
7
3
5
6
A[0]A[1]A[2]A[3]A[4]
2
3
5
6
7
![Page 120: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/120.jpg)
Result after pass 2
2
7
3
5
6
A[0]A[1]A[2]A[3]A[4]
2
3
5
6
7
2
3
5
6
7
![Page 121: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/121.jpg)
Improvement needed
Neither the selection sort, nor the bubble sort know enough to stop if the list is sorted early!
We should be able to come up with a smart version of the bubble sort that can do this.
Instead of the outer for loop, let’s use a while loop that runs until the array is sorted.
![Page 122: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/122.jpg)
Further improvements
We will also make sure the inner loop runs the minimum number of times (n-pass)
and that we keep track of whether a swap was needed for the pass we are on.
If a swap was needed then we cannot assume the array is sorted.
If a swap was not needed, then the array is sorted and we should send that signal to the outer loop.
![Page 123: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/123.jpg)
Bubble sort improvements
What improvements can you suggest for the bubble sort that we have seen?
![Page 124: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/124.jpg)
Improvements
Do not look at portions of the array that are already in sorted order
Leave when the array is sorted– this is something the insertion and
selection sorts we have seen could not do.
![Page 125: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/125.jpg)
Improvements
How much of an operational difference does the smart bubble sort have over it’s dumb form?
How much of an operational difference does the smart bubble sort have over the selection sort?
Do these improvements make a difference in Big-O?
Which form of sort is best?
![Page 126: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/126.jpg)
Bubble Sort void bubbleSort(int a[], int n) { // Precondition: a is an array indexed from a[0] to a[n-1] bool sorted(false); int pass(0); while (!sorted) { sorted = true; for (pos = 0; pos < last - 1; pos++) if (a[pos] > a[pos+1]) { swapElements(pos, pos+1); sorted = false; } pos++; } // Postcondition: a is sorted }
![Page 127: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/127.jpg)
Result after pass 1
2
7
3
5
6
A[0]A[1]A[2]A[3]A[4]
2
3
5
6
7
![Page 128: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/128.jpg)
Result after pass 2
2
7
3
5
6
A[0]A[1]A[2]A[3]A[4]
2
3
5
6
7
2
3
5
6
7
![Page 129: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/129.jpg)
Analysis Only two passes are made 4 pairs are checked on pass 1 swap is called 3 times on pass 1 3 pairs are checked on pass 2, no swaps Total operations: 4*3+3 = 15 Better than the selection sort. The savings would be even bigger for larger
lists that were sorted early.
![Page 130: Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.](https://reader031.fdocuments.us/reader031/viewer/2022012913/56649d385503460f94a111fb/html5/thumbnails/130.jpg)
Bubble sort: final analysis
Not counting swapping, the best case is only n-1 operations!
Worst case is still n-squared Order of complexity same as selection
sort no matter what.