69
Sorting and Efficiency Eric Roberts CS 106B

Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Embed Size (px)

Citation preview

Page 1: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Sorting and Efficiency

Eric RobertsCS 106B

Page 2: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

The Uses of Recursion

“Y” by Mark WallingerMagdalen College, Oxford

Page 3: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Sorting• Of all the algorithmic problems that computer scientists have

studied, the one with the broadest practical impact is certainly the sorting problem, which is the problem of arranging the elements of an array or a vector in order.

• The sorting problem comes up, for example, in alphabetizing a telephone directory, arranging library records by catalogue number, and organizing a bulk mailing by ZIP code.

• There are many algorithms that one can use to sort an array. Because these algorithms vary enormously in their efficiency, it is critical to choose a good algorithm, particularly if the application needs to work with large arrays.

Page 4: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Spark Breaks Previous Large-Scale Sort RecordHadoopWorld Record

Spark100 TB

Spark1 PB

Data Size 102.5 TB 100 TB 1000 TB

Elapsed Time 72 mins 23 mins 234 mins

# Nodes 2100 206 190

# Cores 50400 6592 6080

# Reducers 10,000 29,000 250,000

Rate 1.42 TB/min 4.27 TB/min 4.27 TB/min

Rate/node 0.67 GB/min 20.7 GB/min 22.5 GB/min

Sort Benchmark Daytona Rules

Yes Yes No

Environment dedicated data center EC2 (i2.8xlarge) EC2 (i2.8xlarge)

http://databricks.com/blog/2014/10/10/spark-breaks-previous-large-scale-sort-record.html

sorting 100 TB of data effectively generates 500 TB of disk I/O and 200 TB of network I/O

Page 5: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Formal Definition

• The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order);

• The output is a permutation (reordering) of the input.

• Computational complexity (worst, average and best behavior)

• Space complexity: Memory usage, eg., in place : O(1)• Recursion• Stability • Comparison sort

Page 6: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Stability • stable sorting algorithms maintain the

relative order of records with equal keys (i.e., values).

Page 7: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

The Selection Sort Algorithm• Of the many sorting algorithms, the easiest one to describe is

selection sort, which appears in the text like this:

void sort(Vector<int> & vec) { int n = vec.size(); for (int lh = 0; lh < n; lh++) { int rh = lh; for (int i = lh + 1; i < n; i++) { if (vec[i] < vec[rh]) rh = i; } int temp = vec[lh]; vec[lh] = vec[rh]; vec[rh] = temp; }}

• Coding this algorithm as a single function makes sense for efficiency but complicates the analysis. The next two slides decompose selection sort into a set of functions that make the operation easier to follow.

Page 8: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

/* * Function: sort * -------------- * Sorts a Vector<int> into increasing order. This implementation * uses an algorithm called selection sort, which can be described * in English as follows. With your left hand (lh), point at each * element in the vector in turn, starting at index 0. At each * step in the cycle: * * 1. Find the smallest element in the range between your left * hand and the end of the vector, and point at that element * with your right hand (rh). * * 2. Move that element into its correct position by swapping * the elements indicated by your left and right hands. */

void sort(Vector<int> & vec) { for ( int lh = 0 ; lh < vec.size() ; lh++ ) { int rh = findSmallest(vec, lh, vec.size() - 1); swap(vec[lh], vec[rh]); }}

Decomposition of the sort Function

Page 9: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

/* * Function: sort * -------------- * Sorts a Vector<int> into increasing order. This implementation * uses an algorithm called selection sort, which can be described * in English as follows. With your left hand (lh), point at each * element in the vector in turn, starting at index 0. At each * step in the cycle: * * 1. Find the smallest element in the range between your left * hand and the end of the vector, and point at that element * with your right hand (rh). * * 2. Move that element into its correct position by swapping * the elements indicated by your left and right hands. */

void sort(Vector<int> & vec) { for ( int lh = 0 ; lh < vec.size() ; lh++ ) { int rh = findSmallest(vec, lh, vec.size() - 1); swap(vec[lh], vec[rh]); }}

/* * Function: findSmallest * ---------------------- * Returns the index of the smallest value in the vector between * index positions p1 and p2, inclusive. */

int findSmallest(Vector<int> & vec, int p1, int p2) { int smallestIndex = p1; for ( int i = p1 + 1 ; i <= p2 ; i++ ) { if (vec[i] < vec[smallestIndex]) smallestIndex = i; } return smallestIndex;}

/* * Function: swap * -------------- * Exchanges two integer values passed by reference. */

void swap(int & x, int & y) { int temp = x; x = y; y = temp;}

Decomposition of the sort Function

Page 10: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Simulating Selection Sort

0 1 2 3 4 5 6 7 8 9

809 503 946 367 987 838 259 236 659 361

skip simulation

int main() { Vector<int> vec = createTestVector(); sort(vec); return 0;}

vec

void sort(Vector<int> & vec) { for ( int lh = 0 ; lh < vec.size() ; lh++ ) { int rh = findSmallest(vec, lh, vec.size() - 1); swap(vec[lh], vec[rh]); }}

rh veclh

012345678910 36789

int findSmallest(Vector<int> & vec, int p1, int p2) { int smallestIndex = p1; for ( int i = p1 + 1 ; i <= p2 ; i++ ) { if (vec[i] < vec[smallestIndex]) smallestIndex = i; } return smallestIndex;}

9

p2 vec

0

p1ismallestIndex

1234567891001367

0 1 2 3 4 5 6 7 8 9

809 503 946 367 987 838 259 236 659 361

int main() { Vector<int> vec = createTestVector(); sort(vec); return 0;}

vec

void sort(Vector<int> & vec) { for ( int lh = 0 ; lh < vec.size() ; lh++ ) { int rh = findSmallest(vec, lh, vec.size() - 1); swap(vec[lh], vec[rh]); }}

rh veclh

Page 11: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Is Selection Sort Stable?

• 2 5 8 5 9

• 5 8 5 2 9

Page 12: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Efficiency of Selection Sort• The primary question for today is how one might evaluate the

efficiency of an algorithm such as selection sort.

• One strategy is to measure the actual time it takes to run for arrays of different sizes. In C++, you can measure elapsed time by calling the time function, which returns the current time in milliseconds. Using this strategy, however, requires some care:– The time function is often too rough for accurate measurement.

It therefore makes sense to measure several runs together and then divide the total time by the number of repetitions.

– Most algorithms show some variability depending on the data. To avoid distortion, you should run several independent trials with different data and average the results.

– Some measurements are likely to be wildly off because the computer needs to run some background task. Such data points must be discarded as you work through the analysis.

Page 13: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Measuring Sort Timings

.0021 .0025 .0022 .0026 .0020 .0030 .0022 .0023 .0022 .0025

.006 .007 .008 .007 .007 .011 .007 .007 .007 .007

.014 .014 .014 .015 .014 .014 .014 .014 .014 .014

.028 .024 .025 .026 .023 .025 .025 .026 .025 .027

.039 .037 .036 .041 .042 .039 .140 .039 .034 .038

.187 .152 .168 .176 .146 .146 .165 .146 .178 .154

3.94 3.63 4.06 3.76 4.11 3.51 3.48 3.64 3.31 3.45

13.40 12.90 13.80 17.60 12.90 14.10 12.70 81.60 16.00 15.50

322.5 355.9 391.7 321.6 388.3 321.3 321.3 398.7 322.1 321.3

1319. 1388. 1327. 1318. 1331. 1336. 1318. 1335. 1325. 1319.

N = 10

20

30

40

50

100

500

1000

5000

10000

Trial 1

Trial 2

Trial 3

Trial 4

Trial 5

Trial 6

Trial 7

Trial 8

Trial 9

Trial 10 .0024

.007

.014

.025

.049

.162

3.69

21.05

346.4

1332.

.00029

.00139

.00013

.0014

.0323

.0151

0.272

21.33

33.83

20.96

.011

.140

81.60

1388.

.039

14.32

1326.

.00036

.0025

1.69

7.50

Because timing measurements are subject to various inaccuracies, it is best to run several trials and then to use statistics to interpret the results. The table below shows the actual running time for the selection sort algorithm for several different values of N, along with the mean () and standard deviation ().

The table entries shown in red indicate timing measurements that differ by more than two standard deviations from the average of the other trials (trial #8 for 1000 elements, for example, is more than five times larger than any other trial). Because these outliers probably include background tasks, it is best to discard them.

The following table shows the average timing of the selection sort algorithm after removing outlying trials that differ by more than two standard deviations from the mean. The column labeled (the Greek letter mu, which is the standard statistical symbol for the mean) is a reasonably good estimate of running time.

Page 14: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Selection Sort Running Times• Many algorithms that operate on vectors have running times

that are proportional to the size of the array. If you multiply the number of values by ten, you would expect those algorithms to take ten times as long.

10

100

1000

10000

.0024

0.162

14.32

1332.

N time• As the running times on the preceding slide make clear, the situation for selection sort is very different. The table on the right shows the average running time when selection sort is applied to 10, 100, 1000, and 10000 values.

• As a rough approximation—particularly as you work with larger values of N—it appears that every ten-fold increase in the size of the array means that selection sort takes about 100 times as long.

Page 15: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Counting Operations• Another way to estimate the running time is to count how

many operations are required to sort an array of size N.

• In the selection sort implementation, the section of code that is executed most frequently (and therefore contributes the most to the running time) is the body of the findSmallest method. The number of operations involved in each call to findSmallest changes as the algorithm proceeds:

N values are considered on the first call to findSmallest. N - 1 values are considered on the second call. N - 2 values are considered on the third call, and so on.

• In mathematical notation, the number of values considered in findSmallest can be expressed as a summation, which can then be transformed into a simple formula:

∑i = 1

N

i1 + 2 + 3 + . . . + (N - 1) + N = = N x (N + 1)

2

Page 16: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

A Geometric Insight• You can convince yourself that

by thinking about the problem geometrically.

1 + 2 + 3 + . . . + (N - 2) + (N - 1) + N = N x (N + 1)

2

• The terms on the left side of the formula can be arranged into a triangle, as shown at the bottom of this slide for N = 6.

• If you duplicate the triangle and rotate it by 180˚, you get a rectangle that in this case contains 6 x 7 dots, half of which belong to each triangle.

Page 17: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Quadratic Growth• The reason behind the rapid growth in the running time of

selection sort becomes clear if you make a table showing the xxx

55

N

5050

500,500

50,005,000

10

100

1000

10000

N x (N + 1) 2

N x (N + 1) 2

• The growth pattern in the right column is similar to that of the measured running time of the selection sort algorithm. As the x N x (N + 1)

2 value of N increases by a factor of 10, the value of xxincreases by a factor of around 100, which is 102. Algorithms whose running times increase in proportion to the square of the problem size are said to be quadratic.

value of for various values of N:

Page 18: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Big-O Notation• The most common way to express computational complexity

is to use big-O notation, which was introduced by the German mathematician Paul Bachmann in 1892.

• Big-O notation consists of the letter O followed by a formula that offers a qualitative assessment of running time as a function of the problem size, traditionally denoted as N. For example, the computational complexity of linear search is

O ( N )and the computational complexity of selection sort is

O ( N 2

)• If you read these formulas aloud, you would pronounce them

as “big-O of N ” and “big-O of N 2

” respectively.

Page 19: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Common Simplifications of Big-O• Given that big-O notation is designed to provide a qualitative

assessment, it is important to make the formula inside the parentheses as simple as possible.

• When you write a big-O expression, you should always make the following simplifications:

Eliminate any term whose contribution to the running time ceases to be significant as N becomes large.

1.

Eliminate any constant factors.2.

• The computational complexity of selection sort is therefore

O ( N 2

)

N x (N + 1) 2 O

( )and not

Page 20: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Deducing Complexity from the Code• In many cases, you can deduce the computational complexity

of a program directly from the structure of the code.

• The standard approach to doing this type of analysis begins with looking for any section of code that is executed more often than other parts of the program. As long as the individual operations involved in an algorithm take roughly the same amount of time, the operations that are executed most often will come to dominate the overall running time.

• In the selection sort implementation, for example, the most commonly executed statement is the if statement inside the findSmallest method. This statement is part of two for loops, one in findSmallest itself and one in Sort. The total number of executions is

1 + 2 + 3 + . . . + (N - 1) + N

which is O(N

2).

Page 21: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Finding a More Efficient Strategy• As long as arrays are small, selection sort is a perfectly

workable strategy. Even for 10,000 elements, the average running time of selection sort is just over a second.

• The quadratic behavior of selection sort, however, makes it less attractive for the very large arrays that one encounters in commercial applications. Assuming that the quadratic growth pattern continues beyond the timings reported in the table, sorting 100,000 values would require two minutes, and sorting 1,000,000 values would take more than three hours.

• The computational complexity of the selection sort algorithm, however, holds out some hope:– Sorting twice as many elements takes four times as long.– Sorting half as many elements takes only one fourth the time.– Is there any way to use sorting half an array as a subtask in a

recursive solution to the sorting problem?

Page 22: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

The Merge Sort Idea

0 1 2 3 4

809 503 946 367 9875 6 7 8 9

838 259 236 659 361

809 503 946 367 987

838 259 236 659 361

809 503 946 367 987

838 259 236 659 361

367 503 809 946 9870 1 2 3 4

236 259 361 659 8380 1 2 3 4

vec

0

2361

2592

3613

3674

5035

6596

8097

8388

9469

987

v1

v2

Divide the vector into two halves: v1 and v2.1.

Sort each of v1 and v2 recursively.2.

Clear the original vector.3.

Merge elements into the original vector by choosing the smallest element from v1 or v2 on each cycle.

4

Page 23: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

/* * The merge sort algorithm consists of the following steps: * * 1. Divide the vector into two halves. * 2. Sort each of these smaller vectors recursively. * 3. Merge the two vectors back into the original one. */

void sort(Vector<int> & vec) { int n = vec.size(); if (n <= 1) return; Vector<int> v1; Vector<int> v2; for (int i = 0; i < n; i++) { if (i < n / 2) { v1.add(vec[i]); } else { v2.add(vec[i]); } } sort(v1); sort(v2); vec.clear(); merge(vec, v1, v2);}

The Merge Sort Implementation

Page 24: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

/* * The merge sort algorithm consists of the following steps: * * 1. Divide the vector into two halves. * 2. Sort each of these smaller vectors recursively. * 3. Merge the two vectors back into the original one. */

void sort(Vector<int> & vec) { int n = vec.size(); if (n <= 1) return; Vector<int> v1; Vector<int> v2; for (int i = 0; i < n; i++) { if (i < n / 2) { v1.add(vec[i]); } else { v2.add(vec[i]); } } sort(v1); sort(v2); vec.clear(); merge(vec, v1, v2);}

/* * Function: merge * --------------- * This function merges two sorted vectors (v1 and v2) into the * vector vec, which should be empty before this operation. * Because the input vectors are sorted, the implementation can * always select the first unused element in one of the input * vectors to fill the next position. */

void merge(Vector<int> & vec, Vector<int> & v1, Vector<int> & v2) { int n1 = v1.size(); int n2 = v2.size(); int p1 = 0; int p2 = 0; while (p1 < n1 && p2 < n2) { if (v1[p1] < v2[p2]) { vec.add(v1[p1++]); } else { vec.add(v2[p2++]); } } while (p1 < n1) vec.add(v1[p1++]); while (p2 < n2) vec.add(v2[p2++]);}

The Merge Sort Implementation

Page 25: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

The Complexity of Merge Sort

Sorting 8 items

Two sorts of 4 items

requires

Four sorts of 2 items

which requires

Eight sorts of 1 item

which requires

The work done at each level (i.e., the sum of the work done by all the calls at that level) is proportional to the size of the vector. The running time is therefore proportional to N times the number of levels.

Page 26: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

How Many Levels Are There?• The number of levels in the merge sort decomposition is equal

to the number of times you can divide the original vector in half until there is only one element remaining. In other words, what you need to find is the value of k that satisfies the following equation:

1 = N / 2 / 2 / 2 / 2 . . . / 2

k times

• You can simplify this formula using basic mathematics:

1 = N / 2k

2k = Nk = log2 N

• The complexity of merge sort is therefore O(N log N).

Page 27: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Comparing N

2 and N log N• The difference between O(N

2) and O(N log N) is enormous for large values of N, as shown in this table:

1,000,000 1,000,000,000,000 19,931,569

100,000 10,000,000,000 1,660,964

10,000 100,000,000 132,877

1,000 1,000,000 9,966

100 10,000 664

10 100 33

N 2 N log2 NN

• Based on these numbers, the theoretical advantage of using merge sort over selection sort on a vector of 1,000,000 values would be a factor of more than 50,000.

Page 28: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Standard Complexity Classes

exponential O(2N) Tower of Hanoi solution

cubic O(N 3) Obvious algorithms for matrix multiplication

quadratic O(N 2) Selection sort

N log N O(N log N) Merge sort

linear O(N) Summing a vector; linear search

logarithmic O(log N) Binary search in a sorted vector

constant O(1) Finding first element in a vector

• The complexity of a particular algorithm tends to fall into one of a small number of standard complexity classes:

• In general, theoretical computer scientists regard any problem whose complexity cannot be expressed as a polynomial as intractable.

Page 29: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Graphs of the Complexity Classesru

nnin

g t

ime

problem size

Page 30: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Part II More Sorting Algorithms

Jingyu Zhou

Page 31: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Bubble Sort

• Simplest sorting algorithm

• Idea: – 1. Set flag = false– 2. Traverse the array and compare pairs of two

elements • 1.1 If E1 E2 - OK

• 1.2 If E1 > E2 then Switch(E1, E2) and set flag = true

– 3. If flag = true goto 1.

• What happens?

Page 32: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Bubble Sort1 1 23 2 56 9 8 10 1002 1 2 23 56 9 8 10 1003 1 2 23 9 56 8 10 1004 1 2 23 9 8 56 10 1005 1 2 23 9 8 10 56 100---- finish the first traversal -------- start again ----1 1 2 23 9 8 10 56 1002 1 2 9 23 8 10 56 1003 1 2 9 8 23 10 56 1004 1 2 9 8 10 23 56 100---- finish the second traversal -------- start again ----………………….

Why Bubble Sort ?

Page 33: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Implement Bubble Sort with an Array

void bubbleSort (Array S, length n) {boolean isSorted = false;while(!isSorted) {

isSorted = true;for(i = 0; i<n; i++) { if(S[i] > S[i+1]) {

int aux = S[i];S[i] = S[i+1]; S[i+1] = aux; isSorted = false;

} }}

Page 34: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Running Time for Bubble Sort

• One traversal = move the maximum element at the end

• Traversal #i : n – i + 1 operations

• Running time:

(n – 1) + (n – 2) + … + 1 = (n – 1) n / 2 = O(n 2)

• When does the worst case occur ?

• Best case ?

Page 35: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Bubble Sort summary

Name Best Average Worst Memory Stable

Bubble sort

Yes

Page 36: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Quick-Sort• Another divide-and-conquer sorting algorithm• Idea:1) Divide : If the sequence S has 2 or more elements,

select an element x from S to be your pivot (e.g., the last), and divide them into 3 sequences:L, holds S’s elements less than xE, holds S’s elements equal to xG, holds S’s elements greater than x

2) Recurse: Recursively sort L and G3) Conquer: Finally, to put elements back into S in

order, first inserts the elements of L, then those of E, and those of G.

Page 37: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Idea of Quick Sort

1) Select: pick an element

2) Divide: rearrange elements so that x goes to its final position E

3) Recursion and Conquer: recursively sort

Page 38: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Quick-Sort Tree

Page 39: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

In-Place Quick-Sort

Divide step: l scans the sequence from the left, and r from the right.

A swap is performed when l is at an element larger than the pivot and r is at one smaller than the pivot.

Page 40: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

In Place Quick Sort (cont’d)

A final swap with the pivot completes the divide step

Page 41: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Running time analysis

• Average case analysis

• Worst case analysis

• What is the worst case for quick-sort?

• Running time?

Page 42: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Quick Sort Running Time• Worst case: when the pivot does not divide the sequence in

two– At each step, the length of the sequence is only reduced by 1– Total running time

• General case:– Time spent at level i in the tree is O(n)– Running time: O(n) * O(height)

• Average case:– O(n log n)

1

2 )()(ni

i nOSlength

Page 43: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

What to do?

• Choose pivot differently– Compute actual median

• O(N) algorithm exists that guarantee 50/50 split

– “Median of three”• Approximate median with the first, last, middle

– Random• Worst case possible, but unlikely

Page 44: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Quick Sort Summary

Name Best Average Worst Memory Stable

Quicksort

on

average, worst

case is ;

typical in-place

sort is not stable;

stable versions

exist

Page 45: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

More Sorting Algorithms

• Bucket Sort

• Radix Sort

Page 46: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Bucket Sort• Bucket sort

– Assumption: the keys are in the range [0, N)– Basic idea:

1. Create N linked lists (buckets) to divide interval [0,N) into subintervals of size 1

2. Add each input element to appropriate bucket3. Concatenate the buckets

– Expected total time is O(n + N), with n = size of original sequence

• if N is O(n) sorting algorithm in O(n) !

Page 47: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Bucket SortEach element of the array is put in one of the N “buckets”

Page 48: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Bucket Sort

Now, pull the elements from the buckets into the array

At last, the sorted array (sorted in a stable way):

Page 49: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Does it Work for Real Numbers?

• What if keys are not integers?– Assumption: input is n reals from [0, 1)– Basic idea:

• Create N linked lists (buckets) to divide interval [0,1) into subintervals of size 1/N

• Add each input element to appropriate bucket and sort buckets with insertion sort

– Uniform input distribution O(1) bucket size• Therefore the expected total time is O(n)

– Distribution of keys in buckets similar with …. ?

Page 50: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Bucket Sort Summary

Name Best Average Worst Memory Stable

Bucket

sort (integer

keys)

Yes

Page 51: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Radix Sort

• How did IBM get rich originally?

• Answer: punched card readers for census tabulation in early 1900’s. – In particular, a card sorter that could sort cards into

different bins• Each column can be punched in 12 places

• (Decimal digits use only 10 places!)

– Problem: only one column can be sorted on at a time

http://www-31.ibm.com/ibm/cn/ibm100/icons/tabulator/index.shtml

Page 52: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Radix Sort

• Intuitively, you might sort on the most significant digit, then the second most significant, etc.

• Problem: lots of intermediate piles of cards to keep track of

• Key idea: sort the least significant digit ( LSD ) first RadixSort(A, d)

for i=1 to d

StableSort(A) on digit i

Page 53: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Example• Sorting a sequence of 4-bit integers

1001

0010

1101

0001

1110

0010

1110

1001

1101

0001

1001

1101

0001

0010

1110

1001

0001

0010

1101

1110

0001

0010

1001

1101

1110

Page 54: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford
Page 55: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Radix Sort

• Can we prove it will work?

• Inductive argument:– Assume lower-order digits {j: j<i}are sorted– Show that sorting next digit i leaves array

correctly sorted • If two digits at position i are different, ordering

numbers by that digit is correct (lower-order digits irrelevant)

• If they are the same, numbers are already sorted on the lower-order digits. Since we use a stable sort, the numbers stay in the right order

Page 56: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Radix Sort

• What sort will we use to sort on digits?

• Bucket sort is a good choice: – Sort n numbers on digits that range from 1..N– Time: O(n + N)

• Each pass over n numbers with d digits takes time O(n+k), so total time O(dn+dk)– When d is constant and k=O(n), takes O(n)

time

Page 57: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Radix Sort Example

• Problem: sort 1 million 64-bit numbers– Treat as four-digit radix 216 numbers– Can sort in just four passes with radix sort!– Running time: 4( 1 million + 216 ) 4 million

operations

• Compare with typical O(n lg n) comparison sort – Requires approx. lg n = 20 operations per number

being sorted– Total running time 20 million operations

Page 58: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Radix Sort

• In general, radix sort based on bucket sort is– Asymptotically fast (i.e., O(n))– Simple to code– A good choice

• Can radix sort be used on floating-point numbers?

Page 59: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Summary: Radix Sort

• Radix sort:– Assumption: input has d digits ranging from 0 to k– Basic idea:

• Sort elements by digit starting with least significant

• Use a stable sort (like bucket sort) for each stage

– Each pass over n numbers with 1 digit takes time O(n+k), so total time O(dn+dk)

• When d is constant and k=O(n), takes O(n) time

– Fast, Stable, Simple– Doesn’t sort in place

Page 60: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Summary: LSD Radix Sort

Name Best Average Worst Memory Stable

LSD

Radix Sort —

Yes

Page 61: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Sorting Algorithms

• Assuming an input sequence of length n– Bubble sort – Insertion sort– Selection sort– Merge sort– Quick sort– Bucket sort– Radix sort

Page 62: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Sorting Algorithms: In-Place Sorting

• A sorting algorithm is said to be in-place if– it uses no auxiliary data structures (however,

O(1) auxiliary variables are allowed)– it updates the input sequence only by means of

operations replaceElement and swapElements

• Which sorting algorithms seen so far can be made to work in place?

– Bubble, Insertion, Selection, Merge, Quick, Bucket, Radix

Page 63: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Spark Review

• In Spark 1.1, we switched our default sorting algorithm from quicksort to TimSort, a derivation of merge sort and insertion sort.

• It performs better than quicksort in most real-world datasets, especially for datasets that are partially ordered.

• We use TimSort in both the map and reduce phases.

Page 64: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Name Best Average Worst Memory Stable

Bubble sort

Yes

Quicksort

on

average, worst

case is ;

typical in-place

sort is not stable;

stable versions

exist

Bucket

sort (integer

keys)

Yes

Merge sort

worst case Yes

Selection sort

No

LSD

Radix Sort —

Yes

Page 65: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

http://www.cs.princeton.edu/~rs/AlgsDS07/18RadixSort.pdf

Page 66: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

http://www.cs.princeton.edu/~rs/AlgsDS07/18RadixSort.pdf

Page 67: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Reference

• PAC Chapter 10, DS Chapter 6

• https://en.wikipedia.org/wiki/Sorting_algorithm

Page 68: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

Next

• Maps And Hasing

• PAC Chapter 15, DS Chapter 7.4

Page 69: Sorting and Efficiency Eric Roberts CS 106B. The Uses of Recursion “Y” by Mark Wallinger Magdalen College, Oxford

The End