1 5. abstract data structures & algorithms 5.6 algorithm evaluation

16
1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

Upload: cory-bridges

Post on 21-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

1

5. Abstract Data Structures & Algorithms5.6 Algorithm Evaluation

Page 2: 1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

5.6.1 Big O Notation

Page 3: 1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

Is efficiency the same as speed?

Page 4: 1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

4

Efficiency

•The efficiency of an algorithm depends on:

‣ the speed of the processor

‣ available memory

‣how well the algorithm is written

•Only the latter is under the programmer's control

Page 5: 1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

5

Speed vs memory

•Usually a compromise between these two.

•e.g. a linked list (dynamic) requires less memory that an array (static), but searching an array is faster if they are the same size.

Page 6: 1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

6

Processor speed

•This is not usually measured in time, but in number of operations per second.

•Often floating point operations per second (flops), or gigaflops, teraflops, etc.

•Obviously depends on the number of elements you are processing (n)

Page 7: 1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

7

Memory

•More cache makes the processor faster.

•More IAS means

‣ less time paging data in and out of virtual memory on the hard drive, and

‣more data/programs running simultaneously

Page 8: 1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

8

Algorithm speed•Difficult to predict - depends on

circumstances

•E.g. a linear search of an array:

‣ best case scenario: 1 (it's the first element!)

‣worst case scenario: n (it's not there and you've wasted your time looking at n elements!)

‣ average case scenario: n/2

Page 9: 1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

9

BigO notation

•BigO notation assumes worst case

•It is defined in terms of n, the number of elements being operated on.

•You should know the BigO efficiency of a single operation, linear search (array or linked list), binary search, bubble and selection sort, quicksort

Page 10: 1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

Single operation

•Only need one operation no matter what the arguments

• E.g. reading a value from a random access file.

• BigO efficiency for a single operation is O(1)

Page 11: 1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

Linear search

• The time to search an array is proportional to the size of the array

• BigO efficiency for a linear search is O(n)

Page 12: 1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

Binary search

• Binary search is more complex, but the splitting in half each time is efficient on a logarithmic scale

• Big O order is O(log n)

• A binary search is much more efficient than linear with larger numbers of elements.

Page 13: 1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

Quicksort

•Quicksort is the binary equivalent for sorting

• Big O order is O(n log n)

•More efficient than bubble or selection sort with larger numbers of elements

Page 14: 1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

Bubble sort

•With a bubble sort and a selection sort, you have to compare each element with every other one, so the BigO order is O(n2)

• Time requirements are proportional to the square of the size of the list (double the number of elements, quadruple the time taken)

Page 15: 1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

Sorting

• In some circumstances, the efficiency of an algorithm may depend on the distribution of the data

• E.g. a quicksort where the pivot is chosen too far away from the centre may deteriorate to O(n2).

Page 16: 1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation

16

Other operations

•An input or output is O(1) (single operation)

•Sequential access of a data file is O(n) (it’s a linear search)

•Direct access of a data file is O(1) (single operation)

•Finding a specified element of an array is O(1).