Reviewing the Basics of Algorithms and Data Structures

A comprehensive review of algorithms and data structures basics: arrays, strings, hash tables, linked lists, trees, stacks, queues, sorting, and time complexity for coding problems.

Read in: ja
Reviewing the Basics of Algorithms and Data Structures

Overview

As I resume the daily routine of solving coding quizzes, I am reviewing the basics of algorithms and data structures as a form of rehabilitation.

Arrays

Time Complexity

Operation Complexity
Access O(1)
Search O(n)
Search (sorted) O(log(n))
Insertion O(n)
Insertion (end) O(1)
Deletion O(n)
Deletion (end) O(1)

Considerations

Strings

Time Complexity

Omitted due to implementation

Considerations

Hash Tables (Hash Maps)

Operation Complexity
Access N/A
Search O(1)*
Insertion O(1)*
Deletion O(1)*

*Average case

Considerations

Recursion

Time Complexity

Omitted due to implementation

Considerations

Sorting and Searching

Sorting Algorithm Complexity

Algorithm Time Complexity Space Complexity
Bubble Sort O(n^2) O(1)
Insertion Sort O(n^2) O(1)
Selection Sort O(n^2) O(1)
Quick Sort O(nlog(n)) O(log(n))
Merge Sort O(nlog(n)) O(n)
Heap Sort O(nlog(n)) O(1)
Counting Sort O(n+k) O(k)
Radix Sort O(nk) O(n+k)

Search Algorithm Complexity

Algorithm Complexity
Binary Search O(log(n))

Two-Dimensional Arrays

package main

import "fmt"

func main() {
    matrix := [][]int{[]int{0, 1}, []int{2, 3}, []int{4, 5}}
    for i, v := range matrix {
        for j, _ := range v {
            // i is row and j is column
            matrix[i][j] = 0
        }
    }
    fmt.Println(matrix)
}

Considerations

Linked Lists

Time Complexity

Operation Complexity
Access O(n)
Search O(n)
Insertion O(1)
Deletion O(1)

Queue

Time Complexity

Operation Complexity
Enqueue O(1)
Dequeue O(1)

Stack

Time Complexity

Operation Complexity
Push O(1)
Pop O(1)

Trees

Graphs

Complexity

Let V be vertices and E be the number of edges.

Algorithm Complexity
Depth-First Search O(V+E)
Breadth-First Search O(V+E)
Topological Sort O(V+E)

Heap

Time Complexity

Operation Complexity
Insertion O(log(n))
Deletion O(log(n))

Trie

Time Complexity

Let m be the length of the string.

Operation Complexity
Search O(m)
Insertion O(m)
Deletion O(m)

References

Others

Articles referenced while studying data structures and algorithms.

Tags: Algorithm Data Structure
Share: 𝕏 Post Facebook Hatena
✏️ View source / Discuss on GitHub
☕ Support

If you enjoy this blog, consider supporting it. Every bit helps keep it running!


Related Articles