Sunteți pe pagina 1din 2

The Efficiency of Linked Lists

Insertion and deletion at the beginning of a linked list are very fast. They involve changing only one or two pointers, which takes O(1) time. Finding or deleting a specified item requires searching through, on the average, half the items in the list. This requires O(N) comparisons. An array is also O(N) for these operations, but the linked list is nevertheless faster because nothing needs to be moved when an item is inserted or removed. The increased efficiency can be significant, especially if a copy takes much longer than a comparison. Of course, another important advantage of linked lists over arrays is that the linked list uses exactly as much memory as it needs, and can expand to fill all available memory. The size of an array is fixed when its created; this usually leads to inefficiency because the array is too large, or to running out of room because the array is too small. Vectors, which are expandable arrays, might solve this problem to some extent, but they usually expand in fixed-sized increments (such as doubling the size of the array whenever its about to overflow). This use of memory is still not as efficient as a linked list. Linear search O(N) Binary search O(log N) Insertion in unordered array O(1) Insertion in ordered array O(N) Deletion in unordered array O(N) Deletion in ordered array O(N)

W hy Not Use Arrays for Everything?


They seem to get the job done, so why not use arrays for all data storage? We've already seen some of their disadvantages. In an unordered array you can insert items quickly, in O(1) time, but searching takes slow O(N) time. In an ordered array you can search quickly, in O(logN) time, but insertion takes O(N) time. For both kinds of arrays, deletion takes O(N) time, because half the items (on the average) must be moved to fill in the hole. It would be nice if there were data structures that could do everythinginsertion, deletion, and searchingquickly, ideally in O(1) time, but if not that, then in O(logN) time. In the chapters ahead, we'll see how closely this ideal can be approached, and the price that must be paid in complexity. Another problem with arrays is that their size is fixed when the array is first created with new. Usually when the program first starts, you don't know exactly how many items will be placed in the array later on, so you guess how big it should be. If your guess is too large, you'll waste memory by having cells in the array that are never filled. If your guess is too small, you'll overflow the array, causing at best a message to the program's user, and at worst a program crash. Other data structures are more flexible and can expand to hold the number of items inserted in them. The linked list, discussed in Chapter 5, "Linked Lists," is such a structure. We should mention that Java includes a class called Vector that acts much like an array but is expandable. This added capability comes at the expense of some loss of efficiency.

TABLE 24.1 GENERAL-PURPOSE DATA STORAGE STRUCTURES


Data Structure Search Insertion Deletion Traversal Array O(N) O(1) O(N) Ordered array O(log N) O(N) O(N) O(N) Linked list O(N) O(1) O(N) Ordered linked list O(N) O(N) O(N) O(N) Binary tree (average) O(log N) O(log N) O(log N) O(N) Binary tree (worst case) O(N) O(N) O(N) O(N) Balanced tree (average O(log N) O(log N) O(log N) O(N) and worst case) Hash table O(1) O(1) O(1)

S-ar putea să vă placă și