Sorting Algos Flashcards

1
Q

merge sort

A

function mergeArrs(arr1, arr2) {
let result = [];
let i = 0;
let j = 0;

while (i < arr1.length && j < arr2.length) {
    if (arr1[i] < arr2[j]) {
        result.push(arr1[i]);
        i++;
    }
    if (arr2[j] < arr1[i]) {
        result.push(arr2[j]);
        j++
    }
}
while (i < arr1.length) {
    result.push(arr1[i]);
    i++
}
while (j < arr2.length) {
    result.push(arr2[j]);
    j++;
}
return result; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

merge sort runtime

A

O(n*log n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

insertion sort

A
function insertion(arr) {
  for (let i = 0; i < arr.length; i++) {
    let key = arr[i];
    let j = i - 1;
    while (j >= 0 && arr[j] > key) {
      arr[j + 1] = arr[j];
      j--;
    }
    arr[j + 1] = key;
  }
  return arr;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

insertion sort runtime

A

0(n*n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Pathfinding and Maze Solving

A

DFS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Detecting Cycles in Graphs

A

DFS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Connected Components in Graphs

A

DFS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Topological Sorting (push to stack after visiting all dependencies)

A

DFS post-order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Solving Puzzles and Backtracking

A

DFS, backtracks when it finds deadends

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Binary Tree Traversal

A

DFS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Binary Tree Traversal: Preorder

A

DFS, process root first

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Binary Tree Traversal: inorder

A

DFS, process left subtree first

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Binary Tree Traversal: postorder

A

DFS, process root last

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Solving Island Problems in Grids

A

DFS, explore each island, traverse adjacent cells in multiple directions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Generating All Possible Subsets or Combos

A

DFS - explore each subset by including or excluding each el in set

17
Q

Word Search in Grid

A

DFS explores each cell recursively to see if it can form word moving in all directions

18
Q

Tree/Graph Cloning

A

DFS traverses each node in graph, copying it and connections to make identical copy

19
Q

DFS - inorder

A

function inorder(root) {
let result = [];
function traverse(node) {
if (node) {
traverse(node.left);
result.push(node.value);
traverse(node.right)
}
}
traverse(root)
return result;
}

20
Q

DFS -preorder

A

function preorder(root) {
let result = [];
function traverse(node) {
if (node) {
result.push(node.value);
traverse(node.left);
traverse(node.right);
}
}
traverse(root)
return result;
}

21
Q

post order

A

function postorder(root) {
let result = [];
function traverse(node) {
if (node) {
traverse(node.left);
traverse(node.right);
result.push(node.value);
}
}
traverse(root)
return result;
}

22
Q

delete at position in linked list

A

function deletePosition(list, position) {
if (list === null) return null;
if (position === 1) {
return list.next;
}
let prev = list;
let curr = list.next;
let count = 2;
while (curr && count < position) {
prev = curr;
curr = curr.next;
count++;
}
prev.next = curr.next;
return list;
}

23
Q

delete head linked list

A

function deleteHead(list, node) {
if (list === null) return null;
list = list.next;
return list;
}

24
Q

delete tail linked list

A

function deleteNode(list) {
if (!list || list.next === null) return null;
let prev = list;
let curr = list.next;
while (curr.next) {
prev = curr;
curr = curr.next
}
prev.next = null;
return list;
}