Uncertain my big analysis o

int RiskSort(int* PlayerA, int* PlayerB,int Length){
    int i,j;
    int Losses = 0;
    for(i=0;i<Length-Losses;i++){
         printf("%d,%d\n",PlayerA[i],PlayerB[i]);
        if(PlayerB[i]<PlayerA[i]){
                for(j=i;j<((Length-1)-Losses);j++){
                    Swap(&PlayerB[j],&PlayerB[j+1]);
                }
                i--;
                Losses++;
        }

    }
return Losses;

}

I just wrote this and I get O (n log n) as my answer. This is homework, but most of O is just my way of learning.

Thanks again

Edit: I get N from the first for the loop and the number of passes N-1-X to if, and I'm not sure how to designate this, since it limits the number of passes that I called log log (probably inaccurate, but I could not find a manual that did not look at the code and did not select online)

Edit 2: just try to make this feature more efficient

int RiskSortB(int* PlayerA, int* PlayerB,int Length){
int i,j;
int Losses = 0;
for(i=0;i<Length-Losses;i++){
j=i+1;
if(PlayerB[i]<PlayerA[i])
    Losses++;

while(PlayerB[i]<PlayerA[i]&&j<Length){
    if(PlayerB[j]>=PlayerA[i]){
        Swap(&PlayerB[i],&PlayerB[j]);
        if(j!=(Length-Losses))
            Swap(&PlayerB[j],&PlayerB[Length-Losses]);
    }
    j++;
}


}

return Losses;
}

So, since the amount of time that the maximum Swap is called for the for loop is 2, it means it's O (2N), but the constants don't matter, so is its O (N) right?

+4
1

, PlayerB "". -1. -2. -3. Etc.

? 1 + 2 +... + (n-1). , : 1..n = n * (n + 1)/2 = (n 2 + n)/2. ( 2).

( (1..n) (1.. (n-1)) big-O.)

+4

Source: https://habr.com/ru/post/1614018/


All Articles