Looking for IN1002 Introduction to Algorithms (PRD2 A 2024/25) test answers and solutions? Browse our comprehensive collection of verified answers for IN1002 Introduction to Algorithms (PRD2 A 2024/25) at moodle4.city.ac.uk.
Get instant access to accurate answers and detailed explanations for your course questions. Our community-driven platform helps students succeed!
What is the time complexity of a method that finds the largest value in an array of size n by examining each element?
What is the principal disadvantage of quicksort?
What is the maximum number of keys that will be examined in a single binary search of an ordered array of 25,000,000 elements?
Recall binary search:
lo ← 0
hi ← n-1
WHILE lo < hi
mid ← (lo+hi) DIV 2
IF a[mid] = k
RETURN mid
IF a[mid] < k
lo ← mid+1
IF a[mid] > k
hi ← mid-1
RETURN -1;
where DIV is division discarding any remainder to return a whole number.
Given the following array:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| B | C | F | G | J | K | M | N | P | Q | S | T | U | W | Y |
How many values in the array will be examined in a binary search for the key K?
What is the worst-case time complexity of merge sort?
Recall merge sort:
Sort(a[0..n-1])RETURN MergeSort(a, 0, n-1)
MergeSort(a, lo, hi)IF lo >= hi
RETURN a[lo..hi]
mid ← (lo + hi + 1) DIV 2
l ← MergeSort(a, lo, mid-1)
r ← MergeSort(a, mid, hi)
RETURN Merge(l, r)
where DIV is division discarding any remainder to return a whole number.
Merge(l[0..q-1], r[0..s-1])m ← a new array of length q+s
i ← 0
j ← 0
WHILE i < q OR j < s
IF i < q AND (j = s OR l[i] <= r[j])
m[i+j] ← l[i]
i ← i+1
ELSE
m[i+j] ← r[j]
j ← j+1
RETURN m
Consider a merge sort of the following array:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 17 | 12 | 8 | 2 | 7 | 18 | 46 | 99 | 14 | 3 |
What values will be assigned to the variables l and r in the outermost call of MergeSort?
What is the space complexity of selection sort (excluding the array being sorted)?
What is the worst-case time complexity of selection sort?
Recall selection sort:
SelectionSort(a[0..n-1])
i ← 0
WHILE i < n-1
min ← i
j ← i+1
WHILE j < n
IF a[j] < a[min]
min ← j
j = j + 1
swap the elements of a at positions i and min
i ← i + 1
Consider a selection sort of the following array:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| L | E | X | I | C | O | G | R | A | P | H | Y |
What is the state of the array at the end of the third iteration of the outer loop?