CPI 220 Mock Midterm

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/49

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

50 Terms

1
New cards

What is an Algorithm?

A set of well-defined logical steps that must be taken to perform a task.

2
New cards

What are the properties of an algorithm?

unambiguous, complete, provable

3
New cards

Complete the sentence:

An Abstract Data Type is a class of objects whose behavior is defined by a set of _____________________ and a set of ________________________.

values, operations

4
New cards

Complete the sentence:

API is an acronym for ___________________________________________. It is a list of _________________________ and _____________________ with an informal description of each.

Application programming interface, constructors, instance methods

5
New cards

A program that uses an API is called a _________________

client

6
New cards

Name two collection data types

Stack, Queue

7
New cards

What are the add and delete operations of a stack?

Push()

pop()

8
New cards

What are the add and delete operations of a queue?

enqueue()

dequeue()

9
New cards

What is the 4 letter acronym for the way in which items are removed from a stack?

LIFO (last in, first out)

10
New cards

What is the 4 letter acronym for the way in which items are removed from a queue?

FIFO (first in, first out)

11
New cards

Name two data structures used to implement a stack

Linked lists, Resizing Arrays

12
New cards

Name two types of algorithm analysis

run-time, memory

13
New cards

Run time analysis of algorithms maybe carried out with mathematical models or _____________________________.

Empirical analysis observations

14
New cards

Run time is modeled with the power law,

T(N) = aN^b.

What is the log of this equation?

log(T(N)) = log(a) + b log(N)

15
New cards

Do all basic operations (add, divide, sine, etc) run in the same amount of time?

No. They all have different compute times.

16
New cards

How many operations as a function of N are executed in the following code?
for(int i=0; i<N; i++)
for(int j=i+1; j<N; j++)
a = j;

(N(N-1)) / 2

17
New cards

How many operations as a function of N are executed in the following code?

for(int i=0; i<N; i++)

for(int j=0; j<N; j++)

a = j;

N^2

18
New cards

Analysis shows that a particular algorithm has run time 2N^3 + 3N + 20. What is the tilde notation for the run time?

2N^3 (takes the leading term only)

19
New cards

What is order of growth for the algorithm with run time 2N^3 + 3N + 20?

O(N^3)

20
New cards

Most algorithms have an order of growth that falls into one of the following functions: N^3, N, 2^N, 1, logN, NlogN, N^2

1 < log N < N < N log N < N^2 < N^3 < 2^N

21
New cards

What is the dynamic connectivity problem? (input and two operations)

- The input is a sequence of pairs of integers.

- The operations are connected (find) and union.

22
New cards

How many connected components are in graph?

0 ‐‐‐ 1

2 ‐‐‐‐ 3 ‐‐‐‐ 5

6‐‐‐‐‐ 7‐‐‐‐‐8‐‐‐‐‐9

3

23
New cards

The implementations of Union Find discussed in class were implemented with three functions:

Find, union, connected

24
New cards

The basic Quick‐union algorithm (slide 20) uses trees for each connected component. The trees may be constructed by examining the id array, where id[i] is the parent of i. Sketch the connected component trees defined by the following id array

array index = 0 1 2 3 4 5 6 7 8 9

id = 0 1 9 4 9 6 6 7 8 9

0, 1, 7, 9, 6-5, 9-2+4-3

25
New cards

Why is Weighted Quick Union superior to Quick‐union?

Weighted quick union attaches the smaller tree to the root of the larger tree, reducing the tree height and the time complexity of operations.

26
New cards

T/f for Selection Sort: Algorithm finds the index of the minimum remaining entry "min" and swaps a[i] and a[min].

True

27
New cards

T/f For Insertion Sort: Entries to the left of a[i] are fixed and in ascending order. No entry to the right of a[i] is smaller than an entry to the left of a[i].

False

28
New cards

Insertion sort is an Nlog(N) algorithm.

False. O(N^2)

29
New cards

Binary Insertionsort sorts 0s and 1s only.

False. It can sort any comparable elements.

30
New cards

A sorting algorithm has been applied to an input array. Currently the array is A B C D A B C with pointer [i] positioned at the first D. Is it possible that Selection Sort is the sorting algorithm being applied?

No. In selection sort, the first unsorted element is always swapped with the minimum element found in the unsorted portion of the array

31
New cards

A sorting algorithm has been applied to an input array. Currently the array is A B C D A B C with pointer [i] positioned at the first D. Is it possible that Insertion Sort is the sorting algorithm being applied?

Yes. In insertion sort, elements to the left of the current element are sorted.

32
New cards

Apply one h‐iteration of shell sort

H A P P Y D A Y S

with h=4.

H A A P S D P Y Y

33
New cards

What are the three basic steps of Mergesort?

1. Divide the array into two halves

2. recursively sort each half

3. merge the two sorted halves into a single sorted array

34
New cards

Given the left subarray A B D C and right subarray E F G H, which need to be merged to produce the final sorted array. Is it possible that these subarrays are the product of Mergesort?

No, A B D C is not sorted.

35
New cards

How many compares does Mergesort use for an array of length n?

NlogN compares in the worst case, where N is the length of the array.

36
New cards

Does Mergesort use a significant amount of auxiliary memory?

Yes. requires O(N) aux memory

37
New cards

What is the in‐place property of sorting algorithms? Give an example of one that has this property and one that does not.

Insertion sort. Merge sort (does not)

38
New cards

What is a stable sort?

Preserves the relative order with equal keys

39
New cards

Which sorting algorithm was honored as one of the top 10 algorithms of the 20th century?

Quicksort

40
New cards

What are the 3 basic steps of Quicksort?

1. Shuffle

2. Partition the array into two subarrays: elements less than the pivot and elements greater than the pivot

3. recursively sort subarray

41
New cards

Does Quicksort run in optimal time if the items in the array are already sorted?

No. it can degrade to O(N^2) time complexity

42
New cards

Is quicksort a divide and conquer method?

Yes. Quicksort divides the array into subarrays based on a pivot, conquers by recursively sorting the subarrays, and combines the subarrays

43
New cards

The basic operations of collections are insertion and deletion of items. What are these operations called for priority queues?

insert and delMax

44
New cards

What is a binary tree?

A structure with nodes, each having up to two children

45
New cards

what is a complete binary tree?

A binary tree where all levels are filled and perfectly balanced, except for the bottom level (filled left to right)

46
New cards

What is a heap-ordered binary tree?

A complete binary tree where each parent's key is greater than or equal to its children

47
New cards

what is a binary heap?

An array representation of a heap-ordered complete binary tree

48
New cards

In a binary heap, are the children ordered smallest to largest from left to right?

No. Only the heap-order property matters; siblings can be in any order.

49
New cards

When a node is added to a binary heap, it __________ to its correct position. When a node is deleted from the heap, the new root node __________ to its correct position.

Swims up, sinks down

50
New cards

heapsort uses the binary heap to build a sorted array. Fill in the two steps

Builds up a max-heap, Repeatedly removes the maximum and places it at the end.