Analysis of algorithms
This presentation is the property of its rightful owner.
Sponsored Links
1 / 29

Analysis of algorithms PowerPoint PPT Presentation


  • 66 Views
  • Uploaded on
  • Presentation posted in: General

Analysis of algorithms. Best case: easiest Worst case Average case: hardest . input: 7,5,1,4,3 7, 5 ,1,4,3 5,7, 1 ,4,3 1,5,7, 4 ,3 1,4,5,7, 3 1,3,4,5,7. Straight insertion sort. M: # of data movements in straight insertion sort 1 5 7 4 3

Download Presentation

Analysis of algorithms

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Analysis of algorithms

Analysis of algorithms

  • Best case: easiest

  • Worst case

  • Average case: hardest


Straight insertion sort

input: 7,5,1,4,3

7,5,1,4,3

5,7,1,4,3

1,5,7,4,3

1,4,5,7,3

1,3,4,5,7

Straight insertion sort


Analysis of of movements

M: # of data movements in straight insertion sort

1 5 7 4 3

temporary

d3=2

Analysis of # of movements


Analysis by inversion table

best case: already sorted

di = 0 for 1  i  n

M = 2(n  1) = (n)

worst case: reversely sorted

d1 = n  1

d2 = n  2

:

di = n  i

dn = 0

Analysis by inversion table


Analysis of algorithms

  • average case:

    xj is being inserted into the sorted sequence

    x1 x2 ... x j-1

  • the probability that xj is the largest: 1/j

    • takes 2 data movements

  • the probability that xj is the second largest : 1/j

    • takes 3 data movements

      :

  • # of movements for inserting xj:

1 4 7 5


Straight selection sort

Straight selection sort

  • 7 5 1 4 3

    1 5 7 4 3

    1 3 7 4 5

    1 3 4 7 5

    1 3 4 5 7

  • Only consider # of changes in the flag which is used for selecting the smallest number in each iteration.

    • best case: (n2)

    • worst case: (n2)

    • average case: (n2)


Quick sort

Quick sort

  • Recursively apply the same procedure.


Analysis of algorithms

  • Best case: (n log n)

    • A list is split into two sublists with almost equal size.

  • log n rounds are needed.

  • In each round, n comparisons (ignoring the element used to split) are required.


Analysis of algorithms

Worst case: (n2)

In each round, the number used to split is either the smallest or the largest.


Analysis of algorithms

Average case: (n log n)


Lower bound

Lower bound

  • Def: A lower bound of aproblem is the least time complexity required for any algorithm which can be used to solve this problem.

    ☆worst case lower bound

    ☆average case lower bound

  • The lower bound for a problem is not unique.

    • e.g. (1), (n), (n log n) are all lower bounds for sorting.

    • ((1), (n) are trivial)


Analysis of algorithms

  • At present, if the highest lower bound of a problem is (n log n) and the time complexity of the best algorithm is O(n2).

    • We may try to find a higher lower bound.

    • We may try to find a better algorithm.

    • Both of the lower bound and the algorithm may be improved.

  • If the present lower bound is (n log n) and there is an algorithm with time complexity O(n log n), then the algorithm is optimal.


Analysis of algorithms

The worst case lower bound of sorting

6 permutations for 3 data elements

a1a2a3

123

132

213

231

312

321


Straight insertion sort1

Straight insertion sort:

  • input data: (2, 3, 1)

    (1) a1:a2

    (2) a2:a3, a2a3

    (3) a1:a2, a1a2

  • input data: (2, 1, 3)

    (1)a1:a2, a1a2

    (2)a2:a3


Decision tree for straight insertion sort

Decision tree for straight insertion sort


Lower bound of sorting

Lower bound of sorting

  • To find the lower bound, we have to find the smallest depth of a binary tree.

  • n! distinct permutations

    n! leaf nodes in the binary decision tree.

  • balanced tree has the smallest depth:

    log(n!) = (n log n)

    lower bound for sorting: (n log n)


Method 1

Method 1:


Method 2

Method 2:

  • Stirling approximation:

    • n! 

    • log n!  log  n log n (n log n)


Heapsort an optimal sorting algorithm

Heapsort—An optimal sorting algorithm

  • A heap : parent  son


Analysis of algorithms

  • output the maximum and restore:

  • Heapsort: construction

    output


Phase 1 construction

input data: 4, 37, 26, 15, 48

restore the subtree rooted at A(2):

restore the tree rooted at A(1):

Phase 1: construction


Analysis of algorithms

Phase 2: output


Implementation

Implementation

  • using a linear array

    not a binary tree.

    • The sons of A(h) are A(2h) and A(2h+1).

  • time complexity: O(n log n)


Time complexity phase 1 construction

Time complexityPhase 1: construction


Time complexity phase 2 output

Time complexity Phase 2: output


Quicksort heapsort

Quicksort & Heapsort

  • Quicksort is optimal in the average case.

    ((n log n) in average )

  • (i)worst case time complexity of heapsort is

    (n log n)

    (ii)average case lower bound: (n log n)

    • average case time complexity of heapsort is (n log n)

    • Heapsort is optimal in the average case.


  • Login