Question 31
MediumWhat is the worst-case time complexity of heap sort?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the worst-case time complexity of heap sort?
O(log n)
O(n²)
O(n log n)
O(n)
Sign up free to see the answer and a step-by-step explanation.
What extra space does standard merge sort on an array need?
O(n²)
O(log n) only
O(1)
O(n)
Sign up free to see the answer and a step-by-step explanation.
Which sorting algorithm is used for integers in a small known range, counting how many times each value appears?
Bubble sort
Quick sort
Shell sort
Counting sort
Sign up free to see the answer and a step-by-step explanation.
How many swaps does selection sort make on an array of n elements in the worst case?
n log n
n²
n(n − 1)/2
n − 1
Sign up free to see the answer and a step-by-step explanation.
Which sort is generally fastest in practice for large arrays in memory, despite its O(n²) worst case?
Insertion sort
Bubble sort
Selection sort
Quick sort
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [5, 2, 9, 1, 5, 6]
swaps = 0
for i in range(len(a)):
for j in range(len(a) - 1 - i):
if a[j] > a[j + 1]:
a[j], a[j + 1] = a[j + 1], a[j]
swaps += 1
print(swaps)
5
7
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [4, 3, 2, 10, 12, 1, 5, 6]
for i in range(1, len(a)):
key, j = a[i], i - 1
while j >= 0 and a[j] > key:
a[j + 1] = a[j]
j -= 1
a[j + 1] = key
if i == 3:
print(a)
[2, 3, 4, 10, 12, 1, 5, 6]
[3, 4, 2, 10, 12, 1, 5, 6]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def part(a, lo, hi):
p, i = a[hi], lo
for j in range(lo, hi):
if a[j] < p:
a[i], a[j] = a[j], a[i]
i += 1
a[i], a[hi] = a[hi], a[i]
return i
a = [7, 2, 9, 4, 3, 8, 5]
k = part(a, 0, len(a) - 1)
print(k, a)
3 [4, 2, 3, 5, 9, 8, 7]
4 [2, 4, 3, 7, 5, 8, 9]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [4, 2, 2, 8, 3, 3, 1]
c = [0] * (max(a) + 1)
for x in a:
c[x] += 1
for i in range(1, len(c)):
c[i] += c[i - 1]
print(c[3], c[8])
5 8
4 7
Sign up free to see the answer and a step-by-step explanation.
How many comparisons does merge sort need at most to merge two sorted lists of 5 elements each?
10
5
25
9
Sign up free to see the answer and a step-by-step explanation.
15
6
[2, 3, 4, 12, 10, 1, 5, 6]
[1, 2, 3, 4, 10, 12, 5, 6]
3 [2, 4, 3, 5, 9, 8, 7]
3 [2, 3, 4, 5, 7, 8, 9]
5 7
2 1