Chapter 9 Algorithm Efficiency Sorting Programming efficiently has

  • Slides: 68
Download presentation
Chapter 9 Algorithm Efficiency & Sorting Programming efficiently has been important up to this

Chapter 9 Algorithm Efficiency & Sorting Programming efficiently has been important up to this point, but we’re now going to examine the methods that have been developed to determine just how efficient a program is. ü Measuring Algorithm Efficiency ü Example: Searching Algorithms ü Example: Sorting Algorithms CS 240 Chapter 9 – Algorithm Efficiency & Sorting 109

Time Complexity Terminology: Big-O n Function T(n) is said to be O(f(n)) if there

Time Complexity Terminology: Big-O n Function T(n) is said to be O(f(n)) if there are positive constants c and n 0 such that T(n) c f(n) for every n n 0. – Example: n 3 + 3 n 2 + 6 n + 5 is O(n 3). (Use c = 15 and n 0 = 1. ) – Example: n 2 + n logn is O(n 2). (Use c = 2 and n 0 = 1. ) g(n) r(n) is O(g(n)) since (1)g(n) exceeds r(n) for all n-values past ng ng CS 240 g(n) is O(r(n)) since (3)r(n) exceeds g(n) for all n-values past nr nr Chapter 9 – Algorithm Efficiency & Sorting 110

Demonstrating The Big-O Concept Both algorithms below have O(n 3) time complexity. ALGORITHM Input

Demonstrating The Big-O Concept Both algorithms below have O(n 3) time complexity. ALGORITHM Input Size n A B 10 1, 110 100 1, 010, 100 2, 010, 100 1, 001, 000 1, 101, 000 10, 000 1, 000, 100, 010, 000 1, 010, 100, 010, 000 100, 000 1, 000, 010, 000, 100, 000 1, 001, 010, 000, 100, 000 1, 000, 001, 000, 000 1, 000, 101, 000, 000 (In fact, the execution time for Algorithm A is n 3 + n 2 + n, and the execution time for Algorithm B is n 3 + 101 n 2 + n. ) CS 240 Chapter 9 – Algorithm Efficiency & Sorting 111

A Second Big-O Demonstration Both algorithms below have O(n 2) time complexity. ALGORITHM Input

A Second Big-O Demonstration Both algorithms below have O(n 2) time complexity. ALGORITHM Input Size n C D 10 123 10, 123 100 10, 203 1, 000 1, 002, 003 10, 000 100, 020, 003 110, 020, 003 100, 000 10, 000, 200, 003 10, 100, 200, 003 1, 000, 002, 000, 003 1, 002, 000, 003 (In fact, the execution time for Algorithm C is n 2 + 2 n + 3, and the execution time for Algorithm D is n 2 + 1002 n + 3. ) CS 240 Chapter 9 – Algorithm Efficiency & Sorting 112

One More, Rather Complex Big-O Demonstration Both algorithms below have O(nlogn) time complexity. ALGORITHM

One More, Rather Complex Big-O Demonstration Both algorithms below have O(nlogn) time complexity. ALGORITHM Input Size n E F 10 90 1, 090 100 1, 200 1, 000 15, 000 10, 000 190, 000 1, 190, 000 100, 000 2, 200, 000 1, 000 25, 000 125, 000 (In fact, the execution time for Algorithm E is nlogn + 5 n, and the execution time for Algorithm F is nlogn + 105 n. Note that the linear term for Algorithm F will dominate until n = 2 105. ) CS 240 Chapter 9 – Algorithm Efficiency & Sorting 113

Big-O Represents An Upper Bound If T(n) is O(f(n)), then f(n) is basically a

Big-O Represents An Upper Bound If T(n) is O(f(n)), then f(n) is basically a cap on how bad T(n) will behave when n gets big. g(n) v(n) r(n) p(n) y(n) b(n) Is g(n) O(r(n))? Is v(n) O(y(n))? Is b(n) O(p(n))? Is r(n) O(g(n))? Is y(n) O(v(n))? Is p(n) O(b(n))? CS 240 Chapter 9 – Algorithm Efficiency & Sorting 114

Big-O Comparisons O(n) O(logn) O(2 n) O(nlogn) CS 240 O(n 2) Chapter 9 –

Big-O Comparisons O(n) O(logn) O(2 n) O(nlogn) CS 240 O(n 2) Chapter 9 – Algorithm Efficiency & Sorting 115

Computational Model For Algorithm Analysis To formally analyze the performance of algorithms, we will

Computational Model For Algorithm Analysis To formally analyze the performance of algorithms, we will use a computational model with a couple of simplifying assumptions: – Each simple instruction (assignment, I/O, comparison, addition, multiplication, etc. ) is assumed to execute in a single time unit. – Memory is assumed to be limitless, so there is always room to store whatever data is needed. The size of the input, n, will normally be used as our main variable, and we’ll primarily be interested in “worst case” scenarios. CS 240 Chapter 9 – Algorithm Efficiency & Sorting 116

General Rules For Running Time Calculation Rule One: Loops The running time of a

General Rules For Running Time Calculation Rule One: Loops The running time of a loop is at most the running time of the statements inside the loop, multiplied by the number of iterations. Example: for (i = 0; i < n; i++) A[i] = (1 -t)*X[i] + t*Y[i]; //n iterations //5 time units //per iteration Thus, the total running time is 5 n time units, i. e. , this part of the program is O(n). CS 240 Chapter 9 – Algorithm Efficiency & Sorting 117

Rule Two: Nested Loops The running time of a nested loop is at most

Rule Two: Nested Loops The running time of a nested loop is at most the running time of the statements inside the innermost loop, multiplied by the product of the number of iterations of all of the loops. Example: for (i = 0; i < n; i++) for (j = 0; j < n; j++) C[i, j] = j*A[i] + i*B[j]; // n iterations // 4 time units/iter. Total running time: 4 n 2 time units, i. e. , this code is O(n 2). More Complex Example: for (i = 0; i < n; i++) for (j = i; j < n; j++) C[j, i] = C[i, j] = j*A[i]+i*B[j]; // // // n iterations n-i iterations 5 t. u. /iter. Total running time: i=0, n-1( j=i, n-15) = i=0, n-1(5(n-i)) = 5( i=0, n-1 n - i=0, n-1 i) = 5(n 2 - ½n(n-1)) = ½(5 n 2 - n) time units, i. e. , this code is also O(n 2). CS 240 Chapter 9 – Algorithm Efficiency & Sorting 118

Rule Three: Consecutive Statements The running time of a sequence of statements is merely

Rule Three: Consecutive Statements The running time of a sequence of statements is merely the sum of the running times of the individual statements. Example: for (i = 0; i < n; i++) { A[i] = (1 -t)*X[i] + t*Y[i]; B[i] = (1 -s)*X[i] + s*Y[i]; } for (i = 0; i < n; i++) for (j = 0; j < n; j++) C[i, j] = j*A[i] + i*B[j]; //10 n time //units for //entire loop //4 n^2 time units //for entire //nested loop Total running time: 4 n 2 + 10 n time units, i. e. , this code is O(n 2). CS 240 Chapter 9 – Algorithm Efficiency & Sorting 119

Rule Four: Conditional Statements The running time of an if-else statement is at most

Rule Four: Conditional Statements The running time of an if-else statement is at most the running time of the conditional test, added to the maximum of the running times of the if and else blocks of statements. Example: if (amt > cost + tax) { count = 0; while ((count < n) && (amt > cost+tax)) { amt -= (cost + tax); count++; } cout << “CAPACITY: ” << count; } else cout << “INSUFFICIENT FUNDS”; // 2 time units // // // 1 time unit 4 TUs per iter. At most n iter. 3 time units 2 time units // 1 time unit Total running time: 2+max(1 + (4+3+2)n + 2, 1) = 9 n+5 time units, i. e. , this code is O(n). CS 240 Chapter 9 – Algorithm Efficiency & Sorting 120

Analyzing A Whole Function: Binary Search template <class etype> int binsrch(const etype A[], const

Analyzing A Whole Function: Binary Search template <class etype> int binsrch(const etype A[], const etype x, const int n) { int low = 0, high = n-1; int middle; while (low <= high) { middle = (low + high)/2; // 3 time units if (A[middle] < x) // << low = middle + 1; // << else if (A[middle] > x) // << At most 4 high = middle - 1; // << time units else // << return middle; // << } return -1; // Return impossible index for unsuccessful search. } Note that the loop will, in the worst case, keep dividing the distance between low and high in half until low and high are equal, i. e. , it will iterate at most logn times. Thus, the total running time is: 8 logn + 4 time units, i. e. , this code is O(logn). CS 240 Chapter 9 – Algorithm Efficiency & Sorting 121

Analysis Of Another Function: Super. Freq template <class etype> etype Super. Freq(const etype A[],

Analysis Of Another Function: Super. Freq template <class etype> etype Super. Freq(const etype A[], int n) { etype best. Element = A[0]; // 1 time unit int best. Freq = 0; // 1 time unit int curr. Freq; for (i = 0; i < n; i++) // n iterations { curr. Freq = 0; // 1 time unit for (j = i; j < n; j++) // n-i iterations if (A[i] == A[j]) // 1 time unit curr. Freq++; // 2 time units if (curr. Freq > best. Freq) // 1 time unit { best. Element = A[i]; // 1 time unit best. Freq = curr. Freq; // 1 time unit } } return best. Element; // 1 time unit } Note that the function is obviously O(n 2) due to its familiar nested loop structure. Specifically, its worst-case running time is ½(3 n 2 + 9 n + 6). CS 240 Chapter 9 – Algorithm Efficiency & Sorting 122

What About Recursion? humong. Int pow(const humong. Int &val, const humong. Int &n) {

What About Recursion? humong. Int pow(const humong. Int &val, const humong. Int &n) { if (n == 0) return humong. Int(1); if (n == 1) return val; if (n % 2 == 0) return pow(val*val, n/2); return pow(val*val, n/2) * val; } The worst-case running time would require all 3 conditions to be checked, and to fail (taking 4 time units). The last return requires 3 time units each time it’s executed, and it’ll be executed logn times (since it halves n with each execution, until it reaches a value of 1). Thus, the worst-case running time is 7 logn. CS 240 Chapter 9 – Algorithm Efficiency & Sorting 123

Sorting Algorithms As our principal example for examining algorithm efficiency, let’s explore several of

Sorting Algorithms As our principal example for examining algorithm efficiency, let’s explore several of the algorithms that have been developed for sorting an array. Names. txt Moe Bob Quo Xon Sue Ren Hal CS 240 Edy Wes Kit Gus Cub Dan Tia Zeb Ann Fly Joe Ida Lex Ort Uma Vin Nan Yul Pez // Generic Sort Driver Program #include <fstream> #include <string> using namespace std; int const MAX_LIST_SIZE = 30; typedef string elt; typedef elt list[MAX_LIST_SIZE]; void sort(list L, int n); void main() { list L; int size = 0; ifstream file; file. open(“Names. txt”); file >> L[0]; while (!file. eof()) { size++; file >> L[size]; } sort(L, size); } Chapter 9 – Algorithm Efficiency & Sorting 124

Bubble Sort void sort(list L, int n) { int j, k = 0; bool

Bubble Sort void sort(list L, int n) { int j, k = 0; bool exchange_made = true; elt temp; // Make up to n-1 passes through the array; exit early // if no exchanges are made on the previous pass. while ((k < n-1) && exchange_made) { exchange_made = false; ++k; for (j = 0; j < n-k; ++j) // n-k comparisons { // on the kth pass if (L[j] > L[j+1]) { temp = L[j]; // Exchange must be made L[j] = L[j+1]; L[j+1] = temp; exchange_made = true; } } CS 240 Chapter 9 – Algorithm Efficiency & Sorting 125

Bubble Sort: Step-by-Step Example Moe Edy Edy Edy Edy Edy Edy Edy Edy Moe

Bubble Sort: Step-by-Step Example Moe Edy Edy Edy Edy Edy Edy Edy Edy Moe Moe Moe Moe Moe Moe Moe Moe Zeb Zeb Ort Ort Ort Ort Ort Ort Ort Bob Bob CS 240 Ort Ort Zeb Bob Bob Bob Bob Bob Bob Ort Ort Bob Bob Zeb Wes Wes Wes Wes Wes Wes Wes Ann Wes Wes Wes Zeb Ann Ann Ann Ann Ann Ann Wes Uma Ann Ann Ann Zeb Uma Uma Uma Uma Uma Uma Wes Uma Uma Zeb Quo Quo Quo Quo Quo Quo Quo Quo Zeb Kit Kit Kit Kit Kit Kit Kit Kit Zeb Fly Fly Fly Fly Fly Fly Fly Fly Zeb Vin Vin Vin Vin Vin Vin Vin Vin Zeb Xon Xon Xon Xon Xon Xon Xon Xon Zeb Gus Gus Gus Gus Gus Gus Gus Gus Zeb Joe Joe Joe Joe Joe Joe Joe Joe Zeb Nan Nan Nan Nan Nan Nan Nan Nan Zeb Sue Sue Sue Sue Sue Sue Sue Sue Zeb Cub Cub Cub Cub Cub Cub Cub Cub Zeb Ida Ida Ida Ida Ida Ida Ida Ida Zeb Yul Yul Yul Yul Yul Yul Yul Yul Zeb Ren Ren Ren Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Ren Zeb Dan Dan Dan Dan Dan Dan Dan Dan Zeb Lex Lex Lex Lex Lex Lex Lex Lex Zeb Pez Pez Pez Pez Pez Pez Pez Pez Zeb Hal Hal Hal Hal Hal Hal Hal Hal Zeb Tia Tia Tia Tia Tia Tia Tia Tia Zeb Zeb 126

Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy Moe Moe Moe Moe

Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy Moe Moe Moe Moe Moe Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Moe Moe Moe Moe Moe CS 240 Ort Ort Ort Ort Ort Ort Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Ort Ort Ort Ort Ort Uma Uma Uma Uma Uma Uma Quo Quo Quo Quo Quo Quo Quo Quo Quo Quo Uma Kit Kit Kit Kit Wes Kit Kit Kit Kit Kit Kit Uma Fly Fly Fly Fly Kit Wes Fly Fly Fly Fly Fly Fly Uma Uma Uma Uma Fly Wes Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Wes Wes Wes Wes Wes Wes Wes Gus Gus Gus Xon Xon Xon Gus Gus Gus Gus Gus Gus Wes Joe Joe Joe Gus Gus Gus Xon Joe Joe Joe Joe Joe Joe Wes Nan Nan Nan Joe Joe Joe Xon Nan Nan Nan Nan Nan Nan Wes Sue Sue Nan Nan Xon Sue Sue Sue Sue Sue Sue Wes Cub Cub Sue Sue Xon Cub Cub Cub Cub Cub Cub Wes Ida Ida Ida Cub Cub Cub Xon Ida Ida Ida Ida Ida Ida Wes Wes Wes Ida Ida Ida Xon Xon Xon Xon Xon Xon Xon Ren Ren Yul Yul Yul Ren Ren Ren Ren Ren Ren Xon Dan Dan Ren Ren Ren Yul Dan Dan Dan Dan Dan Dan Xon Lex Chapter 9 – Algorithm Efficiency & Sorting Dan Dan Dan Dan Yul Lex Lex Lex Lex Lex Lex Xon Pez Lex Lex Lex Lex Yul Pez Pez Pez Pez Pez Pez Xon Pez Pez Pez Pez Yul Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Yul Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia Yul Yul Yul Yul Yul Yul Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb 127

Edy Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Edy Edy Edy

Edy Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Edy Edy Edy Edy Edy Edy Ann Ann Ann Ann Moe Moe Ann Ann Ann Ann Ann Ann Edy Edy Edy Edy CS 240 Ann Ann Moe Moe Moe Moe Moe Moe Moe Moe Moe Ort Ort Ort Ort Ort Ort Ort Kit Kit Kit Quo Quo Kit Kit Kit Kit Kit Kit Ort Fly Fly Fly Kit Kit Quo Fly Fly Fly Fly Fly Fly Ort Ort Ort Fly Fly Quo Quo Quo Quo Quo Quo Quo Quo Uma Uma Uma Uma Uma Uma Uma Uma Gus Gus Vin Vin Vin Gus Gus Gus Gus Gus Gus Uma Joe Joe Joe Gus Gus Gus Vin Joe Joe Joe Joe Joe Joe Uma Nan Nan Nan Joe Joe Joe Vin Nan Nan Nan Nan Nan Nan Uma Sue Sue Nan Nan Nan Nan Vin Sue Sue Sue Sue Sue Sue Uma Cub Cub Sue Sue Sue Sue Vin Cub Cub Cub Cub Cub Cub Uma Ida Cub Cub Cub Cub Vin Ida Ida Ida Ida Ida Ida Uma Ida Ida Ida Ida Vin Vin Vin Vin Vin Vin Wes Wes Wes Wes Wes Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Wes Dan Dan Dan Dan Dan Dan Dan Dan Dan Dan Wes Lex Lex Lex Lex Lex Lex Lex Lex Lex Lex Wes Pez Pez Pez Pez Pez Chapter 9 – Algorithm Efficiency & Sorting Pez Pez Pez Pez Pez Pez Wes Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Wes Tia Tia Tia Tia Xon Tia Tia Tia Tia Tia Tia Wes Wes Wes Wes Tia Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb 128

Bob Bob Bob Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob

Bob Bob Bob Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy CS 240 Moe Moe Moe Kit Kit Kit Kit Kit Fly Fly Fly Kit Kit Kit Moe Fly Fly Fly Fly Fly Kit Kit Kit Fly Fly Fly Moe Moe Moe Moe Moe Moe Moe Moe Ort Ort Ort Ort Ort Ort Ort Ort Gus Gus Quo Quo Quo Quo Gus Gus Gus Gus Gus Ort Joe Joe Joe Gus Gus Gus Gus Quo Joe Joe Joe Joe Joe Ort Nan Nan Nan Joe Joe Joe Joe Quo Nan Nan Nan Nan Nan Ort Ort Ort Nan Nan Nan Nan Quo Quo Quo Quo Quo Quo Cub Cub Sue Sue Sue Sue Sue Cub Cub Cub Cub Cub Quo Ida Cub Cub Cub Cub Cub Sue Ida Ida Ida Ida Ida Quo Ida Ida Ida Ida Ida Sue Sue Sue Sue Sue Sue Uma Uma Uma Uma Uma Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Uma Dan Dan Dan Dan Dan Vin Dan Dan Dan Dan Dan Uma Lex Lex Lex Lex Lex Dan Vin Lex Lex Lex Lex Lex Uma Pez Pez Pez Pez Lex Vin Pez Pez Pez Pez Pez Uma Hal Hal Hal Hal Pez Pez Vin Hal Hal Hal Hal Hal Uma Tia Tia Tia Tia Chapter 9 – Algorithm Efficiency & Sorting Hal Hal Vin Tia Tia Tia Tia Tia Uma Uma Uma Uma Tia Tia Tia Vin Vin Vin Vin Vin Vin Vin Vin Vin Wes Wes Wes Wes Wes Wes Wes Wes Wes Wes Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb 129

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy CS 240 Fly Fly Fly Fly Fly Fly Fly Fly Fly Fly Kit Kit Kit Kit Kit Kit Kit Gus Gus Gus Moe Moe Moe Gus Gus Gus Gus Gus Kit Joe Joe Joe Gus Gus Gus Moe Joe Joe Joe Joe Joe Kit Kit Kit Joe Joe Joe Moe Moe Moe Moe Moe Moe Moe Nan Nan Nan Nan Nan Nan Nan Nan Cub Cub Ort Ort Ort Ort Cub Cub Cub Cub Cub Nan Ida Ida Ida Cub Cub Cub Cub Ort Ida Ida Ida Ida Ida Nan Nan Nan Ida Ida Ida Ida Ort Ort Ort Ort Ort Ort Quo Quo Quo Quo Quo Quo Quo Quo Quo Dan Dan Ren Ren Ren Ren Ren Dan Dan Dan Dan Dan Quo Lex Sue Dan Dan Dan Dan Dan Ren Lex Lex Lex Lex Lex Quo Pez Dan Sue Lex Lex Lex Lex Lex Ren Pez Pez Pez Pez Pez Quo Lex Sue Pez Pez Pez Pez Pez Ren Hal Hal Hal Hal Hal Pez Pez Sue Hal Hal Hal Hal Hal Ren Ren Ren Ren Ren Hal Hal Sue Sue Sue Sue Sue Sue Sue Sue Sue Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia Chapter 9 – Algorithm Efficiency & Sorting Uma Uma Uma Uma Uma Uma Uma Uma Uma Uma Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Wes Wes Wes Wes Wes Wes Wes Wes Wes Wes Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb 130

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy CS 240 Fly Fly Fly Fly Fly Fly Fly Fly Fly Fly Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Joe Joe Joe Joe Joe Joe Joe Joe Joe Joe Cub Kit Kit Kit Kit Kit Kit Cub Cub Cub Cub Joe Moe Moe Moe Cub Cub Cub Cub Kit Ida Ida Ida Ida Cub Cub Cub Moe Ida Ida Ida Ida Kit Kit Kit Kit Ida Ida Ida Moe Moe Moe Moe Moe Moe Moe Moe Nan Nan Nan Nan Nan Nan Nan Dan Dan Dan Ort Ort Ort Ort Dan Dan Dan Dan Nan Lex Lex Lex Dan Dan Dan Dan Ort Lex Lex Lex Lex Nan Nan Nan Lex Lex Lex Lex Ort Ort Ort Ort Ort Hal Hal Pez Pez Pez Pez Hal Hal Hal Hal Ort Ort Hal Hal Hal Hal Pez Pez Pez Pez Pez Pez Quo Quo Quo Quo Quo Quo Quo Quo Quo Quo Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia Chapter 9 – Algorithm Efficiency & Sorting Uma Uma Uma Uma Uma Uma Uma Uma Uma Uma Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Wes Wes Wes Wes Wes Wes Wes Wes Wes Wes Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb 131

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Edy Edy Edy Edy Edy Edy Edy Edy Edy Cub Cub CS 240 Fly Fly Fly Fly Fly Fly Cub Cub Cub Edy Edy Gus Gus Gus Cub Cub Cub Fly Fly Fly Fly Cub Cub Cub Gus Gus Gus Gus Gus Gus Gus Ida Ida Ida Ida Ida Ida Ida Ida Ida Ida Joe Joe Joe Joe Joe Joe Joe Dan Dan Dan Kit Kit Kit Kit Dan Dan Dan Joe Joe Joe Moe Moe Dan Dan Dan Dan Kit Kit Kit Kit Kit Kit Dan Dan Moe Lex Lex Lex Lex Lex Lex Lex Hal Hal Lex Lex Moe Moe Moe Moe Hal Hal Hal Lex Lex Nan Nan Nan Hal Hal Hal Hal Moe Moe Moe Moe Moe Hal Hal Hal Nan Nan Nan Nan Nan Nan Nan Nan Nan Ort Ort Ort Ort Ort Ort Ort Ort Ort Ort Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Quo Quo Quo Quo Quo Quo Quo Quo Quo Quo Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia Chapter 9 – Algorithm Efficiency & Sorting Uma Uma Uma Uma Uma Uma Uma Uma Uma Uma Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Wes Wes Wes Wes Wes Wes Wes Wes Wes Wes Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb 132

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Cub Cub Cub Cub Cub Cub Cub Cub Cub Cub CS 240 Edy Edy Edy Edy Edy Edy Edy Dan Dan Dan Fly Fly Fly Fly Fly Dan Dan Edy Edy Edy Gus Gus Gus Dan Dan Dan Fly Fly Fly Fly Fly Dan Dan Dan Gus Gus Gus Gus Gus Gus Gus Gus Ida Ida Ida Ida Ida Ida Hal Hal Hal Hal Hal Joe Joe Joe Joe Hal Hal Hal Ida Ida Ida Ida Ida Kit Kit Hal Hal Hal Joe Joe Joe Joe Joe Joe Joe Hal Hal Kit Kit Kit Kit Kit Kit Kit Kit Kit Lex Lex Lex Lex Lex Lex Lex Lex Lex Lex Moe Moe Moe Moe Moe Moe Moe Moe Moe Moe Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Ort Ort Ort Ort Ort Ort Ort Ort Ort Ort Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Quo Quo Quo Quo Quo Quo Quo Quo Quo Quo Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia Chapter 9 – Algorithm Efficiency & Sorting Uma Uma Uma Uma Uma Uma Uma Uma Uma Uma Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Wes Wes Wes Wes Wes Wes Wes Wes Wes Wes Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb 133

Bubble Sort Analysis How long does it take the bubble sort to execute? Let’s

Bubble Sort Analysis How long does it take the bubble sort to execute? Let’s examine the worst case (i. e. , for every iteration of the while loop, every iteration of the for loop results in a swap): void sort(list L, int n) { int j, k = 0; bool exchange_made = true; elt temp; } These two steps (both assignments) would be executed exactly once. There would be at most n-1 iterations of the while loop, and calculating the boolean condition takes three steps. while ((k < n-1) && exchange_made) { exchange_made = false; These three steps (two assignments, one addition) would ++k; be executed once for each iteration of the while loop. for (j = 0; j < n-k; ++j) There would be n-k iterations of the for loop for { the kth iteration of the while loop, and if (L[j] > L[j+1]) processing each for-loop iteration takes four steps (except the first three-step iteration). { These eight steps (one comparison, temp = L[j]; three additions, four assignments) L[j] = L[j+1]; would be executed once for each L[j+1] = temp; iteration of the for loop. exchange_made = true; } } } CS 240 Chapter 9 – Algorithm Efficiency & Sorting 134

Bubble Sort Analysis (Continued) Adding all of this up yields the total number of

Bubble Sort Analysis (Continued) Adding all of this up yields the total number of executed steps as: which, after a little bit of math, simplifies to: As n, the size of the array, gets larger, the quadratic term tends to dominate this formula, so bubble sort is said to be O(n 2). CS 240 Chapter 9 – Algorithm Efficiency & Sorting 135

Selection Sort void sort(list L, int n) { int j, k; int index; elt

Selection Sort void sort(list L, int n) { int j, k; int index; elt temp; // Make n - 1 passes through successively smaller segments for (j = 0; j < n - 1; ++j) { index = j; for (k = j+1; k < n; ++k) { if (L[k] < L[index]) index = k; } if (index != j) { temp = L[index]; L[index] = L[j]; L[j] = temp; } } // Find index of smallest element // Exchange must be made } CS 240 Chapter 9 – Algorithm Efficiency & Sorting 136

Selection Sort: Step-by-Step Example Moe Moe Moe Moe Moe Moe Moe Ann Ann Edy

Selection Sort: Step-by-Step Example Moe Moe Moe Moe Moe Moe Moe Ann Ann Edy Edy Edy Edy Edy Edy Edy Edy Edy Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb CS 240 Ort Ort Ort Ort Ort Ort Ort Ort Ort Bob Bob Bob Bob Bob Bob Bob Bob Bob Wes Wes Wes Wes Wes Wes Wes Wes Wes Ann Ann Ann Ann Ann Ann Ann Moe Moe Uma Uma Uma Uma Uma Uma Uma Uma Uma Quo Quo Quo Quo Quo Quo Quo Quo Quo Kit Kit Kit Kit Kit Kit Kit Kit Kit Fly Fly Fly Fly Fly Fly Fly Fly Fly Vin Vin Vin Vin Vin Vin Vin Vin Vin Xon Xon Xon Xon Xon Xon Xon Xon Xon Gus Gus Gus Gus Gus Gus Gus Gus Gus Joe Joe Joe Joe Joe Joe Joe Joe Joe Nan Nan Nan Nan Nan Nan Nan Nan Nan Sue Sue Sue Sue Sue Sue Sue Sue Sue Cub Cub Cub Cub Cub Cub Cub Cub Cub Ida Ida Ida Ida Ida Ida Ida Ida Ida Yul Yul Yul Yul Yul Yul Yul Yul Yul Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Ren Ren Ren Ren Ren Dan Dan Dan Dan Dan Dan Dan Dan Dan Lex Lex Lex Lex Lex Lex Lex Lex Lex Pez Pez Pez Pez Pez Pez Pez Pez Pez Hal Hal Hal Hal Hal Hal Hal Hal Hal Tia Tia Tia Tia Tia Tia Tia Tia Tia 137

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Edy Edy Edy Edy

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Edy Edy Edy Edy Edy Bob Bob Bob Bob Bob Bob Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb CS 240 Ort Ort Ort Ort Ort Ort Ort Ort Ort Ort Bob Bob Bob Bob Bob Edy Edy Edy Edy Edy Edy Wes Wes Wes Wes Wes Wes Wes Wes Wes Wes Moe Moe Moe Moe Moe Moe Moe Moe Moe Moe Uma Uma Uma Uma Uma Uma Uma Uma Uma Uma Quo Quo Quo Quo Quo Quo Quo Quo Quo Quo Kit Kit Kit Kit Kit Kit Kit Kit Kit Kit Fly Fly Fly Fly Fly Fly Fly Fly Fly Fly Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Joe Joe Joe Joe Joe Joe Joe Joe Joe Joe Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Cub Cub Cub Cub Cub Cub Cub Cub Cub Cub Ida Ida Ida Ida Ida Ida Ida Ida Ida Ida Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Dan Dan Dan Dan Dan Dan Dan Dan Dan Dan Lex Lex Lex Lex Lex Lex Lex Lex Lex Lex Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia 138

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Zeb Zeb Cub Cub Cub Cub Cub Cub Cub Cub Cub CS 240 Ort Ort Ort Ort Ort Ort Ort Dan Dan Dan Dan Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy Wes Wes Wes Wes Wes Wes Wes Wes Wes Wes Moe Moe Moe Moe Moe Moe Moe Moe Moe Moe Uma Uma Uma Uma Uma Uma Uma Uma Uma Uma Quo Quo Quo Quo Quo Quo Quo Quo Quo Quo Kit Kit Kit Kit Kit Kit Kit Kit Kit Kit Fly Fly Fly Fly Fly Fly Fly Fly Fly Fly Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Joe Joe Joe Joe Joe Joe Joe Joe Joe Joe Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Cub Cub Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Ida Ida Ida Ida Ida Ida Ida Ida Ida Ida Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Dan Dan Dan Dan Dan Dan Dan Ort Ort Ort Ort Lex Lex Lex Lex Lex Lex Lex Lex Lex Lex Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia 139

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Cub Cub Cub Cub Cub Cub Cub Cub Cub Cub CS 240 Dan Dan Dan Dan Dan Dan Dan Dan Dan Dan Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy Wes Wes Wes Wes Wes Wes Wes Wes Fly Fly Fly Moe Moe Moe Moe Moe Moe Moe Moe Moe Moe Uma Uma Uma Uma Uma Uma Uma Uma Uma Uma Quo Quo Quo Quo Quo Quo Quo Quo Quo Quo Kit Kit Kit Kit Kit Kit Kit Kit Kit Kit Fly Fly Fly Fly Fly Fly Fly Fly Wes Wes Wes Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Joe Joe Joe Joe Joe Joe Joe Joe Joe Joe Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Ida Ida Ida Ida Ida Ida Ida Ida Ida Ida Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Ort Ort Ort Ort Ort Ort Ort Ort Ort Ort Lex Lex Lex Lex Lex Lex Lex Lex Lex Lex Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia 140

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Cub Cub Cub Cub Cub Cub Cub Cub Cub Cub CS 240 Dan Dan Dan Dan Dan Dan Dan Dan Dan Dan Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy Fly Fly Fly Fly Fly Fly Fly Fly Fly Fly Moe Moe Moe Gus Gus Gus Gus Gus Gus Gus Uma Uma Uma Uma Uma Uma Uma Uma Hal Hal Hal Quo Quo Quo Quo Quo Quo Quo Quo Quo Quo Kit Kit Kit Kit Kit Kit Kit Kit Kit Kit Wes Wes Wes Wes Wes Wes Wes Wes Wes Wes Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Gus Gus Gus Moe Moe Moe Moe Moe Moe Moe Joe Joe Joe Joe Joe Joe Joe Joe Joe Joe Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Ida Ida Ida Ida Ida Ida Ida Ida Ida Ida Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Ort Ort Ort Ort Ort Ort Ort Ort Ort Ort Lex Lex Lex Lex Lex Lex Lex Lex Lex Lex Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Hal Hal Hal Hal Hal Hal Hal Hal Uma Uma Uma Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia 141

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Cub Cub Cub Cub Cub Cub Cub Cub Cub Cub CS 240 Dan Dan Dan Dan Dan Dan Dan Dan Dan Dan Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy Fly Fly Fly Fly Fly Fly Fly Fly Fly Fly Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Quo Quo Quo Ida Ida Ida Ida Ida Ida Ida Ida Kit Kit Kit Kit Kit Kit Kit Joe Joe Joe Joe Wes Wes Wes Wes Wes Wes Wes Wes Wes Wes Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Moe Moe Moe Moe Moe Moe Moe Moe Moe Moe Joe Joe Joe Joe Joe Joe Joe Kit Kit Kit Kit Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Ida Ida Ida Quo Quo Quo Quo Quo Quo Quo Quo Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Ort Ort Ort Ort Ort Ort Ort Ort Ort Ort Lex Lex Lex Lex Lex Lex Lex Lex Lex Lex Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Uma Uma Uma Uma Uma Uma Uma Uma Uma Uma Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia 142

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Cub Cub Cub Cub Cub Cub Cub Cub Cub Cub CS 240 Dan Dan Dan Dan Dan Dan Dan Dan Dan Dan Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy Fly Fly Fly Fly Fly Fly Fly Fly Fly Fly Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Ida Ida Ida Ida Ida Ida Ida Ida Ida Ida Joe Joe Joe Joe Joe Joe Joe Joe Joe Joe Wes Wes Kit Kit Kit Kit Kit Kit Kit Kit Kit Vin Vin Vin Vin Vin Lex Lex Lex Lex Lex Lex Xon Xon Xon Xon Xon Xon Xon Xon Moe Moe Moe Moe Moe Moe Moe Moe Moe Moe Xon Xon Kit Kit Wes Wes Wes Wes Wes Wes Wes Wes Wes Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Quo Quo Quo Quo Quo Quo Quo Quo Quo Quo Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Ort Ort Ort Ort Ort Ort Ort Ort Ort Ort Lex Lex Lex Lex Lex Vin Vin Vin Vin Vin Vin Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Uma Uma Uma Uma Uma Uma Uma Uma Uma Uma Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia 143

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Cub Cub Cub Cub Cub Cub Cub Cub Cub Cub CS 240 Dan Dan Dan Dan Dan Dan Dan Dan Dan Dan Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy Fly Fly Fly Fly Fly Fly Fly Fly Fly Fly Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Ida Ida Ida Ida Ida Ida Ida Ida Ida Ida Joe Joe Joe Joe Joe Joe Joe Joe Joe Joe Kit Kit Kit Kit Kit Kit Kit Kit Kit Kit Lex Lex Lex Lex Lex Lex Lex Lex Lex Lex Moe Moe Moe Moe Moe Moe Moe Moe Moe Moe Xon Xon Xon Nan Nan Nan Nan Nan Nan Nan Nan Nan Wes Wes Wes Wes Wes Ort Ort Ort Ort Ort Ort Nan Nan Nan Xon Xon Xon Xon Xon Xon Pez Pez Pez Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Quo Quo Quo Quo Quo Quo Quo Quo Quo Quo Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Ort Ort Ort Ort Ort Wes Wes Wes Wes Wes Wes Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Pez Pez Pez Pez Pez Pez Pez Pez Xon Xon Xon Uma Uma Uma Uma Uma Uma Uma Uma Uma Uma Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia 144

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Cub Cub Cub Cub Cub Cub Cub Cub Cub Cub CS 240 Dan Dan Dan Dan Dan Dan Dan Dan Dan Dan Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy Fly Fly Fly Fly Fly Fly Fly Fly Fly Fly Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Ida Ida Ida Ida Ida Ida Ida Ida Ida Ida Joe Joe Joe Joe Joe Joe Joe Joe Joe Joe Kit Kit Kit Kit Kit Kit Kit Kit Kit Kit Lex Lex Lex Lex Lex Lex Lex Lex Lex Lex Moe Moe Moe Moe Moe Moe Moe Moe Moe Moe Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Ort Ort Ort Ort Ort Ort Ort Ort Ort Ort Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Quo Quo Quo Quo Quo Quo Quo Quo Quo Quo Zeb Zeb Zeb Ren Ren Ren Ren Ren Ren Ren Ren Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Yul Yul Yul Yul Yul Yul Tia Tia Tia Tia Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Zeb Zeb Zeb Zeb Zeb Zeb Uma Uma Uma Wes Wes Wes Wes Wes Wes Wes Wes Wes Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Wes Wes Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Uma Uma Uma Uma Uma Uma Uma Uma Zeb Zeb Zeb Tia Tia Tia Tia Tia Tia Yul Yul Yul Yul 145

Ann Ann Bob Bob Cub Cub Dan Dan Edy Edy Fly Fly Gus Gus

Ann Ann Bob Bob Cub Cub Dan Dan Edy Edy Fly Fly Gus Gus Hal Hal Ida Ida Joe Joe Kit Kit Lex Lex Moe Moe Nan Nan Ort Ort Pez Pez Quo Quo Ren Ren Sue Sue Tia Tia Uma Uma Vin Vin Wes Wes Xon Xon Zeb Zeb Zeb Yul Yul Zeb Selection Sort Analysis For the worst case of selection sort (i. e. , every condition is true for every if statement in the function): void sort(list L, int n) { int j, k; int index; elt temp; There would be n-1 iterations of the outer for loop, and processing each for-loop iteration takes four steps (except the first three-step iteration). for (j = 0; j < n - 1; ++j) { index = j; for (k = j+1; k < n; ++k) { if (L[k] < L[index]) index = k; } if (index != j) { temp = L[index]; L[index] = L[j]; L[j] = temp; } } } CS 240 This step would be executed once for each iteration of the outer for loop. There would be n-j-1 iterations of the inner for loop for the jth iteration of the outer for loop, and processing each for-loop iteration takes three steps. These two steps would be executed once for each iteration of the inner for loop. These four steps would be executed once for each iteration of the outer for loop. This yields a total number of executed steps: which is also O(n 2). Chapter 9 – Algorithm Efficiency & Sorting 146

Insertion Sort void sort(list L, int n) { int j, k; elt item. To.

Insertion Sort void sort(list L, int n) { int j, k; elt item. To. Insert; bool still. Looking; // On the kth pass, insert item k into its correct // position among the first k entries in array. for (k = 1; k < n; k++) { // Walk backwards through the list, seeking // the slot to insert the kth element. item. To. Insert = L[k]; j = k-1; still. Looking = true; while ((j >= 0) && still. Looking ) if (item. To. Insert < L[j]) { L[j+1] = L[j]; j--; } else still. Looking = false; // Upon leaving loop, j+1 is the index // where item. To. Insert belongs L[j+1] = item. To. Insert; } } CS 240 Chapter 9 – Algorithm Efficiency & Sorting 147

Insertion Sort: Step-by-Step Example Moe Moe Edy Edy Edy Bob Bob Bob Ann Ann

Insertion Sort: Step-by-Step Example Moe Moe Edy Edy Edy Bob Bob Bob Ann Ann Edy Moe Moe Moe Edy Edy Edy Bob Bob Zeb Zeb Ort Ort Moe Moe Moe Edy Edy Edy CS 240 Ort Ort Zeb Zeb Ort Ort Ort Moe Moe Moe Bob Bob Bob Zeb Zeb Wes Wes Ort Ort Ort Wes Wes Wes Wes Zeb Zeb Wes Wes Wes Uma Uma Ann Ann Ann Ann Ann Zeb Zeb Zeb Wes Wes Uma Uma Uma Uma Uma Uma Uma Zeb Zeb Zeb Quo Quo Quo Quo Quo Quo Quo Quo Zeb Kit Kit Kit Kit Kit Kit Kit Kit Fly Fly Fly Fly Fly Fly Fly Fly Vin Vin Vin Vin Vin Vin Vin Vin Xon Xon Xon Xon Xon Xon Xon Xon Gus Gus Gus Gus Gus Gus Gus Gus Joe Joe Joe Joe Joe Joe Joe Joe Nan Nan Nan Nan Nan Nan Nan Nan Sue Sue Sue Sue Sue Sue Sue Sue Cub Cub Cub Cub Cub Cub Cub Cub Ida Ida Ida Ida Ida Ida Ida Ida Yul Yul Yul Yul Yul Yul Yul Yul Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Ren Ren Ren Ren Dan Dan Dan Dan Dan Dan Dan Dan Lex Lex Lex Lex Lex Lex Lex Lex Pez Pez Pez Pez Pez Pez Pez Pez Hal Hal Hal Hal Hal Hal Hal Hal Tia Tia Tia Tia Tia Tia Tia Tia 148

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy CS 240 Moe Moe Moe Kit Kit Kit Fly Fly Fly Fly Fly Ort Ort Ort Moe Moe Moe Kit Kit Kit Kit Kit Gus Uma Quo Quo Quo Ort Ort Ort Moe Moe Moe Moe Moe Kit Kit Wes Uma Uma Uma Quo Quo Quo Ort Ort Ort Ort Ort Moe Moe Wes Wes Wes Uma Uma Uma Quo Quo Quo Quo Quo Ort Ort Ort Zeb Zeb Zeb Wes Wes Wes Uma Uma Uma Uma Uma Quo Quo Quo Kit Kit Zeb Zeb Zeb Wes Wes Wes Vin Vin Vin Uma Uma Fly Fly Fly Zeb Zeb Zeb Wes Wes Wes Vin Vin Vin Vin Vin Vin Vin Vin Zeb Zeb Zeb Xon Xon Wes Wes Wes Xon Xon Xon Xon Xon Xon Xon Zeb Zeb Xon Xon Xon Gus Gus Gus Gus Gus Gus Gus Zeb Zeb Zeb Joe Joe Joe Joe Joe Joe Joe Joe Joe Joe Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Cub Cub Cub Cub Cub Cub Cub Cub Cub Cub Ida Ida Ida Ida Ida Ida Ida Ida Ida Ida Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Dan Dan Dan Dan Dan Dan Dan Dan Dan Dan Lex Lex Lex Lex Lex Lex Lex Lex Lex Lex Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia 149

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy CS 240 Fly Fly Fly Fly Fly Fly Fly Fly Fly Fly Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Kit Kit Kit Joe Joe Joe Joe Joe Joe Joe Joe Moe Moe Kit Kit Kit Kit Kit Kit Kit Kit Joe Ort Ort Moe Moe Moe Moe Moe Moe Moe Moe Kit Quo Quo Quo Ort Ort Ort Nan Nan Nan Nan Nan Moe Moe Uma Uma Uma Quo Quo Quo Ort Ort Ort Ort Ort Nan Nan Vin Vin Uma Uma Uma Quo Quo Quo Quo Quo Ort Ort Ort Wes Wes Vin Vin Vin Uma Uma Uma Sue Sue Quo Quo Quo Xon Wes Wes Wes Vin Vin Vin Uma Uma Sue Sue Zeb Xon Xon Xon Wes Wes Wes Vin Vin Uma Uma Zeb Zeb Zeb Xon Xon Xon Wes Wes Vin Vin Vin Nan Nan Nan Zeb Zeb Zeb Xon Xon Wes Wes Wes Sue Sue Sue Sue Sue Zeb Zeb Xon Xon Xon Cub Cub Cub Cub Cub Cub Cub Zeb Zeb Zeb Ida Ida Ida Ida Ida Ida Ida Ida Ida Ida Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Dan Dan Dan Dan Dan Dan Dan Dan Dan Dan Lex Lex Lex Lex Lex Lex Lex Lex Lex Lex Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia 150

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Edy Edy Cub Cub Cub Cub Cub Cub Cub Cub Cub CS 240 Fly Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy Gus Fly Fly Fly Fly Fly Fly Fly Fly Fly Fly Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Joe Joe Joe Joe Joe Ida Ida Ida Ida Ida Ida Kit Kit Kit Kit Joe Joe Joe Joe Joe Joe Moe Moe Moe Moe Kit Kit Kit Kit Kit Kit Nan Nan Nan Nan Moe Moe Moe Moe Moe Moe Moe Ort Ort Ort Ort Nan Nan Nan Nan Nan Nan Nan Quo Quo Quo Ort Ort Ort Ort Ort Ort Ort Sue Sue Sue Quo Quo Quo Quo Quo Quo Quo Uma Uma Uma Sue Sue Sue Sue Sue Ren Ren Ren Vin Vin Vin Uma Uma Uma Uma Uma Sue Sue Sue Ren Wes Wes Vin Vin Vin Vin Vin Uma Uma Uma Sue Xon Xon Wes Wes Wes Wes Wes Vin Vin Vin Uma Uma Zeb Zeb Zeb Xon Xon Xon Xon Xon Wes Wes Wes Vin Vin Ida Ida Ida Zeb Zeb Zeb Zeb Yul Yul Xon Xon Xon Wes Wes Wes Yul Yul Yul Yul Yul Zeb Zeb Yul Yul Yul Xon Xon Xon Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Ren Ren Zeb Zeb Zeb Yul Yul Dan Dan Dan Dan Dan Dan Dan Dan Zeb Zeb Lex Lex Lex Lex Lex Lex Lex Lex Lex Lex Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia 151

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Cub Cub Cub Cub Cub Cub Cub Cub Cub Cub CS 240 Edy Edy Edy Dan Dan Dan Dan Dan Dan Dan Dan Fly Fly Fly Edy Edy Edy Edy Edy Edy Edy Edy Gus Gus Fly Fly Fly Fly Fly Fly Fly Fly Ida Ida Gus Gus Gus Gus Gus Gus Gus Gus Joe Joe Joe Ida Ida Ida Ida Ida Ida Ida Ida Ida Kit Kit Kit Joe Joe Joe Joe Joe Joe Joe Joe Joe Moe Moe Kit Kit Kit Kit Kit Kit Kit Kit Kit Nan Nan Moe Moe Moe Moe Moe Moe Lex Lex Lex Lex Ort Nan Nan Nan Nan Nan Nan Moe Moe Moe Moe Quo Ort Ort Ort Ort Ort Ort Nan Nan Nan Nan Nan Quo Quo Quo Quo Quo Quo Ort Ort Ort Ort Ort Ren Ren Ren Ren Ren Quo Quo Quo Quo Pez Pez Sue Sue Sue Sue Sue Ren Ren Ren Ren Quo Quo Quo Uma Uma Uma Uma Uma Sue Sue Sue Sue Ren Ren Ren Vin Vin Vin Vin Vin Uma Uma Uma Uma Sue Sue Wes Wes Wes Wes Vin Vin Vin Vin Uma Uma Xon Xon Xon Xon Wes Wes Wes Wes Vin Vin Vin Chapter 9 – Algorithm Efficiency & Sorting Yul Yul Yul Yul Xon Xon Xon Xon Wes Wes Wes Zeb Zeb Zeb Zeb Yul Yul Yul Yul Xon Xon Xon Lex Lex Lex Zeb Zeb Zeb Zeb Yul Yul Yul Pez Pez Pez Pez Pez Pez Pez Zeb Zeb Zeb Yul Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Zeb Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia 152

Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Cub Cub

Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Cub Cub Cub Cub Cub Cub CS 240 Dan Dan Dan Dan Dan Dan Edy Edy Edy Edy Edy Edy Fly Fly Fly Fly Fly Fly Gus Gus Gus Gus Gus Gus Ida Ida Ida Ida Hal Hal Hal Joe Joe Joe Joe Ida Ida Ida Kit Kit Kit Kit Joe Joe Joe Lex Lex Lex Kit Kit Kit Moe Moe Moe Lex Lex Lex Lex Nan Nan Nan Moe Moe Moe Moe Ort Ort Ort Nan Nan Nan Nan Pez Pez Ort Ort Ort Ort Quo Quo Pez Pez Pez Pez Pez Ren Ren Ren Quo Quo Quo Quo Quo Sue Sue Sue Ren Ren Ren Ren Ren Uma Uma Sue Sue Sue Sue Sue Vin Vin Uma Uma Uma Uma Uma Tia Chapter 9 – Algorithm Efficiency & Sorting Wes Vin Vin Vin Vin Vin Uma Xon Wes Wes Wes Wes Wes Vin Vin Xon Xon Xon Xon Xon Wes Wes Yul Yul Yul Yul Yul Xon Xon Xon Zeb Zeb Zeb Zeb Zeb Yul Yul Yul Tia Tia Tia Tia Tia Zeb Zeb 153

Insertion Sort Analysis For the worst case of insertion sort (i. e. , every

Insertion Sort Analysis For the worst case of insertion sort (i. e. , every time the while is entered, it must examine all of the array that’s been sorted thus far): void sort(list L, int n) { int j, k; elt item. To. Insert; bool still. Looking; for (k = 1; k < n; k++) { item. To. Insert = L[k]; j = k-1; still. Looking = true; while ((j >= 0) && still. Looking ) if (item. To. Insert < L[j]) { L[j+1] = L[j]; j--; } else still. Looking = false; There would be n-1 iterations of the for loop, and processing each for-loop iteration takes three steps (except the first two-step iteration). These four steps would be executed once for each iteration of the for loop. There would be k iterations of the while loop for the kth iteration of the for loop, and processing each while-loop iteration takes two steps. The five steps associated with the if would be executed once for each iteration of the while loop. These two steps would be executed once for each iteration of the for loop. This yields a total number of executed steps: L[j+1] = item. To. Insert; } } which is also O(n 2). CS 240 Chapter 9 – Algorithm Efficiency & Sorting 154

Shell Sort The worst case for insertion sort involves examining all of the array

Shell Sort The worst case for insertion sort involves examining all of the array that’s been sorted thus far. Shell sort alleviates this problem by sorting periodic subarrays, making it unlikely that complete linear traversals of the list will be needed. CS 240 void segmented. Insertion. Sort(list L, int n, int k); void sort(list L, int n) { int k = n/2; while (k > 0) { segmented. Insertion. Sort(L , n, k); k /= 2; } } void segmented. Insertion. Sort(list L, int n, int k) { int i, j, t; elt item. To. Insert; bool still. Looking; for (i = 0; i < k; i++) for (j = i + k; j < n; j += k) { item. To. Insert = L[j]; t = j - k; still. Looking = true; while ((t >= i) && still. Looking) if (item. To. Insert < L[t]) { L[t + k] = L[t]; t -= k; } else still. Looking = false; L[t + k] = item. To. Insert; } } Chapter 9 – Algorithm Efficiency & Sorting 155

Shell Sort: Step-by-Step Example Moe Gus Gus Gus Gus Gus Gus Gus Ann Ann

Shell Sort: Step-by-Step Example Moe Gus Gus Gus Gus Gus Gus Gus Ann Ann Ann Edy Edy Edy Edy Edy Edy Edy Edy Edy Zeb Zeb Zeb Nan Nan Nan Nan Nan Nan Nan Nan Ort Ort Ort Ort Ort Ort Ort Ort Ort CS 240 Bob Bob Bob Bob Bob Bob Bob Bob Bob Wes Wes Wes Ida Ida Ida Ida Ida Ida Ann Ann Ann Ann Ann Ann Ann Gus Gus Uma Uma Uma Uma Ren Ren Ren Ren Ren Quo Quo Quo Quo Quo Dan Dan Dan Dan Dan Kit Kit Kit Kit Kit Kit Kit Kit Kit Fly Fly Fly Fly Fly Fly Fly Fly Fly Vin Vin Vin Vin Vin Vin Hal Hal Hal Xon Xon Xon Xon Xon Xon Xon Tia Tia Tia Gus Moe Moe Moe Moe Moe Moe Moe Moe Moe Joe Joe Joe Joe Joe Joe Joe Joe Joe Nan Nan Nan Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Sue Sue Sue Sue Sue Sue Sue Sue Sue Cub Cub Cub Cub Cub Cub Cub Cub Cub Ida Ida Ida Wes Wes Wes Wes Wes Wes Vin Yul Yul Yul Yul Yul Yul Yul Yul Yul Ren Ren Ren Ren Uma Uma Uma Uma Uma Uma Chapter 9 – Algorithm Efficiency & Sorting Dan Dan Dan Dan Dan Quo Quo Quo Quo Quo Lex Lex Lex Lex Lex Lex Lex Lex Lex Pez Pez Pez Pez Pez Pez Pez Pez Pez Hal Hal Hal Hal Hal Hal Vin Vin Vin Wes Tia Tia Tia Tia Tia Tia Tia Xon Xon Xon 156

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Edy Edy Edy Edy

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy Nan Nan Nan Dan Dan Dan Dan Dan Dan Dan Dan Ort Ort Ort Ort Ort Kit Kit Kit Kit Kit Kit Gus CS 240 Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Ida Ida Ida Ida Ida Ida Ida Ida Hal Hal Cub Cub Cub Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Kit Ren Ren Moe Moe Moe Moe Moe Moe Moe Moe Moe Dan Dan Dan Nan Nan Joe Joe Joe Joe Joe Joe Joe Kit Kit Kit Kit Ort Ort Ort Ort Ort Ort Fly Fly Fly Fly Fly Fly Fly Fly Fly Fly Hal Hal Hal Hal Hal Hal Hal Ida Ida Hal Hal Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia Moe Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Joe Joe Joe Nan Nan Nan Nan Nan Nan Nan Zeb Zeb Zeb Zeb Zeb Zeb Quo Quo Quo Quo Quo Sue Sue Sue Sue Sue Sue Sue Lex Lex Lex Lex Cub Cub Cub Cub Cub Cub Cub Cub Ida Ida Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Yul Yul Xon Xon Xon Xon Xon Xon Xon Xon Uma Uma Uma Uma Uma Uma Uma Uma Uma Uma Chapter 9 – Algorithm Efficiency & Sorting Quo Quo Quo Quo Quo Zeb Zeb Zeb Zeb Zeb Lex Lex Lex Lex Lex Lex Lex Sue Sue Sue Sue Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Wes Wes Wes Wes Wes Wes Wes Wes Wes Wes Xon Xon Xon Yul Yul Yul Yul Yul Yul Yul Yul Yul 157

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Edy Edy Edy Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Edy Edy Edy Bob Bob Bob Bob Bob Bob Bob Dan Dan Dan Dan Dan Dan Dan Dan Cub Cub Cub CS 240 Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Bob Bob Bob Edy Edy Edy Edy Edy Edy Edy Cub Cub Cub Cub Cub Cub Cub Dan Dan Dan Kit Kit Kit Kit Kit Kit Kit Kit Kit Kit Moe Moe Moe Moe Fly Fly Fly Fly Fly Fly Joe Joe Joe Joe Joe Joe Joe Joe Joe Hal Hal Hal Ort Ort Ort Ort Ort Ort Ort Ort Ort Ort Fly Fly Fly Fly Moe Moe Moe Lex Lex Lex Lex Lex Hal Hal Hal Hal Hal Hal Hal Hal Joe Joe Joe Ida Tia Tia Quo Quo Quo Quo Quo Quo Quo Quo Quo Ren Ren Ren Ren Ren Moe Moe Moe Moe Moe Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Joe Quo Quo Tia Tia Tia Tia Tia Tia Tia Tia Tia Lex Lex Lex Lex Lex Ren Ren Ren Ren Ren Ida Ida Ida Ida Ida Ida Ida Ida Ida Nan Nan Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Xon Xon Xon Xon Xon Xon Xon Sue Sue Sue Sue Chapter 9 – Algorithm Efficiency & Sorting Uma Uma Uma Uma Uma Uma Uma Uma Uma Uma Zeb Zeb Zeb Wes Wes Wes Wes Wes Wes Wes Wes Sue Sue Sue Sue Sue Sue Xon Xon Xon Xon Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Wes Wes Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul 158

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Cub Cub Cub Cub Cub Cub Cub Cub Cub Cub CS 240 Gus Gus Gus Edy Edy Dan Dan Dan Dan Dan Dan Dan Edy Edy Gus Gus Edy Edy Edy Edy Edy Edy Edy Dan Dan Dan Gus Gus Fly Fly Fly Fly Fly Fly Kit Kit Kit Kit Kit Gus Gus Gus Gus Gus Gus Fly Fly Fly Fly Kit Kit Kit Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Kit Kit Kit Ida Ida Ida Ort Ort Ort Ort Ort Ort Ort Lex Lex Kit Kit Kit Lex Lex Lex Lex Lex Lex Ort Ort Lex Lex Lex Ida Ida Ida Ida Ida Ida Ida Ort Ort Moe Moe Quo Quo Quo Quo Quo Quo Quo Quo Quo Ort Ort Ort Moe Moe Moe Moe Moe Moe Moe Moe Moe Quo Quo Quo Ort Joe Joe Joe Joe Joe Joe Joe Joe Joe Joe Quo Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Chapter 9 – Algorithm Efficiency & Sorting Uma Uma Pez Pez Pez Pez Pez Pez Pez Pez Pez Wes Wes Wes Wes Wes Wes Wes Wes Wes Wes Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Pez Uma Uma Uma Uma Uma Uma Uma Uma Uma Uma Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul 159

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Cub Cub Cub Cub Cub Cub Cub Cub Cub Cub CS 240 Dan Dan Dan Dan Dan Dan Dan Dan Dan Dan Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy Fly Fly Fly Fly Fly Fly Fly Fly Fly Fly Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Ida Ida Ida Ida Ida Ida Ida Ida Ida Ida Kit Kit Joe Joe Joe Joe Joe Joe Joe Joe Joe Lex Kit Kit Kit Kit Kit Kit Kit Kit Kit Moe Lex Lex Lex Lex Lex Lex Lex Lex Lex Moe Moe Moe Moe Moe Moe Moe Moe Moe Moe Ort Ort Ort Ort Nan Nan Nan Nan Nan Nan Quo Quo Quo Ort Ort Ort Ort Ort Ort Ort Tia Tia Ren Ren Quo Quo Quo Quo Pez Pez Pez Ren Ren Ren Tia Tia Ren Ren Ren Ren Quo Quo Quo Quo Nan Nan Nan Tia Tia Tia Sue Sue Sue Ren Ren Ren Ren Vin Vin Vin Vin Vin Tia Tia Tia Sue Sue Sue Sue Sue Sue Sue Sue Vin Vin Vin Tia Tia Tia Tia Chapter 9 – Algorithm Efficiency & Sorting Pez Pez Pez Pez Pez Vin Vin Vin Uma Uma Uma Wes Wes Wes Wes Wes Wes Wes Wes Vin Vin Vin Xon Xon Xon Xon Xon Xon Xon Xon Wes Wes Uma Uma Uma Uma Uma Uma Uma Uma Xon Xon Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Yul Yul Yul Yul Yul Yul Yul Yul Yul Zeb 160

Shell Sort Analysis For the worst case of insertion sort (i. e. , for

Shell Sort Analysis For the worst case of insertion sort (i. e. , for every iteration of the while loop, every iteration of the for loop results in a swap): void sort(list L, int n) { int k = n/2; while (k > 0) { segmented. Insertion. Sort(L, n, k); k /= 2; } } These three steps would be executed log(n) times. There would be k iterations of the outer for loop, and processing each for-loop iteration would take three steps (except the first 2 -step iteration). void segmented. Insertion. Sort(list L, int n, int k) { int i, j, t; There would be n/k-1 iterations of the inner for loop, and elt item. To. Insert; processing each for-loop iteration would take three steps. bool still. Looking; for (i = 0; i < k; i++) These four steps would be executed once for (j = i + k; j < n; j += k) for each iteration of the inner for loop. { item. To. Insert = L[j]; There would be (j-i)/k iterations of the while loop for t = j - k; still. Looking = true; each iteration of the inner for loop, and processing while ((t >= i) && still. Looking) each while-loop iteration would take two steps. if (item. To. Insert < L[t]) { These five steps would be executed once for L[t + k] = L[t]; each iteration of the while loop. t -= k; } else These two steps would be executed once still. Looking = false; for each iteration of the inner for loop. L[t + k] = item. To. Insert; } } CS 240 Chapter 9 – Algorithm Efficiency & Sorting 161

Adding all of this up yields the total number of executed steps as: Thus,

Adding all of this up yields the total number of executed steps as: Thus, Shell sort is also O(n 2). CS 240 Chapter 9 – Algorithm Efficiency & Sorting 162

Quick Sort void quick. Sort(list L, int lower, int upper); void partition(list L, int

Quick Sort void quick. Sort(list L, int lower, int upper); void partition(list L, int lo, int hi, int &pivot. Point); void sort(list L, int n) { quick. Sort(L, 0, n-1); } void partition(list L, int lo, int hi, int &pivot. Point) { elt pivot = L[lo]; while (lo < hi) { while ((pivot < L[hi]) && (lo < hi)) hi--; if (hi != lo) { L[lo] = L[hi]; lo++; } while ((pivot > L[lo]) && (lo < hi)) lo++; if (hi != lo) { L[hi] = L[lo]; hi--; } } L[hi] = pivot; pivot. Point = hi; } void quick. Sort(list L, int lower, int upper) { int pivot. Point; partition(L, lower, upper, pivot. Point); if (lower < pivot. Point) quick. Sort(L, lower, pivot. Point - 1); if (upper > pivot. Point) quick. Sort(L, pivot. Point + 1, upper); } CS 240 Chapter 9 – Algorithm Efficiency & Sorting 163

Quick Sort: Step-by-Step Example Moe Hal Hal Hal Hal Hal Hal Hal Hal Hal

Quick Sort: Step-by-Step Example Moe Hal Hal Hal Hal Hal Hal Hal Hal Hal Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy Zeb Zeb Lex Lex Lex Lex Lex Lex Lex Lex Ort Ort Ort Dan Dan Dan Dan Dan Dan Dan CS 240 Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Wes Wes Wes Wes Wes Ida Ida Ida Ida Ida Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Uma Uma Uma Uma Uma Uma Cub Cub Cub Cub Quo Quo Quo Quo Quo Quo Quo Quo Joe Joe Kit Kit Kit Kit Kit Kit Kit Kit Kit Kit Fly Fly Fly Fly Fly Fly Fly Fly Fly Fly Vin Vin Vin Vin Vin Vin Vin Vin Vin Gus Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Joe Joe Joe Joe Joe Joe Joe Joe Joe Vin Vin Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Cub Cub Cub Cub Cub Cub Cub Quo Quo Quo Ida Ida Ida Ida Ida Ida Uma Uma Uma Uma Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Chapter 9 – Algorithm Efficiency & Sorting Dan Dan Dan Dan Wes Wes Wes Wes Wes Wes Lex Lex Lex Ort Ort Ort Ort Ort Ort Ort Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Hal Hal Hal Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia 164

Hal Hal Hal Gus Gus Gus Gus Gus Ann Ann Ann Ann Ann Edy

Hal Hal Hal Gus Gus Gus Gus Gus Ann Ann Ann Ann Ann Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy Cub Lex Lex Lex Fly Fly Fly Fly Fly Fly Fly Fly CS 240 Dan Dan Dan Dan Dan Dan Dan Dan Dan Dan Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Ida Ida Ida Ida Ida Cub Cub Cub Cub Cub Cub Ann Ann Ann Ann Ann Ann Ann Ann Gus Gus Gus Cub Cub Cub Cub Cub Cub Hal Hal Hal Hal Hal Joe Joe Joe Joe Joe Joe Joe Joe Joe Joe Kit Kit Kit Kit Kit Kit Kit Kit Kit Kit Fly Fly Fly Fly Ida Ida Ida Ida Ida Ida Ida Gus Gus Lex Lex Lex Lex Lex Lex Lex Lex Xon Xon Moe Moe Moe Moe Moe Moe Moe Moe Moe Moe Gus Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Quo Quo Quo Quo Quo Quo Quo Quo Quo Quo Uma Uma Uma Uma Uma Uma Uma Uma Uma Uma Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Wes Wes Wes Wes Wes Wes Wes Wes Wes Wes Ort Ort Ort Ort Ort Ort Ort Ort Ort Ort Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia 165

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Cub Cub Cub Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Cub Cub Cub Bob Bob Bob Bob Bob Bob Bob Bob Fly Fly Bob Bob Cub Cub Cub Cub Cub Cub Cub Cub CS 240 Dan Dan Dan Dan Dan Dan Dan Dan Dan Dan Bob Bob Bob Edy Edy Edy Edy Edy Edy Edy Edy Edy Cub Fly Fly Fly Fly Fly Fly Fly Fly Fly Fly Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Joe Joe Joe Joe Joe Ida Ida Ida Ida Ida Ida Kit Kit Kit Kit Kit Kit Joe Joe Joe Joe Joe Ida Ida Ida Ida Ida Kit Kit Kit Kit Kit Kit Lex Lex Lex Lex Lex Lex Lex Lex Lex Lex Moe Moe Moe Moe Moe Moe Moe Moe Moe Moe Xon Xon Xon Xon Xon Xon Xon Tia Tia Tia Vin Vin Vin Vin Vin Vin Vin Vin Vin Vin Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Quo Quo Quo Quo Quo Quo Quo Quo Quo Quo Uma Uma Uma Uma Uma Uma Uma Uma Uma Uma Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Pez Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Wes Wes Wes Wes Wes Wes Wes Wes Wes Wes Ort Ort Ort Ort Ort Ort Ort Ort Ort Ort Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Tia Tia Tia Tia Tia Tia Tia Tia Tia Yul Yul Yul 166

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob

Ann Ann Ann Ann Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Cub Cub Cub Cub Cub Cub Cub Cub Cub Cub CS 240 Dan Dan Dan Dan Dan Dan Dan Dan Dan Dan Edy Edy Edy Edy Edy Edy Edy Edy Edy Edy Fly Fly Fly Fly Fly Fly Fly Fly Fly Fly Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Ida Ida Ida Ida Ida Ida Ida Ida Ida Ida Joe Joe Joe Joe Joe Joe Joe Joe Joe Joe Kit Kit Kit Kit Kit Kit Kit Kit Kit Kit Lex Lex Lex Lex Lex Lex Lex Lex Lex Lex Moe Moe Moe Moe Moe Moe Moe Moe Moe Moe Tia Tia Tia Ort Ort Ort Ort Ort Nan Nan Nan Nan Vin Vin Vin Ren Ren Ren Ren Ren Ort Ort Ort Nan Nan Nan Nan Nan Nan Nan Ren Ren Ren Pez Pez Pez Sue Sue Sue Sue Sue Sue Sue Sue Sue Quo Quo Quo Quo Quo Quo Quo Quo Quo Quo Quo Ren Ren Uma Uma Uma Uma Uma Pez Pez Pez Pez Sue Sue Pez Pez Pez Pez Pez Tia Tia Tia Tia Tia Tia Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Uma Uma Uma Uma Uma Uma Uma Wes Wes Wes Wes Wes Wes Wes Wes Wes Wes Ort Ort Vin Vin Vin Vin Vin Vin Vin Vin Vin Pez Pez Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Zeb Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul 167

Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Cub Cub Cub Cub

Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Cub Cub Cub Cub Cub Dan Dan Dan Dan Dan Edy Edy Edy Edy Edy Fly Fly Fly Fly Fly Gus Gus Gus Gus Gus Hal Hal Hal Hal Hal Ida Ida Ida Ida Ida Joe Joe Joe Joe Joe Kit Kit Kit Kit Kit Lex Lex Lex Lex Lex Moe Moe Moe Moe Moe Nan Nan Nan Nan Nan Ort Ort Ort Ort Ort Pez Pez Pez Pez Pez Quo Quo Quo Quo Quo Ren Ren Ren Ren Ren Sue Sue Sue Sue Sue Tia Tia Tia Tia Tia Uma Uma Uma Uma Uma Wes Wes Vin Vin Vin Vin Vin Wes Wes Xon Xon Xon Xon Xon Zeb Zeb Zeb Zeb Yul Yul Yul Yul Yul Zeb Zeb Quick Sort Analysis For the worst case of quick sort (i. e. , no pivot point reduces the problem size): void sort(list L, int n) { quick. Sort(L, 0, n-1); } void quick. Sort(list L, int lower, int upper) { int pivot. Point; partition(L, lower, upper, pivot. Point); if (lower < pivot. Point) quick. Sort(L, lower, pivot. Point - 1); if (upper > pivot. Point) quick. Sort(L, pivot. Point + 1, upper); } CS 240 In the worst case, the pivot point always comes back with the value of lower. In the worst case, this recursive call is always executed. Chapter 9 – Algorithm Efficiency & Sorting 168

Quick Sort Analysis (Continued) void partition(list L, int lo, int hi, int &pivot. Point)

Quick Sort Analysis (Continued) void partition(list L, int lo, int hi, int &pivot. Point) { elt pivot = L[lo]; while (lo < hi) { while ((pivot < L[hi]) && (lo < hi)) hi--; In the worst case, one of these if (hi != lo) loops will iterate hi-lo times. { L[lo] = L[hi]; lo++; } while ((pivot > L[lo]) && (lo < hi)) lo++; if (hi != lo) { L[hi] = L[lo]; Basically, in the worst case, each hi--; value becomes the pivot point } } without splitting the list at all, so L[hi] = pivot; the ith pivot point will yield n-i pivot. Point = hi; } comparisons, making the worstcase time complexity O(n 2). CS 240 Chapter 9 – Algorithm Efficiency & Sorting 169

Merge Sort void order(list source, list dest, int lower, int upper); void merge(list source,

Merge Sort void order(list source, list dest, int lower, int upper); void merge(list source, list dest, int lower, int middle, int upper); void sort(list L, int n) { list Lcopy; for (int k = 0; k < n; k++) Lcopy[k] = L[k]; order(Lcopy, L, 0, n - 1); } void order(list source, list dest, int lower, int upper) { int middle; if (lower != upper) { middle = (lower + upper) / 2; order(dest, source, lower, middle); order(dest, source, middle + 1, upper); merge(source, dest, lower, middle, upper); } } void merge(list source, list dest, int lower, int middle, int upper) { int s 1 = lower; int s 2 = middle + 1; int d = lower; do { if (source[s 1] < source[s 2]) { dest[d] = source[s 1]; s 1++; } else { dest[d] = source[s 2]; s 2++; } d++; } while ((s 1 <= middle) && (s 2 <= upper)); if (s 1 > middle) do { dest[d] = source[s 2]; s 2++; d++; } while (s 2 <= upper); else do { dest[d] = source[s 1]; s 1++; d++; } while (s 1 <= middle); } CS 240 Chapter 9 – Algorithm Efficiency & Sorting 170

Merge Sort: Step-by-Step Example Moe Moe Edy Edy Edy Ann Ann Ann Ann Ann

Merge Sort: Step-by-Step Example Moe Moe Edy Edy Edy Ann Ann Ann Ann Ann Ann Edy Edy Moe Moe Moe Bob Bob Bob Bob Bob Bob Zeb Zeb Zeb Ort Ort Edy Edy Edy Edy Edy Edy CS 240 Ort Ort Ort Zeb Zeb Moe Moe Moe Moe Fly Fly Fly Bob Bob Bob Bob Ann Ort Ort Ort Ort Kit Kit Kit Wes Wes Wes Wes Bob Wes Wes Wes Wes Moe Moe Moe Ann Ann Ann Ann Wes Zeb Zeb Zeb Zeb Ort Ort Ort Uma Uma Uma Uma Uma Quo Quo Kit Kit Fly Quo Quo Quo Quo Quo Quo Quo Uma Uma Quo Quo Kit Uma Uma Uma Kit Kit Kit Kit Kit Uma Uma Quo Vin Vin Vin Fly Fly Fly Fly Fly Fly Fly Uma Wes Wes Wes Vin Vin Vin Vin Vin Vin Vin Xon Xon Xon Xon Xon Xon Xon Xon Xon Xon Zeb Zeb Zeb Gus Gus Gus Gus Gus Gus Gus Gus Gus Gus Joe Joe Joe Joe Joe Joe Joe Joe Joe Joe Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Sue Sue Sue Sue Sue Sue Sue Sue Sue Sue Cub Cub Cub Cub Cub Cub Cub Cub Cub Cub Ida Ida Ida Ida Ida Ida Ida Ida Ida Ida Yul Yul Yul Yul Yul Yul Yul Yul Yul Yul Chapter 9 – Algorithm Efficiency & Sorting Ren Ren Ren Ren Ren Ren Ren Ren Ren Ren Dan Dan Dan Dan Dan Dan Dan Dan Dan Dan Lex Lex Lex Lex Lex Lex Lex Lex Lex Lex Pez Pez Pez Pez Pez Pez Pez Pez Pez Pez Hal Hal Hal Hal Hal Hal Hal Hal Hal Hal Tia Tia Tia Tia Tia Tia Tia Tia Tia Tia 171

Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Edy Edy

Ann Ann Ann Ann Ann Ann Bob Bob Bob Bob Bob Bob Edy Edy Edy Edy Edy Cub Fly Fly Fly Fly Fly Dan Kit Kit Kit Kit Kit Edy Moe Moe Moe Moe Moe Fly Ort Ort Ort Ort Ort Gus Quo Quo Quo Quo Quo Hal Uma Uma Uma Uma Uma Ida Vin Vin Vin Vin Vin Joe Wes Wes Wes Wes Wes Kit Xon Xon Xon Xon Xon Lex Zeb Zeb Zeb Zeb Zeb Moe Gus Gus Gus Cub Cub Cub Cub Nan Joe Joe Joe Gus Gus Gus Gus Dan Ort Nan Nan Nan Ida Ida Ida Ida Gus Pez Sue Sue Sue Joe Joe Joe Joe Hal Quo Cub Cub Cub Nan Nan Nan Nan Ida Ren Ida Ida Ida Sue Sue Sue Sue Joe Sue Yul Yul Yul Yul Yul Lex Tia Ren Ren Dan Dan Dan Nan Uma Dan Dan Ren Ren Lex Lex Hal Pez Vin Lex Lex Lex Ren Ren Lex Ren Wes Pez Pez Pez Pez Hal Hal Pez Sue Xon Hal Hal Hal Hal Pez Pez Ren Tia Yul Tia Tia Tia Tia Tia Yul Zeb Merge Sort Analysis An analysis of the code indicates that the total execution time of the Merge sort will always be: where order. Time() is the amount of time it takes to execute the order function. CS 240 Chapter 9 – Algorithm Efficiency & Sorting 172

Merge Sort Analysis (Continued) An analysis of the order function indicates that it satisfies

Merge Sort Analysis (Continued) An analysis of the order function indicates that it satisfies the recurrence relation: where merge. Time() is the amount of time it takes to execute the merge function, which evaluates simply to: Putting this all together and solving the recurrence relation yields: Therefore, the total execution time of the Merge sort will always be: So, the Merge sort is O(nlogn). CS 240 Chapter 9 – Algorithm Efficiency & Sorting 173

Radix Sort #include <cstdlib> const int NBR_OF_LTRS = 26; const int MAX_STRING_SIZE = 3;

Radix Sort #include <cstdlib> const int NBR_OF_LTRS = 26; const int MAX_STRING_SIZE = 3; void sort(list L, int n) { int bin. Nbr, item. Nbr, ltr. Nbr, index, i; list bin[NBR_OF_LTRS]; int bin. Size[NBR_OF_LTRS]; // This loop controls which index is used to classify the list data for bin insertion. for (ltr. Nbr = MAX_STRING_SIZE-1; ltr. Nbr >= 0; ltr. Nbr--) { // Set all bins to empty. for (bin. Nbr = 0; bin. Nbr < NBR_OF_LTRS; bin. Nbr++) bin. Size[bin. Nbr] = 0; // Put list elements into bins, using the character at index ltr. Nbr. for (item. Nbr = 0; item. Nbr < n; ++item. Nbr) { index = int(toupper(L[item. Nbr][ltr. Nbr ]) - 'A'); bin[index][bin. Size[index ]] = L[item. Nbr]; bin. Size[index]++; } // Load bin contents back into the list. index = 0; for (bin. Nbr = 0; bin. Nbr < NBR_OF_LTRS; bin. Nbr++) for (i = 0; i < bin. Size[bin. Nbr]; i++) { L[index] = bin[bin. Nbr][i]; index++; } } } CS 240 Chapter 9 – Algorithm Efficiency & Sorting 174

Radix Sort Step-by-Step Example Moe Gus Edy Joe Zeb Nan Ort Sue Bob Cub

Radix Sort Step-by-Step Example Moe Gus Edy Joe Zeb Nan Ort Sue Bob Cub Wes Ida Ann Yul Uma Ren Quo Dan Kit Lex Fly Pez Vin Hal Xon Tia Hal Edy Ann Fly Vin Pez Tia Yul Vin Quo Kit Gus Kit Xon Lex Yul Moe Zeb Sort by third letter Uma Xon Ida Nan Tia Ren Zeb Dan Bob Quo Cub Wes Moe Gus Joe Ort Sue Kit Yul Lex Sort by second letter Hal Fly Nan Uma Dan Ann Ida Bob Edy Moe Zeb Joe Ren Xon Wes Ort Lex Cub Pez Sue Sort by first letter Ann Nan Bob Ort Cub Pez Dan Quo Edy Ren Fly Sue Gus Tia Hal Uma Ida Vin Joe Wes In general, the time complexity of the radix sort is O(kmn), where n is the array size, k is the maximum number of digits, and m is the number of possible values for a digit. CS 240 Chapter 9 – Algorithm Efficiency & Sorting 175

Sorting Algorithm Summary Algorithm Worst Case Best Case Bubble Sort O(n 2): List in

Sorting Algorithm Summary Algorithm Worst Case Best Case Bubble Sort O(n 2): List in Reverse Order O(n): List in Order Selection Sort O(n 2): Any List Insertion Sort O(n 2): List in Reverse Order O(n): List in Order Shell Sort O(n 2): Depends on Increments O(nlogn): List in Order Quick Sort O(n 2): Depends on Pivots O(nlogn): Perfect Shuffle Merge Sort O(nlogn): Any List Radix Sort O(kmn): Any List CS 240 Chapter 9 – Algorithm Efficiency & Sorting 176