Docsity

Runtime Efficiency - Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Runtime Efficiency - Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Runtime Efficiency

Efficiency (Complexity)

  • The rate at which storage or time grows as a function of the problem size.
  • Two types of efficiency:
    • Time efficiency
    • Space efficiency
  • There are always tradeoffs between these two efficiencies. - Example: singly vs. doubly linked list

Time Efficiency Improvements

Possibilities (some better than others!)

  • Move code out of loops that does not belong there (just good programming!)
  • Remove any unnecessary I/O operations (I/O operations are expensive time-wise)
  • Code so that the compiled code is more efficient
  • Replace an inefficient algorithm (best solution)

Moral - Choose the most appropriate algorithm(s) BEFORE program implementation

Asymptotic Analysis (Runtime Analysis)

  • Independent of any specific hardware or software
  • Expresses the complexity of an algorithm in terms of its relationship to some known function
  • The efficiency can be expressed as ā€œproportional toā€ or ā€œon the order of some function
  • A common notation used is called Big-Oh:

T(n) = O(f(n))

Said: T of n is ā€œon the order of f(n)

Common Functions in Big-Oh (Most Efficient to Least Efficient)

  • O(1) or O(c)

Constant growth. The runtime does not grow at all as a function of n. It is a constant. Basically, it is any operation that does not depend on the value of n to do its job. Has the slowest growth pattern (none!).

Examples:

  • Accessing an element of an array containing n elements
  • Adding the first and last elements of an array containing n elements

Common Functions in Big-Oh (con’t)

  • O(lg(n))

Logarithmic growth. The runtime growth is proportional to the base 2 logarithm (lg) of n.

Example:

  • Binary search

Common Functions in Big-Oh (con’t)

  • O(n lg(n))

n log n growth. Any sorting algorithm that uses comparisons between elements is O(n lg n).

Examples

  • Merge sort
  • Quicksort

Common Functions in Big-Oh (con’t)

  • O(nk^ )

Polynomial growth. Runtime grows very rapidly.

Examples:

  • Bubble sort (O(n^2 ))
  • Selection (exchange) sort (O(n^2 ))
  • Insertion sort (O(n^2 ))

So, Which Algorithm Do I Choose?

  • What are my response time needs?
    • real-time
    • ā€œgo out to lunchā€ time
    • overnight time
  • What is the maximum size of n expected now and in the future? - If n will always be very small, pick the algorithm that is easiest to understand, debug, maintain, etc., and still meets your response time needs

So, Which Algorithm Do I Choose? (con’t)

  • How close to sorted are my values already?
    • The same algorithm can have different efficiencies depending on the order of the values (best case, average case, worst case)
    • Examples:
      • linear search
        • best case O(1) - value is first in list
        • average case O(1/2 n)
        • worst case O(n) - value is last in list
      • quicksort
        • best and average cases O(n lg n)
        • worst case O(n^2 ) - values are already in order