# Complexity of while loop Average,Best,worse case

int[]A;
int L=A.length-1;
int index=(L-1)/2;

int k=2*index+1;
while(k<n){
if(k+1<n){
if(A[k]<A[k+1]) k=k+1;}
if(A[index]<A[k]){
int t=A[index];
A[index]=A[k];
A[k]=t;
index=k;
k=2*index+1;
}else break;

• Are you asking us to do your homework for you? Because it looks like you're asking us to do your homework for you. – Turksarama Dec 16 '19 at 1:47
• What did you try? Where did you get stuck? We're happy to help you understand the concepts but just solving exercises for you is unlikely to achieve that. You might find this page helpful in improving your question. – D.W. Dec 16 '19 at 2:13

## 1 Answer

I think your code tries to do heap sorting. Im saying "tries" because youre missing some code.

If you want to know the complexity of the entire algorithm then here it is: Best case: n* log(n) Worst case: nlog(n) Average: nlog(n)

Where log(n) is the logarithm of n in base 2.

But if youre interested in only the complexity of YOUR while, then it is: Best case: log(n) Worst case: log(n) Average: log(n)

If you want to read more about heaps, try some youtube videos, the wikipedia link, or this link : https://www.geeksforgeeks.org/time-complexity-of-building-a-heap/

Please vote my answer if i helped you.