What does “O(1) access time” mean?

后端 未结 16 2114
栀梦
栀梦 2020-11-28 18:21

I have seen this term \"O(1) access time\" used to mean \"quickly\" but I don\'t understand what it means. The other term that I see with it in the same context is \"O(n) ac

相关标签:
16条回答
  • 2020-11-28 18:51

    You're going to want to read up on Order of complexity.

    http://en.wikipedia.org/wiki/Big_O_notation

    In short, O(1) means that it takes a constant time, like 14 nanoseconds, or three minutes no matter the amount of data in the set.

    O(n) means it takes an amount of time linear with the size of the set, so a set twice the size will take twice the time. You probably don't want to put a million objects into one of these.

    0 讨论(0)
  • 2020-11-28 18:51

    O(1) always execute in the same time regardless of dataset n. An example of O(1) would be an ArrayList accessing its element with index.

    O(n) also known as Linear Order, the performance will grow linearly and in direct proportion to the size of the input data. An example of O(n) would be an ArrayList insertion and deletion at random position. As each subsequent insertion/deletion at random position will cause the elements in the ArrayList to shift left right of its internal array in order to maintain its linear structure, not to mention about the creation of a new arrays and the copying of elements from the old to new array which takes up expensive processing time hence, detriment the performance.

    0 讨论(0)
  • 2020-11-28 18:53

    It means that the access takes constant time i.e. does not depend on the size of the dataset. O(n) means that the access will depend on the size of the dataset linearly.

    The O is also known as big-O.

    0 讨论(0)
  • 2020-11-28 18:56

    Every answer currently responding to this question tells you that the O(1) means constant time (whatever it happens to measuring; could be runtime, number of operations, etc.). This is not accurate.

    To say that runtime is O(1) means that there is a constant c such that the runtime is bounded above by c, independent of the input. For example, returning the first element of an array of n integers is O(1):

    int firstElement(int *a, int n) {
        return a[0];
    }
    

    But this function is O(1) too:

    int identity(int i) {
        if(i == 0) {
            sleep(60 * 60 * 24 * 365);
        }
        return i;
    }
    

    The runtime here is bounded above by 1 year, but most of the time the runtime is on the order of nanoseconds.

    To say that runtime is O(n) means that there is a constant c such that the runtime is bounded above by c * n, where n measures the size of the input. For example, finding the number of occurrences of a particular integer in an unsorted array of n integers by the following algorithm is O(n):

    int count(int *a, int n, int item) {
        int c = 0;
        for(int i = 0; i < n; i++) {
            if(a[i] == item) c++;
        }
        return c;
    }
    

    This is because we have to iterate through the array inspecting each element one at a time.

    0 讨论(0)
提交回复
热议问题