Big-O Algorithms Time Complexity

Ace your homework & exams now with Quizwiz!

Find the minimum in a min heap

O(1)

Inserting at end of double-ended linked list

O(1)

Insertion /Deletion at beginning of linked list

O(1)

Priority Queue/Heap - find min/max

O(1)

Push an element on a stack

O(1)

Remove the first element of a linked list

O(1)

Removing from a hash table (no collisions)

O(1)

hash table average deletion hash table worst deletion

O(1) O(n)

hash table average insertion hash table worst insertion

O(1) O(n)

Inserting into an external chaining hash table where there are no more than k collisions at each index

O(k)

Getting the k smallest elements from a min-heap

O(klogn)

sorted array - find by value

O(log n)

AVL tree worst space complexity

O(n)

Find a given value in a BST

O(n)

Getting the k smallest elements from a BST

O(n)

Getting the values of a BST in a sorted order

O(n)

add elements to the end of a linked list (no tail)

O(n)

array - Delete element front or middle

O(n)

array - Delete min/max or by value

O(n)

hash table - delete min/max

O(n)

hash table - find min/max

O(n)

hash table worst space complexity

O(n)

insertion/removal of sorted linked list

O(n)

linked list w/ pointer - delete middle or end

O(n)

linked list w/ pointer - delete min/max or by value

O(n)

linked list w/ pointer - find min/max or by value

O(n)

linked list w/ pointer - find position middle (ANYWHERE BUT front or back)

O(n)

linked list worst space complexity

O(n)

singly-linked list worst space complexity

O(n)

sorted array - delete any element that is not the max

O(n)

sorted array - delete by value

O(n)

sorted array - insert

O(n)

sorted linked list - delete ANYTHING BUT min

O(n)

sorted linked list - delete by value

O(n)

Advantages - Easy to access a particular item - Insertion is fast when *unordered* - Searching is fast when *ordered* - Data locality in memory

array

Disadvantages: - Some algorithms require large number of shifting/moves - The size cannot be changed once created - If the size is too large... waste of memory - If the size is too small, it cannot be expanded dynamically

array

Advantages: - get and set take constant time

array list

Disadvantages: - inserting and deleting is expensive

array list

a tree in which every node has at most two children. Height - longest path from root to leaf (count edges).

binary tree

Advantages: - Traverse backward - More flexibility in removing, inserting objects- Deletion from the end of the list is O(1) - Similar to singly linked list and LinkedList has methods - addFirst, - removeFirst, - addLast, - remove last, - getFirst and - getLast to efficiently add, remove, and access the items at both ends of the list.

double linked list

Last In First Out (LIFO): Push and pop are fast. often used for balancing symbols, postfix expressions (takes O(n) to evaluate), and infix to postfix conversion.

stacks

single linked lists - Deleting: _____, if done at the head, _______ if anywhere else since we have to reach that position by traversing the linkedlist linearly.

O(1) O(n)

single linked lists: Inserting: ______ if done at the head, ______ if anywhere else since we have to reach that position by traversing the linkedlist linearly.

O(1) O(n)

min/max heap Find Max min/max heap Extract Max min/max heap Increase Key min/max heap Insert min/max heap Delete min/max heap Merge

O(1) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(m+n)

Calculate binary numbers using binary recursion

O(2^n)

Number of comparisons in linked list is _________ like for arrays but items do not need to be shifted/movies --> expected to be faster than using arrays

O(N)

double linked list - findKth(i) is

O(i)

double linked list - findKth(i) is ____ because it needs to traverse the list.

O(i)

sorted array - find by value is _____ because it uses binary search

O(log n)

- A binary search on a sorted list

O(logn)

AVL tree best case Big O for indexing, inserting, and deleting

O(logn)

AVL tree worst case Big O for indexing, inserting, and deleting

O(logn)

BST best case Big O for indexing, inserting, and deleting

O(logn)

Heap - delete min or max

O(logn)

Heap - insert

O(logn)

Priority Queue/Heap - delete or find min/max

O(logn)

Priority Queue/Heap - dequeue (remove)

O(logn)

Priority Queue/Heap - enqueue (insert)

O(logn)

Removing from a Heap

O(logn)

Removing from an AVL tree

O(logn)

checking if an AVL tree contains a given element

O(logn)

AVL search tree average access AVL search tree worst access

O(logn) O(logn)

AVL search tree average deletion AVL search tree worst deletion

O(logn) O(logn)

AVL search tree average insertion AVL search tree worst insertion

O(logn) O(logn)

AVL search tree average search AVL search tree worst search

O(logn) O(logn)

binary search tree average access binary search tree worst access

O(logn) O(n)

binary search tree average deletion binary search tree worst deletion

O(logn) O(n)

binary search tree average insertion binary search tree worst insertion

O(logn) O(n)

- Finding an item (search) in an unsorted list where the size of the list is N.

O(n)

Adding to the back of an ArrayList with a size variable

O(n)

BST worst case Big O for indexing, inserting, and deleting

O(n)

Determining if a given data is in a linkedlist

O(n)

Finding, deleting or inserting next to a specific link in lined list

O(n)

Heap - delete any element NOT the min or max

O(n)

Heap - search

O(n)

Heap - space usage

O(n)

Indexing in a linked list

O(n)

Inserting into a linear probing hash table where there are no more than 2 collisions at each index

O(n)

Priority Queue/Heap - lookup or delete

O(n)

Regrow an array to twice its previous size

O(n)

Remove the first element of an array

O(n)

Removing from a BST

O(n)

Removing from a hash table (there could be many collisions)

O(n)

array - Find min/max or by value

O(n)

array - Insert element front or middle

O(n)

array list - find worst case

O(n)

array list - insert (index, element)

O(n)

array list - insert (index, element) is _____ because values would have to shift to the right.

O(n)

array list - insert value worst case

O(n)

array list - insert value worst case is ____ because it has to create a new array with a bigger size and recopy all values into the new array.

O(n)

array list - remove is

O(n)

array space complexity worst

O(n)

binary search tree worst space complexity

O(n)

circular array - Insert element middle (ANYWHERE BUT the front or back)

O(n)

circular array - delete element middle (ANYWHERE BUT the front or back)

O(n)

circular array - delete min/max or by value

O(n)

circular array - find min/max or by value

O(n)

double linked list - delete middle (ANYWHERE BUT front or back)

O(n)

double linked list - delete min/max or by value

O(n)

double linked list - find middle (ANYWHERE BUT front or back position)

O(n)

double linked list - find min/max or value

O(n)

double linked list - find(x) is

O(n)

double linked list - find(x) is ______ because you have to traverse using an iterator.

O(n)

double linked list - insert middle (ANYWHERE BUT front or back)

O(n)

doubly-linked list worst space complexity

O(n)

linked list w/ pointer - insert middle (ANYWHERE BUT front or back)

O(n)

queue worst space complexity

O(n)

remove is _____ because it needs to find it and then shift elements accordingly.

O(n)

single linked list - delete min/max or by value

O(n)

single linked list - deleting last element is complicated and is

O(n)

single linked list - deleting middle or last position

O(n)

single linked list - find position middle or end

O(n)

single linked list - find(x) is

O(n)

single linked list - printList is

O(n)

single linked list - printList or find(x) is ____ because you have to traverse the next links using an iterator.

O(n)

singly linked list - find min/max or by value

O(n)

singly linked list - insert at middle or end position

O(n)

sorted linked list - find by value

O(n)

sorted linked list - find max or any other element NOT min

O(n)

sorted linked list - insert

O(n)

stack worst space complexity

O(n)

worst-case time complexity for searching a fixed-size array-based bag ADT for a particular entry

O(n)

Creating a heap takes ______ time while inserting into a heap (or priority queue) takes _______ time.

O(n) O(logn)

array average deletion array worst deletion

O(n) O(n)

array average insertion array worst insertion

O(n) O(n)

array average search array worst search

O(n) O(n)

doubly-linked list average access doubly-linked list worst access

O(n) O(n)

doubly-linked list average search doubly-linked list worst search

O(n) O(n)

queue average access queue worst access

O(n) O(n)

queue average search queue worst search

O(n) O(n)

singly-linked list average access singly-linked list worst access

O(n) O(n)

singly-linked list average search singly-linked list worst search

O(n) O(n)

stack average access stack worst access

O(n) O(n)

stack average search stack worst search

O(n) O(n)

linked list (unsorted) Find Max linked list (unsorted) Extract Max linked list (unsorted) Increase Key linked list (unsorted) Insert linked list (unsorted) Delete linked list (unsorted) Merge

O(n) O(n) O(1) O(1) O(1) O(1)

queue average deletion queue worst deletion

O(1) O(1)

binary search tree average search binary search tree worst search

O(logn) O(n)

Finding the min value in a BST

O(n)

single linked list - If link to the front of a linked list is kept, then... add at the end is

O(1)

single linked list - If link to the front of a linked list is kept, then... add to front or remove the first item is

O(1)

single linked list - deleting front position

O(1)

single linked list - find front position

O(1)

singly linked list - insert at front position

O(1)

sorted array - Find min/max or by value

O(1)

sorted array - delete max

O(1)

sorted linked list - delete min

O(1)

sorted linked list - find min

O(1)

array average access array worst access

O(1) O(1)

doubly-linked list average deletion doubly-linked list worst deletion

O(1) O(1)

doubly-linked list average insertion doubly-linked list worst insertion

O(1) O(1)

queue average insertion queue worst insertion

O(1) O(1)

singly-linked list average deletion singly-linked list worst deletion

O(1) O(1)

singly-linked list average insertion singly-linked list worst insertion

O(1) O(1)

stack average deletion stack worst deletion

O(1) O(1)

stack average insertion stack worst insertion

O(1) O(1)

linked list (sorted) Find Max linked list (sorted) Extract Max linked list (sorted) Increase Key linked list (sorted) Insert linked list (sorted) Delete linked list (sorted) Merge

O(1) O(1) O(n) O(n) O(1) O(m+n)

double linked list: Deleting: _____, if done at the head or tail, _____ if anywhere else since we have to reach that position by traversing the linkedlist linearly.

O(1) O(n)

double linked lists - Inserting: _____, if done at the head or tail, ____ if anywhere else since we have to reach that position by traversing the linkedlist linearly.

O(1) O(n)

hash table average search hash table worst search

O(1) O(n)

a tree where values are sorted based on the root value, - if value inserted greater than root it goes to the right subtree and - if value inserted less than root it goes to the left subtree. Accessing, searching, insertion, and deleting in the best case is O(log n). Accessing, searching, insertion, and deleting in the worst case is O(n). O(n log n) for building the BST inserting n elements.

Binary Search Tree (BST):

queue dequeue

O(1)

queue enqueue

O(1)

hash table average access hash table worst access

N/A N/A

- Determining if a number is even or odd - Using a constant-size lookup table

O(1)

array - Find by position

O(1)

array - Insert element at the back of an array

O(1)

array - Remove element at the back

O(1)

array list - findKth

O(1)

array list - findKth is____ because you know the index of the value.

O(1)

array list - insert value at end

O(1)

best-case time complexity for searching a fixed-size array-based bag ADT for a particular entry?

O(1)

circular array - Find by position

O(1)

circular array - delete at back position

O(1)

circular array - delete at front position

O(1)

circular array - insert at back position

O(1)

circular array - insert at front position

O(1)

double linked list - Add to front or end is

O(1)

double linked list - Delete at the beginning or end is

O(1)

double linked list - delete at back

O(1)

double linked list - delete at front

O(1)

double linked list - find back position

O(1)

double linked list - find front position

O(1)

double linked list - insert at front or back

O(1)

hash table - delete by value

O(1)

hash table - find by value

O(1)

hash table - insert

O(1)

linked list w/ pointer - delete at front

O(1)

linked list w/ pointer - find back position

O(1)

linked list w/ pointer - find front position

O(1)

linked list w/ pointer - insert at back

O(1)

linked list w/ pointer - insert at front

O(1)

Disadvantages: - Need to manage multiple pointers

double linked list

Advantages: - Take as much memory as needed --> Number of links can be expanded dynamically - No need to move items around

linked list

Disadvantages: - Conceptually less intuitive than arrays - The objects/items of the link can be located anywhere in memory (loss of data locality)

linked list

First In First Out (FIFO): a list of elements such that: elements are inserted at the end of the list (rear/back) and deleted at the front of the list. Enqueue - insert at the end. Dequeue - delete the most recently inserted element.

queue

Advantages: - insertion of new items and removal of existing items is cheap

single linked list

Disadvantages: - it's not easily indexable so calls are expensive

single linked list


Related study sets

Public Speaking Final Review UNWSP

View Set

IGGY Chapter 08: Concepts of Care for Patients at End of Life

View Set