Exam Flashcards

1
Q

Analysis of Selection Sort

A

Best case:
Array in sorted order
Comparisons: O(n^2)
Time: O(n^2)

Average case:
Comparisons: O(n^2)
Time: O(n^2)

Worst case:
Array in reverse order
Comparisons: O(n^2)
Time: O(n^2)

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

Analysis of Merge Sort

A

Best case:
Comparisons: O(n log n)
Time: O(n log n)

Average case:
Comparisons: O(n log n)
Time: O(n log n)

Worst case:
Comparisons: O(n log n)
Time: O(n log n)

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

Analysis of Insertion Sort

A

Best case:
Array in sorted order
Comparisons: O(n)
Time: O(n)

Average case:
Comparisons: O(n^2)
Time: O(n^2)

Worst case:
Array in reverse order
Comparisons: O(n^2)
Time: O(n^2)

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

Analysis of Quick Sort

A

Best case:
Comparisons: O(n log n)
Time: O(n log n)

Average case:
Comparisons: O(n log n)
Time: O(n log n)

Worst case:
When the list is sorted and left most element is chosen as the pivot
Comparisons: O(n^2)
Time: O(n^2)

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

Analysis of Shell Sort

A

Best case:
Comparisons: O(n log n)
Time: O(n log n)

Average case:
Comparisons: O(n^4/3)
Time: O(n(log(n))^2)

Worst case:
Comparisons: O(n^3/2)
Time: O(n(log(n))^2)

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

Given a list of size n, for which gap value is shell sort the same as insertion sort?

A

1

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

Analysis of Sequential Search

A

Best case:
O(1)

Average case:
O(n)

Worst case:
O(n)

Comparisons:
n

n+1/ 2

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

Analysis of Binary Search

A

Best case:
O(log n)

Average case:
O(log n)

Worst case:
O(log n)

Comparisons:
log n
2 log n (average)
n (best)

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

Pros and Cons of Selection Sort

A

Pros:
Simple implementation

Cons:
Inefficient on large lists

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

Pros and Cons of Merge Sort

A

Pros:
Quicker for larger lists
Unlike insertion sort, it doesn’t go through the list multiple times

Cons:
Comparatively slower than other algorithms for smaller data sets

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

Pros and Cons of Insertion Sort

A

Pros:
Simple implementation
Extremely efficient on small lists
More efficient than bubble and selection sort

Cons:
Much less efficient on large lists

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

Pros and Cons of Quick Sort

A

Pros:
An in-place sorting algorithm does not use additional memory space while sorting.

Cons:
Not very stable

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

Binary Search Tree Analysis

A

Average:
O(log n)

Worst:
O(n)

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

Hash Table Analysis

A

Average:
O(1)

Worst:
O(n)

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

Calculate the number of comparisons insertion sort

A

Best Case:
n - 1

Worst Case:
n(n-1)/2

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

Calculate the number of comparisons selection sort

A

n(n-1)/2

17
Q

Benefits of Hash search over Binary Search

A

Hash search has O(1) for:
Search
Insert
Delete

Binary only has O(log n)

18
Q

Benefits of Binary Search over Hash Search

A

Binary Search can get all keys in sorted order by just doing an inorder traversal
Binary Search is easier to implement whereas Hash Search general requires importing libraries

19
Q

An important advantage of quadratic probing over Linear Probing

A

Quadratic Probing is far less prone to clustering whereas Linear Probing forms groups by placing elements one after another thus clustering occurs which can slow down the algorithm

20
Q

What is BFS (Breadth First Search) used for?

A
  • Shortest Path in an unweighted graph
  • Underpins other algorithms
  • Peer-to-peer networking
  • Cycle detection
21
Q

What is DFS (Depth First Search) used for?

A
  • Finding connected components in subgraphs
  • Maze solving and generation
  • Solving problems which have only one possible solution
  • cycle detection
  • Scheduling
22
Q

Prim’s algorithm is:

A
  • Built from a starting vertex

- Similar to BFS

23
Q

Kruskal’s algorithm is:

A
  • Built by picking edges anywhere in a graph

- Priority queue

24
Q

How do you know if an Euler’s PATH exists in a graph?

A

if and only if there are no more than 2 vertices with an odd number of edges

25
Q

How do you know if an Euler’s CYCLE exists in a graph?

A

if and only if all vertices have an even number of edges