Name: Exercise: Big-O and Sorting - Algonquin...

5
Name:________________________ Exercise: Big-O and Sorting Assess Software Efficiency with Order-of-Magnitude Algorithm Analysis Introduction Achieving lightning-fast performance in software solutions, is not a matter of luck. It involves a careful assessment of the performance of each algorithm. In this lab exercise, you’ll experiment with a number of sorting algorithms – measuring the time taken to complete sort operations with ever increasing sizes of data sets. Using this experimental evidence, you’ll be able to identify the Big-O pattern, and predict the time required to sort even larger data sets. That’s the whole point of Big-O analysis: as you increase the size of the data set being processed, how does the performance change. In this case, you’ll be systematically scaling to larger and larger data sets: 2-fold increase; 3-fold increase, 4-fold increase, 5-fold increase, and so on. Ultimately, you’ll be experimenting with four different sort algorithms: bubble sort, selection sort, merge sort, and the Java built-in sort. Before beginning your experiments, look up the documentation for the Java built-in sort. The sort() method is a static member of the Collections class. If you haven’t already done so, you may want to bookmark the online Java documentation. It’s at: http://java.sun.com/javase/7/docs/api You’ll start with bubble sort and selection sort. These algorithms all have a Big-O performance of O(n 2 ). Later, you’ll experiment with better performing algorithms. The Details Launch the Program I wrote a simple Java program to help perform this experimental analysis. You can find it on my website. Here’s a screen capture of the freshly launched program currently executing Bubble Sort. Preparing to Sort You’ll have to choose a sort algorithm and choose a size for your data set. The actual time taken to sort will vary from machine to machine. Primarily, the machine-to-machine time differences depend on the speed of the CPU, and the number of other programs currently running (and, of course, these other programs do demand CPU cycles). To get a sense of the configuration on your machine, launch Task Manager 1 . Based on my screen capture, you’ll notice that my machine had several applications running, but this list is deceptive. At any given time, there could be dozens to hundreds of processes running. Click on the Processes tab to view all programs currently running. 1 You access Task Manager by using <Ctrl-Alt-Del>. What does the documentation say about the algorithm used by Collections.sort()?

Transcript of Name: Exercise: Big-O and Sorting - Algonquin...

Page 1: Name: Exercise: Big-O and Sorting - Algonquin Collegeelearning.algonquincollege.com/coursemat/woollar/... · Big-O Analysis: Merge Sort / Java Sort For Merge Sort and Java Sort, compare

Name:________________________ Exercise: Big-O and Sorting Assess Software Efficiency with Order-of-Magnitude Algorithm Analysis

Introduction Achieving lightning-fast performance in software solutions, is

not a matter of luck. It involves a careful assessment of the

performance of each algorithm.

In this lab exercise, you’ll experiment with a number of

sorting algorithms – measuring the time taken to complete sort

operations with ever increasing sizes of data sets. Using this

experimental evidence, you’ll be able to identify the Big-O

pattern, and predict the time required to sort even larger data

sets.

That’s the whole point of Big-O analysis: as you increase the

size of the data set being processed, how does the performance

change. In this case, you’ll be systematically scaling to larger

and larger data sets: 2-fold increase; 3-fold increase, 4-fold

increase, 5-fold increase, and so on.

Ultimately, you’ll be experimenting with four different sort

algorithms: bubble sort, selection sort, merge sort, and the

Java built-in sort. Before beginning your experiments, look up

the documentation for the Java built-in sort. The sort()

method is a static member of the Collections class. If you

haven’t already done so, you may want to bookmark the

online Java documentation. It’s at:

http://java.sun.com/javase/7/docs/api

You’ll start with bubble sort and selection sort. These

algorithms all have a Big-O performance of O(n2). Later,

you’ll experiment with better performing algorithms.

The Details

Launch the Program

I wrote a simple Java program to help perform this

experimental analysis. You can find it on my website. Here’s a

screen capture of the freshly launched program currently

executing Bubble Sort.

Preparing to Sort

You’ll have to choose a sort algorithm and choose a size for

your data set. The actual time taken to sort will vary from

machine to machine. Primarily, the machine-to-machine time

differences depend on the speed of the CPU, and the number

of other programs currently running (and, of course, these

other programs do demand CPU cycles). To get a sense of the

configuration on your machine, launch Task Manager1.

Based on my screen capture, you’ll notice that my machine

had several applications running, but this list is deceptive. At

any given time, there could be dozens to hundreds of

processes running. Click on the Processes tab to view all

programs currently running.

1 You access Task Manager by using <Ctrl-Alt-Del>.

What does the documentation say about the algorithm used by Collections.sort()?

Page 2: Name: Exercise: Big-O and Sorting - Algonquin Collegeelearning.algonquincollege.com/coursemat/woollar/... · Big-O Analysis: Merge Sort / Java Sort For Merge Sort and Java Sort, compare

You can see that the list of running processes is larger. I’ve

sorted the list by CPU cycles. At the time of my screen

capture, Firefox was consuming the most CPU cycles.

Ultimately, you’ll want to monitor the performance of the

sorting algorithms by viewing the Performance tab of Task

Manager.

Here’s a screen shot of my machine’s performance. When I

captured this image, a number of applications were demanding

CPU cycles. This snapshot shows consumption at 9% of

available CPU cycles. Ideally, you should reduce the

consumption of CPU cycles by closing unnecessary

applications; otherwise they will affect your timing

measurements. When you perform your sorting experiments,

you’ll see CPU Usage change. You’ll also see Physical

Memory Usage change (both real RAM and virtual memory).

Special Note about some Computers

Many laptop computers dynamically adjust the clock speed of

the CPU in an effort to conserve battery life. If your computer

is performing this dynamic adjustment, then your results will

not accurately measure the performance of the sorting

algorithms. You might need to temporarily disable the power-

saving feature to improve reliability of your tests.

Experimenting with Bubble Sort

Start by sorting a small number of integers. Today’s

computers are so fast, that you’re likely to record a time of

0.00 seconds for anything less than 1,000. I’ll leave if for you

to find a suitable starting size (since your computer will likely

have different performance than mine), but look for a small

size that takes close to 1 second. Choose a size that is easily

multiplied. (For example, 20,000 would be a far better choice

than 22,433, though the 20,000 might result in a time of 0.8

rather than a full 1 second. And remember, don’t choose

20,000 just because that’s the value I chose. Find a number

that results in a time of nearly 1 second on your computer.)

Fill out the following chart with your starting size and the time

taken to sort for that size.

Page 3: Name: Exercise: Big-O and Sorting - Algonquin Collegeelearning.algonquincollege.com/coursemat/woollar/... · Big-O Analysis: Merge Sort / Java Sort For Merge Sort and Java Sort, compare

In each of the subsequent rows, increase the data set size by

some multiple (remember, you’ll experiment with the

following kinds of growth: 2-fold increase; 3-fold increase,

4-fold increase, 5-fold increase, etc.).

The column labeled Proportional Increase identifies the factor

by which you chose to increase the data set. This will be one

of 2, 3, 4, 5, 6, etc. This item is grayed in the first row since

it’s the starting size and hasn’t been scaled from a previous

experiment.

In the column Time to Complete record the time taken to a

couple of decimal points.

In the column labeled Proportional Change, you’ll need to

identify the factor by which Time to Complete changed as you

scaled to a larger data set. You don’t want exact numbers with

decimal places in this final column; you want an

approximation. That’s because this experimental evidence for

The Big-O. And the O in The Big-O stands for order of

magnitude. For the sake of argument, imagine that the first

sort (with the smallest data set) took 0.8 second. Your second

sort doubled the size of the data set and imagine that Time to

Complete registered as 2.98. The Proportional Change is

between 3 and 4.2 It’s closer to 4, so record 4.

Chart of Performance: Bubble Sort

Size Proportional Increase

Time to Complete

Proportional Change

x 2

x 3

x 4

x 5

x 6

x 7

x 8

x 9

x 10 If you tried to sort an enormous set of numbers, say

100,000,000, you would have waited a very long time. On my

computer, based on Big-O analysis, sorting 100,000,000

numbers with Bubble Sort would have taken well over 2 years

to complete.

2An arithmetic calculation of the change shows that 2.98 is 3.725 times greater

than 0.8. But because you are concerned with the order-of-magnitude, you

would round that to 4 times greater.

You might have to kill the dragon to terminate an otherwise

intractable sort routine.

Experimenting with Selection Sort

Now that you’ve finished your experiments with Bubble Sort,

perform similar tests with Selection Sort.

Chart of Performance: Selection Sort

Size Proportional Increase

Time to Complete

Proportional Change

x 2

x 3

x 4

x 5

x 6

x 7

x 8

x 9

x 10

Big-O Analysis: Bubble, Selection

Examine the two charts of time for: Bubble Sort, Selection

Sort. Compare the Proportional Increase in the data set size3

with the Proportional Change in time.

3That is, was the increase 2 times, 3 times, 4 times, etc.

What is the relationship?

Experiment to find a Size that comes close to 1 second.

Again, experiment to find a Size that comes close to 1 second.

Page 4: Name: Exercise: Big-O and Sorting - Algonquin Collegeelearning.algonquincollege.com/coursemat/woollar/... · Big-O Analysis: Merge Sort / Java Sort For Merge Sort and Java Sort, compare

Based on this relationship, now estimate the time required to

sort other data set sizes with Bubble Sort (as the number grows

normalize the units to hours, then days, then years). Fill in the

first row with your last experimental reading, and determine

the proportional change between your size and 1,000,000. Fill

in the subsequent rows based on your Big-O calculations.

Chart of Estimated Performance: Bubble Sort

Size Proportional Increase

Time to Complete

Proportional Change

1,000,000 x

5,000,000 x 5

10,000,000 x 2

50.000.000 x 5

100,000,000 x 2

500,000,000 x 5

Preparing to use Merge Sort, Java Sort

You’ve endured the pain of n2 algorithms long enough. It’s

time to reveal the better sorting algorithms: my

implementation of Merge Sort and Java Sort.

Both of these sorting algorithms have dramatically better

Big-O, but there are some subtle differences between Merge

Sort and Java Sort. One of those differences can be identified

by watching the Performance tab in Task Manager. So, before

you begin these experiments, open Task Manager to that tab.

In particular, you’ll need to pay attention to Physical Memory

Usage History. This tracks the amount of memory that is

currently being used.

When you open Task Manager, stretch the window to its

maximum height. In this way, you’ll be able to discern the

differences in memory use when comparing Merge Sort and

Java Sort.

Issues with Garbage Collection

You might have noticed the Garbage Collection button on my

SortTests application. Every time you click this button, my

application politely asks the Java Virtual Machine (VM) to

invoke the garbage collection routines. Sometimes the VM

will; sometimes it won’t. You can monitor the behavior by

watching Physical Memory Usage History.

Experimenting with Merge Sort, Java Sort

Fill out the chart below in much the same way you did for

Bubble Sort and Selection Sort. As a first step, find a starting

size which results in a time of something near 1 second.

Because Merge Sort and Java Sort are so much better, your

starting data set size will be noticeably larger than was the

case with Bubble and Selection sorts.

Chart of Performance: Merge Sort

Size Proportional Increase

Time to Complete

Proportional Change

x 2

x 3

x 4

x 5

x 6

x 7

x 8

x 9

x 10

Chart of Performance: Java Sort

Size Proportional Increase

Time to Complete

Proportional Change

x 2

x 3

x 4

x 5

x 6

x 7

x 8

x 9

x 10

Use the number from SizeX10 on the previous page.

Calculate the proportional change (with decimals).

Again, experiment to find a Size that comes close to 1 second.

Again, experiment to find a Size that comes close to 1 second.

Page 5: Name: Exercise: Big-O and Sorting - Algonquin Collegeelearning.algonquincollege.com/coursemat/woollar/... · Big-O Analysis: Merge Sort / Java Sort For Merge Sort and Java Sort, compare

Big-O Analysis: Merge Sort / Java Sort

For Merge Sort and Java Sort, compare the Proportional

Increase in the size with the Proportional Change in time.

Based on this relationship, now estimate the time required to

sort other data set sizes with the Java Sort. Fill in the first row

with your last experimental reading. Fill in the subsequent

rows based on your Big-O calculations.

When calculating these estimates, assume that you have a

computer with unlimited real RAM. Of course, that’s not

really the case. On the computer you’re currently using, and

with the bigger numbers shown below, performance would

degrade as the operating system shunts data to virtual memory

on disk.

Chart of Estimated Performance: Java Sort

Size Proportional Increase

Time to Complete

Proportional Change

100,000,000

500,000,000 x 5

1,000,000,000 x 2

5,000,000,000 x

10,000.000.000 x

50,000,000,000 x

100,000,000,000 x

How Bad is O(n2) at 100,000,000,000

You’ve estimated the time to complete a sort of

100,000,000,000 numbers with Java Sort. Now estimate the

time to complete the sort using Bubble Sort.

Evaluation for Credit

Show your work before the start of your lab period for a bonus

(up to 3 out of 2). Show it by the end of your lab period for

full marks (up to 2 out of 2). Show it later in the week for

partial marks (up to 1 out of 2).

What is the relationship?

Estimated Time to Complete:

To sort with a number this big, you’ll have to run SortTest.jar from the command line and set the Heap Space larger (4 GB here) with the following command to launch:

java -Xmx4g -jar sorttest.jar

Actual elapsed time.

All the rest are calculated estimates.