Search data structure: Difference between revisions

Content deleted Content added
top: lx
WikiCleanerBot (talk | contribs)
m v2.05b - Bot T5 CW#16 - Fix errors for CW project (Unicode control characters)
 
(2 intermediate revisions by 2 users not shown)
Line 40:
|-
| Unsorted [[Array data structure|array]]
| [[Constant time|''O''(1)]]<br /><sup>(see&nbsp;note)</sup>
| ''O''(1)<br /><sup>(see&nbsp;note)</sup>
| N/A
| ''O''(1)
Line 81:
| Unsorted [[linked list]]
| ''O''(1)
| ''O''(1)<ref name="listdelete">{{cite book |title=[[Introduction to Algorithms]] |publisher=The College of Information Sciences and Technology at Penn State|isbn=9780262530910978-0-262-53091-0 |authorsauthor=Thomas H. Cormen, |author2=Charles E. Leiserson, |author3=Ronald L. Rivest |year=1990 |quote=LIST-DELETE runs in ''O''(1) time, but if to delete an element with a given key, Θ(n) time is required in the worst case because we must first call LIST-SEARCH.}}</ref>
| N/A
| ''O''(''n'')
Line 125:
| N/A
| ''O''(''n'')
| ''O''(1) for a ''min-heap''<br />''O''(''n'') for a ''max-heap''<ref name="heap">{{cite book |title=Introduction to Algorithms|publisher=The College of Information Sciences and Technology at Penn State|isbn=9780262530910978-0-262-53091-0 |authorsauthor=Thomas H. Cormen, |author2=Charles E. Leiserson, |author3=Ronald L. Rivest |year=1990 |quote=There are two kinds of binary heaps: max-heaps and min-heaps. In both kinds, the values in the nodes satisfy a '''heap property'''... the largest element in a max-heap is stored at the root... The smallest element in a min-heap is at the root... The operation HEAP-MAXIMUM returns the maximum heap element in Θ(1) time by simply returning the value ''A''[1] in the heap.}}</ref>
| ''O''(1) for a ''max-heap''<br />''O''(''n'') for a ''min-heap''<ref name="heap" />
| ''O''(''n'')
|-
Line 210:
|}
 
''Note'': Insert on an unsorted array is sometimes quoted as being ''O''(''n'') due to the assumption that the element to be inserted must be inserted at one particular ___location of the array, which would require shifting all the subsequent elements by one position. However, in a classic array, the array is used to store arbitrary unsorted elements, and hence the exact position of any given element is of no consequence, and insert is carried out by increasing the array size by 1 and storing the element at the end of the array, which is a ''O''(1) operation.<ref name="games">{{cite book|isbn=9781584506638978-1-58450-663-8|title=Data Structures and Algorithms for Game Developers|author=Allen Sherrod|publisher=Cengage Learning|year=2007|quote=The insertion of an item into an unordered array does not depend on anything other than placing the new item at the end of the list. This gives the insertion into an unordered array of ''O''(1).}}</ref><ref>{{cite book |title=[[Introduction to Algorithms]]|publisher=The College of Information Sciences and Technology at Penn State|isbn=9780262530910978-0-262-53091-0 |authorsauthor=Thomas H. Cormen, |author2=Charles E. Leiserson, |author3=Ronald L. Rivest |year=1990 }}</ref> Likewise, the deletion operation is sometimes quoted as being ''O''(''n'') due to the assumption that subsequent elements must be shifted, but in a classic unsorted array the order is unimportant (though elements are implicitly ordered by insert-time), so deletion can be carried out by swapping the element to be deleted with the last element in the array and then decrementing the array size by 1, which is a ''O''(1) operation.<ref>{{cite web|url=https://stackoverflow.com/questions/9358481/algorithm-the-time-complexity-of-deletion-in-a-unsorted-array#9358634|title=Algorithm - the time complexity of deletion in a unsorted array|quote=Finding the element with a given value is linear. Since the array isn't sorted anyway, you can do the deletion itself in constant time. First swap the element you want to delete to the end of the array, then reduce the array size by one element.}}</ref>
 
This table is only an approximate summary; for each data structure there are special situations and variants that may lead to different costs. Also two or more data structures can be combined to obtain lower costs.