-5

What is the Big O of deque data structure in c++? And how it really implemented!

I didn't Found the answer anywhere.

avocadoLambda
  • 1,332
  • 7
  • 16
  • 33
  • 7
    big O of what? Insertion? Sorting? removal? A data structure has no big O, operations on a data structure have – 463035818_is_not_an_ai Jun 25 '20 at 06:50
  • for example insertion: https://en.cppreference.com/w/cpp/container/deque/insert – 463035818_is_not_an_ai Jun 25 '20 at 06:52
  • Deque is typically implemented as a dynamic array of pointers to dynamically allocated buckets of objects. If you search "c++ deque implementation", you'll find several questions about it. – Evg Jun 25 '20 at 06:58
  • The 2 answers pretty much answered your question already. However, if you want to check on some studies: [An In-Depth Study of the STL Deque Container](https://www.codeproject.com/Articles/5425/An-In-Depth-Study-of-the-STL-Deque-Container) – Ranoiaetep Jun 25 '20 at 07:23

2 Answers2

3

From the CPPreference page on std::deque:

The complexity (efficiency) of common operations on deques is as follows:

Random access - constant O(1)
Insertion or removal of elements at the end or beginning - constant O(1)
Insertion or removal of elements - linear O(n)

While I'm at it:

As opposed to std::vector, the elements of a deque are not stored contiguously: typical implementations use a sequence of individually allocated fixed-size arrays, with additional bookkeeping, which means indexed access to deque must perform two pointer dereferences, compared to vector's indexed access which performs only one.

If you want to read the nitty-gritty details, suggest you open your <deque> header file and start reading. Be aware: STL code likes to use underscores and single-letter variable names a lot.

Botje
  • 26,269
  • 3
  • 31
  • 41
2

Referring to the document https://en.cppreference.com/w/cpp/container/deque

The complexity (efficiency) of common operations on deques is as follows:

Random access - constant O(1)

Insertion or removal of elements at the end or beginning - constant O(1)

Insertion or removal of elements - linear O(n)

yue you
  • 2,206
  • 1
  • 12
  • 29