Test 1 Questions Flashcards

1
Q

The time complexity of an algorithm can be Ω(n) and O(n^2)

T/F

A

TRUE

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

Interpolation search is ___ on average than binary search.

A

Faster

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

Given an unsorted array of n integers, the median element in the array can be found ____ time in the average-case

A

Θ(n)

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

Quicksort uses a ____ pivot selection

A

Randomized.

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

Is there an algorithm that can factor an integer into its prime factors in polynomial time?

A

Yes

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

The asymptotic time complexity for adding two n-bit integers is:

A

Θ(n)

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

Timsort is a hybrid of

A

Merge and Insertion Sort.

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

Timesort is a ___ sorting algorithm

A

Stable - Like Merge and Insertion Sort.

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

In a binary heap (tree), any two leaf nodes have either the same depth or depths differing by one.

T/F

A

TRUE

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

Given an array of 1 million records which only a few are out of order and are not far from their correct position, the best choice of sorting algorithm is :

A

Insertion Sort

Because the array is almost sorted.

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

What is the selection problem?
What is the name of an algorithm that solves the selection problem?

A

Find the i-th element in an unsorted array.
Quickselect.

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

What algorithm have Θ(n ^ (lg2^3)) time complexity?

A

Divide and Conquer Multiplication.

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

What is meant by a decrease and conquer algorithm?
Name 3 Decrease and conquer algorithms

A

With every recursive call the size of the input decreases.

  1. Binary Search
  2. Square Root Alg
  3. Euclid’s Alg
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What’s the recurrence relation for quicksort?

A

T(n)= 2T(n/2) + n

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

Give the name of another sorting algorithm with the same recurrence relation with quicksort?

A

Merge Sort

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

Describe a better way to pick a pivot in quicksort.

A
  1. Get 1st, Last, and Middle element/index and use the median of those.
  2. Randomly pick a pivot.
17
Q

If a heap is used to represent a priority queue, and the first element is removed, the heap property is restored by moving the last element to the root of the tree, and then running MAX-HEAPIFY on the new root element. What is the worst-case complexity of restoring the heap property this way?

A

Θ(log n)

18
Q

Name a sorting algorithm that has better best-case complexity than worst-case complexity and give both its best and worst-case complexity.

A

Max-Heapify

Best: Θ(1)
Worst: Θ(log n)

19
Q

What’s an “in-place” algorithm? Give an example.

A

Uses at most O(1) more space than the original array.

Heap Sort

20
Q

What’s a “stable” algorithm? Give an example.

A

Doesn’t change the duplicate’s original ordering relative to each other.

Merge Sort.

21
Q

Give an example of a hybrid sorting algorithm.

A

You can use quickselect and once the array get to a smaller size, switch to Insertion Sort.