Time complexity of common data structures

Source: Internet
Author: User
Time complexity of common data structures

The complexity of programs can be divided into time complexity and space complexity. We can literally see their meanings. Next we mainly look at the time complexity of a set, which basically includes. net, huh, huh!

Data Structure Add Find Delete Getbyindex
Array (T [])

O (N)

O (N)

O (N)

O (1)

Linked List (Revoke list <t>)

O (1)

O (N)

O (N)

O (N)

Resizable array list (List <t>)

O (1)

O (N)

O (N)

O (1)

Stack (Stack <t>)

O (1)

-

O (1)

-

Queue (Queue <t>)

O (1)

-

O (1)

-

Hash table (Dictionary <K, T>)

O (1)

O (1)

O (1)

-

Tree-based dictionary

(Sorteddictionary <K, T>)

O (log n)

O (log n)

O (log n)

-

Hash Table based set (Hashset <t>)

O (1)

O (1)

O (1)

-

Tree based set (Sortedset <t>)

O (log n)

O (log n)

O (log n)

-

How to select a Data Structure

Array (T [])

  • When the number of elements is fixed and the submark is required.

Linked List (Revoke list <t>)

  • When an element needs to be added at both ends of the list. Otherwise, use list <t>.

Resizable array list (List <t>)

  • When the number of elements is not fixed and you need to use the lower mark.

Stack (Stack <t>)

  • When you need to implement LIFO (last in first out.

Queue (Queue <t>)

  • When first in first out is required.

Hash table (Dictionary <K, T>)

  • When you need to use key-value pairs to quickly add and search, and the elements do not have a specific order.

Tree-based dictionary (Sorteddictionary <K, T>)

  • When you need to use key-value pairs to quickly add and search, and the elements are sorted by key.

Hash Table based set (Hashset <t>)

  • When you need to save a unique set of values, and the elements do not have a specific order.

Tree based set (Sortedset <t>)

  • When you need to save a set of unique values and the elements need to be sorted.
Thanks

Original articleDennis GaoI posted my blog on the blog site. I just added it to my favorites.

This article from: http://www.cnblogs.com/gaochundong/p/3813252.html

References:

  • Test the data structure-Part 1: Data Structure Overview
  • Test the data structure-Part 2: queue, stack, and hash table

Time complexity of common data structures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.