Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Big O effeciency for multiple variables

I'm trying to rate the efficiency of a function where the input is an array of strings. The algorithm always iterates through every item in this array. This strings contained in this array are of variable length. In this initial for loop, a character replace function is called on each string. I believe the replace function on its own would be O(n) where n is the length of the string.

So I'm confused how to evaluate big o efficiency here. If n is the size of the array, I know it will at least be O(n). But with variable string lengths, how would you rate the overall efficiency with the string replacement? Would you say n is the size of the array and use other variables to represent the different sizes of each string?

like image 844
DannyLeavitt Avatar asked Dec 27 '10 16:12

DannyLeavitt


2 Answers

Personally, I would express efficiency through size of input data (as opposed to the length of array). So, if input is t bytes, running time will be O(t). And t here is also a common length of all strings.

like image 61
Nikita Rybak Avatar answered Sep 30 '22 15:09

Nikita Rybak


I see two ways to say this (out of many possible).

The first is to say it is O(N) where N is the total number of characters.

The other you could say is O(N*M) where N is the number of strings and M is the average number of characters per string. Note that this is actually the same as my above answer. You could say M = k/N, so you get O(N*k/N) or O(k) where k is the total characters in all the strings.

like image 36
Donald Miner Avatar answered Sep 30 '22 13:09

Donald Miner