- #1
s3a
- 818
- 8
Homework Statement
What is the runtime complexity for the following (pseudo-code) algorithm?:
Code:
Algorithm MyAlgorithm(A, n)
Input: Array of integer containing n elements
Output: Possibly modified Array A
done ← true
j ← 0
while j ≤ n - 2 do
if A[j] > A[j + 1] then
swap(A[j], A[j + 1])
done ← false
j ← j + 1
end while
j ← n - 1
while j ≥ 1 do
if A[j] < A[j - 1] then
swap(A[j - 1], A[j])
done ← false
j ← j - 1
end while
if ¬ done
MyAlgorithm(A, n)
else
return A
Homework Equations
Each primitive instruction is assumed to have the same constant amount of running time.
The Attempt at a Solution
Each line of the algorithm that's not "MyAlgorithm(A, n)" (without the quotes) has a constant run time, and each while loop is run (n-1) times.
I'm not sure about the run time of the line "MyAlgorithm(A, n)," other than the (recursive) call itself having a constant runtime, in addition to the runtime of the method called.
If the aforementioned line weren't a recursive call, I think I would understand the problem 100%.
Could someone please help me understand how to go about determining the runtime of that line, following which I should be able to determine the runtime of the algorithm itself?
Any input would be GREATLY appreciated!