algorithm - Running time of counting sort -


In my notes, I am trying to understand the time of sort calculation, the size of array A Assuming the N is, and the Kashmir number is in each number,

  for the count-ordering (a, k) {i (i = 1; i & lt; = k; i + +) // Count the counters 0 times [i] = 0; For (J = 1; J & lt; = Length [A]; J ++) // How often fixed [A] [J]] ++; // the serial in serial number / input / // appears in M ​​= 1; Consider each number in the range for (I = 1; i & lt; = k; i ++) // (j = 1; j and lt; = time [ii]; J ++) {// that Make the number in A [M] = I; // M ++ as the output multiple times; // It is in the input}}   

Let i repeat the internal loop for every i. When we look at the nested for the loops in the lower part of the code, note that, the internal loop runs every time, we are keeping a new number in the right place in the output array. Therefore: sum t i (i = 1 to k) = n

I do not understand that this amount is equal to n. The external loop repeats the time of Kashmir, and the internal loop can mostly be repeated in the N bar, so it should be O (nk). Can anyone explain? Thanks

As you can see for loop

  m = 1; Consider each number in the range for (I = 1; i & lt; = k; i ++) // (j = 1; j and lt; = time [ii]; J ++) {// that Make the number in A [M] = I; // M ++ as the output multiple times; // it is in the input}   

How often its complexity will be equivalent to performing the body of the inner circle. When i = 1 is executed, it is executed by bar [1] bar, when i = 2 => Bar [2] Bar, and therefore, when i = k , bar [k] 1] + times [2] + ... + times [k] is executed, and that amount equals n , counting each element once goes.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -