Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation."— Presentation transcript:

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

2 5.6.1 Big O Notation

3 Is efficiency the same as speed?

4 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

5 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.

6 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)

7 7 Memory More cache makes the processor faster. More RAM means ‣ less time paging data in and out of virtual memory on the hard drive, and ‣ more data/programs running simultaneously

8 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

9 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

10 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)

11 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)

12 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.

13 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

14 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(n 2 ) Time requirements are proportional to the square of the size of the list (double the number of elements, quadruple the time taken)

15 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 center may deteriorate to O(n 2 ).

16 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).

17 Some old questions State the efficiency of a quicksort algorithm in BigO notation. [1 mark] (May 2008 HL P1 q16) In relation to accessing data stored in a file or an array, state the BigO efficiency of ‣ (a) retrieving a specific data record from a sequential access file. [1 mark] ‣ (b) retrieving a specific data record from a direct access file. [1 mark] ‣ (c) locating a specific data item in an array if the index is known. [1 mark] ‣ (d) locating a specific data item in an array if the index is not known. [1 mark] (May 2009 HL P1 q12) 17


Download ppt "1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation."

Similar presentations


Ads by Google