-4

I am having trouble calculating the complexity of this problem:

REVERSE3(A): // Reverse the order of elements in an array
// P is an array; assume generating next permutation takes 1 step.


    for every possible permutation P of A:
      for index i = 1 to N:
        if P[i] is not equal to A[N-i+1]:
          continue to the next permutation
          // All elements matched in proper places
    return P

I think some of my misunderstandings arise from the first for-loop. What does it even mean? How would you calculate the complexity for this?

Any help would be appreciated. Thank you.

FrustratedWithFormsDesigner
  • 46,235
  • 7
  • 128
  • 176

1 Answers1

0

for every possible permutation P of A:

This is apparently a sort of a joke problem, as this would be almost the most inefficient way possible to sort an array. They want you to step through every possible ordering of the array looking for the one that's sorted correctly.

How many ways can you order an array with M elements? Well, you have M choices for the first position, M - 1 choices for the 2nd position, etc. Does that remind you of a common mathematical function?