Docstoc

Arrays

Document Sample
Arrays Powered By Docstoc
					Evaluating Sorting
   Algorithms
    Comparing Algorithms
• There are a variety of ways to
  compare an algorithm:
   1.   Algorithm complexity
   2.   Algorithm structure
   3.   Computational complexity
   4.   Memory usage
   5.   Array stability
    Algorithm Complexity
• This is a comparison of the overall
  complexity of the code required to
  write the array.
• In this case, all three sorts have
  relatively simple algorithms
     Algorithm Structure
• This is a comparison of the general
  style or type of the algorithm
• Can be:
   – Swap (switching paired items)
   – Merge (divides array into sorted and
     unsorted parts)
   – Tree (places data in a binary tree)
   – Other (generally more complex)
     Algorithm Structure
What are the structures for the arrays
covered in class?
    – Bubble sort
        • swap based
    – Selection sort
        • swap based
    – Insert sort
        • merge based
  Computational Complexity
• This comparison is based on the
  processing time of the algorithm
  – Usually based on the number of
    comparisons made, since it can be difficult
    to compute the number of swaps made
• Processing time can change as a result
  of the complexity or amount of disarray
  of the array to be sorted
  – Results in analyses of best, worst and
    average case scenarios
  Computational Complexity
• Generally, programmers want to know
  the efficiency of their programs when
  there is a large amount of data (n)
• Evaluated using O Notation:
   – A mathematical language for evaluating
     the running-time of algorithms
   – Gives an approximated rate of growth for
     a function
  Computational Complexity
Bubble Sort:
• For an array of 10 elements, the
  number of comparisons is as follows:
   9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0 = 45
• In general, if there are N elements,
  there are N-1 comparisons on the first
  pass, N-2 on the second, etc.:
 (N-1) + (N-2) + (N -3) + … + 2 + 1 + 0 = N * (N-1)/2
  Computational Complexity
• For the bubble sort,
           N * (N-1)/2

 evaluates to O(N2) comparisons.

• There are fewer swaps, because any two
  elements are only swapped if required
• For random data, a swap is necessary 50%
  of the time, so there will be half as many
  swaps
 Computational Complexity
Selection Sort:
• The selection sort has the same
  number of comparisons as the bubble
  sort
          N* (N-1)/2 or O(N2)
• The number of swaps performed is
  much lower (only 10 swaps for 10
  elements)
  – Results in much quicker sorting for a
    smaller N
  Computational Complexity
Insert Sort:
• Works in the reverse order of the other two
  algorithms:
  – First term has one comparison, second term has
    two comparisons, up to the last term having N-1
    comparisons
             N*(N-1)/2 or O(N2)
• Although, on average only half the number
  of comparisons are performed for each pass
  before the term is found, so this algorithm
  becomes twice as fast as the bubble sort
  Computational Complexity
• Overall, all three algorithms have O(N2)

  Which algorithm is the best for an array that
  only has a few elements out of order?
             Insert Sort!
  (The inner while loop is almost never true, so only
  the outer loop is executed)
          Memory Usage
• Used to compare the amount of
  memory required to sort the array (not
  including the size of the actual array)
• All three algorithms require very little
  extra memory
  – Only 1 variable used for temporary
    storage during data swapping
               Stability
• Used to see if data remains in the
  same order when sorted for multiple
  attributes
  – ex. If you sort a list of names already
    arranged alphabetically by postal code, do
    the names with the same postal code
    remain sorted alphabetically?


• All three of these algorithms are stable