CS algorithms Flashcards

1
Q

Linear Search

A

we simply start at the beginning of our data set and iterate through each item until we either i) find what we were searching for or ii) reach the end and conclude the item being searched for is not in the set.

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

Issues with Linear Search

A

it can be very time consuming.

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

Binary Search

A

Takes an ordered set of data and starts searching in the middle. If the middle element is what we were looking for, great! That means we’re done. If the middle element is smaller than the target, we can eliminate the entire left-hand side of our data set and repeat this process on the right-hand side.

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

Issues with Binary Search

A

can’t use binary searching unless the data is sorted!

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

Selection Sort

A

several playing cards you need to put in order. You start off by looking for the lowest card, and when you find it, put it at the front of your hand. Then, you look for the second lowest card and insert it directly behind the first card. You repeat this process, selecting the next lowest card and moving it behind the most recently placed card, until you have moved all cards into the right place.

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

drawbacks to selection sort

A

One of the drawbacks of this algorithm is its efficiency. Regardless of the starting order of your elements (random, mostly sorted, reverse sorted, etc.), this algorithm will always have a runtime of O(n²). This is because each pass requires us to check every element of the unsorted part of our list or array.

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

Is Selection Sort a comparison sorting algorithm?

A

Yes, since to select the desired element, we compare a current value to the rest of the “unsorted” segment of our list or array.

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

Why do we end our loop before processing the item in the last index of an array when performing Selection Sort?

A

By the nature of how this algorithm works, if all other elements have been sorted, the last element will have been moved to the correct index.

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

When using Selection Sort, what is the difference between the order or arrangement of elements in best case versus worst case?

A

None, since the same number of operations will be performed regardless of how elements are ordered.

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

Show how the array [4, 8, 3, 1, 9, 6] changes as it is being sorted using Selection Sort. To do this, write out the contents of the array after each pass of the algorithm. (hint…we should only need n-1 passes to sort the array, where n is the size)

A
[1, 8, 3, 4, 9, 6]  
[1, 3, 8, 4, 9, 6]  
[1, 3, 4, 8, 9, 6]  
[1, 3, 4, 6, 9, 8]  
[1, 3, 4, 6, 8, 9]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Insertion Sort

A

Imagine you lay out several playing cards in a line on a table. Consider the first card to be a sorted subarray of lengh = 1. Pick up each card (in this case, from left to right) and insert it into the correct position in the sorted subarray. Shift other cards to the right to make room for the card you are inserting as you go. O(n²).

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

main steps of Insertion sort

A
  1. Separate the first element from the rest of the array. Consider it a sorted list of one element.
  2. For all other indices, beginning with [1]:
    a. Copy the current item into a temp variableb. Iterate to the left until you find the correct index in the “sorted” part of the array at which this element should be inserted
    - Shift items over to the right as you iteratec. When the right index is found, copy temp into this position
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Selection Sort steps

A
  1. Start with current index = 0
  2. For all indices EXCEPT last:a. Loop through elements on right-hand-side
    of current index and find smallest elementb. Swap element at current index with
    smallest element found in above loop
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Is Insertion Sort a comparison sorting algorithm?

A

Yes, since to find the correct index at which we “insert” the current element we are sorting, we compare it to the rest of the “sorted” segment of our list or array.

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

When using Insertion Sort, what is the difference between the order or arrangement of elements in best case versus worst case?

A

When performing Insertion Sort, the algorithm runs more efficiently if the elements are already mostly sorted. When the original array is close to sorted order, fewer comparisons are required to sort it. But if the array is in reverse-sorted order, the number of comparisons that need to be done increases significantly. For example: [5, 4, 3, 2, 1] When inserting the 1 into the correct part of the array, we have to compare it with every other number. While this is not such a big deal in an array with 5 elements, it gets very costly as that array becomes bigger.

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

Show how the array [4, 8, 3, 1, 9, 6] changes as it is being sorted using Insertion Sort.

A

[4, 8, 3, 1, 9, 6] (comparison done, no swap made)
[3, 4, 8, 1, 9, 6]
[1, 3, 4, 8, 9, 6]
[1, 3, 4, 8, 9, 6] (comparison done, no swap made)
[1, 3, 4, 6, 8, 9]

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

Merge Sort

A
  1. Recursively divide orginal array or list in half until both pieces have a length of 1.
  2. Starting with these singular subsets, merge sorted pieces back together.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Quick Sort

A

In this algorithm, we move smaller elements to one side of the array and larger elements to the other side of the array by comparing them to a “pivot” element, which can be chosen in a few different ways. This divides our original array into two subarrays that are closer to being sorted. We then recursively call Quick Sort on each subarray until we are down to singular elements (which by their singular nature, must be sorted) runtime: best case: Ω(nlog(n)) worst: O(n²)

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

Quick Sort steps

A

While the data set has a length >= 1:

  1. Select pivot (typically first, last or middle)
  2. For each item in data set:
    - if item < pivot AND not on LHS of pivot, move to appropriate side of data set
    - else if item > pivot AND not on RHS of pivot, move to appropriate side of data set
  3. Recursively Quick Sort LHS and RHS of pivot
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Show how Quick Sort for array [2, 7, 5, 4, 0, 1]

A

[2, 7, 5, 4, 0, 1] // pivot = 2
[0, 1, 2, 4, 7, 5]

[0, 1, 2, 4, 7, 5] // LHS pivot = 0
[0, 1, 2, 4, 7, 5] // LHS pivot = 0

[0, 1, 2, 4, 7, 5] // LHS done, RHS pivot = 1 (single element)

[0, 1, 2, 4, 7, 5] // Quicksort RHS of 2

[0, 1, 2, 4, 7, 5] // Quicksort RHS of 2, pivot = 4
✓ ✓ ✓ ✓
[0, 1, 2, 4, 5, 7] // Quicksort RHS of 4, pivot = 7
✓ ✓ ✓ ✓
[0, 1, 2, 4, 5, 7] // Quicksort LHS of 7, pivot = 5 (single element)

21
Q

Time complexity: What is Constant time 1

A

No matter how many elements we’re working with, the algorithm/operation/whatever will always take the same amount of time

22
Q

Time Complexity: What is logarithmic Time log(n)

A

You have this if doubling the number of elements you are iterating over doesn’t double the amount of work. Always assume that searching operations are log(n)

23
Q

Time Complexity: Linear Time: n

A

Iterating through all elements in a collection of data. If you see a for loop spanning from 0 to array.length you probably have ‘n’ or linear runtime

24
Q

Time Complexity: Quasilinear Time: n*log(n)

A

You have this if doubling the number of elements you are iterating over doesn’t double the amount of work. Always assume that any sorting operation is n*log(n)

25
Q

Time Complexity: Quadratic Time: n^2

A

every element in a collection has to be compared to every other element. ‘The handshake problem’

26
Q

Time Complexity: exponential time 2^n

A

if you add a single element to a collection the processing power required doubles

27
Q

Time Complexity: worst to best

A

O(n!) => O(2^n) => O(n^2) => O(n log n) => O(n) => O(log n), O(1)

28
Q

What is Fibonacci series, what is the sequence?

A

Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34. The next number is found by adding up the two numbers before it.

29
Q

What is the fibonacci formula using recursion?

A
def fib(n):
if n < 2:
return n
else:
return fib(n-1) + fib(n -2)
30
Q

What is the fibonacci formula using recursion/cache?

A
def fib(n, cache):
if n < 2:
return n
elif cache and cache[n] > 0:
return cache[n]
else:
if not cache:
cache = {i: 0 for i in range(n +1)}
cache[n] = fib(n-1) + fib(n -2)
return cache
31
Q

What are Data Structures used for

A

Ways of organizing information with optimal runtime complexity for adding or removing records

32
Q

What is a Queue and does the data structure look like?

A

a queue is a line or array like: FIFO so first in first out
rec = record
rec&raquo_space; [rec&raquo_space; rec&raquo_space; rec» rec]&raquo_space; rec

33
Q

Pros of a Linked list

A

Easier to insert into and delete from the middle of a linked list compared to an array
We can keep adding elements to linked lists as much as we want unlike arrays with a static amount of allocated memory

34
Q

Cons of a linked List

A

not as cache-friendly since caches are typically optimized for contiguous memory accesses

35
Q

Pros of Queues

A

Queues are simple and intuitive to use and implement

They can be used to serialize data coming in from multiple sources

36
Q

Cons of Queues

A

Are not general-purpose at all in and of themselves.

cannot be used for anything else. Only one function

37
Q

What are trees

A

trees can be thought of as linked lists, but without the constraint that each node only points to one other node. A tree node can point to multiple other nodes in the tree. Linked lists themselves count as trees.

38
Q

what is the root of a tree

A

The topmost node in the tree

39
Q

Tree: what is a child

A

a node directly connected to another node when moving away from the root node

40
Q

Tree: Parent

A

a node directly connected to another node when moving towards the root node

41
Q

Tree: siblings

A

Nodes that share the same parent are considered siblings

42
Q

Tree: siblings

A

A node that does not have any children of its own

43
Q

How are binary search tree organized. How is the data split up?

A

For any given node all values in the left subtree are less than the value at the given node. Conversely, all values in the right subtree are greater than or equal to the value at the given node

14
/ \
9 22

44
Q

How are binary search tree organized. How is the data split up?

A

For any given node all values in the left subtree are less than the value at the given node. Conversely, all values in the right subtree are greater than or equal to the value at the given node

 14
 /   \
9    22
/  \
8   11
45
Q

Pros binary search tree

A

searching for an element in a binary search tree is significantly more efficient than searching through an array or linked list

46
Q

Cons binary search tree

A

as a trade-off, it is not as efficient to insert into a binary search tree

the performance of a binary search tree depends quite a lot on whether the tree is balanced or not.

47
Q

What is a heap?

A

priority: you keep the data you use more often at the top of the heap so they are easier to reach.

Think about it like a queue, organized with the max value in front.

it also could be a binary tree

or a hash table which is like a dictionary in python

or array

48
Q

pros of heaps

A

searching for an element in a binary search tree is significantly more efficient than searching through an array or linked list

49
Q

Cons of heaps

A

as a tradeoff, it is not as efficient to insert into a binary search tree

the performance of a binary search tree depend quite a lot on whether the tree is balanced or not.