160 likes | 261 Views
5. Abstract Data Structures & Algorithms. 5.6 Algorithm Evaluation. 5.6.1 Big O Notation. Is efficiency the same as speed?. Efficiency. The efficiency of an algorithm depends on: the speed of the processor available memory how well the algorithm is written
E N D
5. Abstract Data Structures & Algorithms • 5.6 Algorithm Evaluation
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
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.
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)
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
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
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
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)
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)
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.
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
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)
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).
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).