Example of O(n!)?

后端 未结 16 2404
渐次进展
渐次进展 2020-11-30 22:20

What is an example (in code) of a O(n!) function? It should take appropriate number of operations to run in reference to n; that is, I\'m asking a

相关标签:
16条回答
  • 2020-11-30 22:58

    printf("Hello World");

    Yes, this is O(n!). If you think it is not, I suggest you read the definition of BigOh.

    I only added this answer because of the annoying habit people have to always use BigOh irrespective of what they actually mean.

    For instance, I am pretty sure the question intended to ask Theta(n!), at least cn! steps and no more than Cn! steps for some constants c, C > 0, but chose to use O(n!) instead.

    Another instance: Quicksort is O(n^2) in the worst case, while technically correct (Even heapsort is O(n^2) in the worst case!), what they actually mean is Quicksort is Omega(n^2) in the worst case.

    0 讨论(0)
  • 2020-11-30 22:59

    Any algorithm that calculates all permutation of a given array is O(N!).

    0 讨论(0)
  • 2020-11-30 23:04

    In Wikipedia

    Solving the traveling salesman problem via brute-force search; finding the determinant with expansion by minors.

    http://en.wikipedia.org/wiki/Big_O_notation#Orders_of_common_functions

    0 讨论(0)
  • 2020-11-30 23:07

    the simplest example :)

    pseudocode:

    input N
    calculate N! and store the value in a vaiable NFac - this operation is o(N)
    loop from 1 to NFac and output the letter 'z' - this is O(N!)
    

    there you go :)

    As a real example - what about generating all the permutations of a set of items?

    0 讨论(0)
提交回复
热议问题