Data Structure 6 Flashcards

1
Q

What is the logical last step in an algorithm that averages the high temperatures for 10 days and displays the average high temperature?

A

Printing the temperature

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

What is the output of the pseudocode below if the variables declared in the main program are global?
Main
Declare X as Integer, Y as Integer
Set X = 1
Set Y = 2
Call Sub(X, Y)
Write X
Write Y
End Program

Subprogram Sub(Integer Num1, Integer Num2 as Reference)
Declare X as Integer
Set Num1 = 3
Set Num2 = 4
Set X = 5
Write X
End Subprogram

A

5
14

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

How many times in this pseudocode is the function F called?
Main
Declare K as Integer
K = 3
Set Result = F(K)
Write Result
End Program

Function F(N) as Integer
If N == 1 Then
Set F = 1
Else
Set F = N * F(N - 1)
Set N = N - 1
End If
End Function

A

3

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

What is displayed in Step 5 if A = 15 and B = 5 in the pseudocode below?

Step 1: Start
Step 2: Read A, B
Step 3: C= A*B
Step 4: D=A/B
Step5: Print C
Step 6: Stop

A

75

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

What is displayed in step 3 if midterm = 60 and final = 65 in this pseudocode?

Step 1: Declare midterm, final as integer
Step 2: average = (midterm+final)/2
Step 3: if (average < 50) then Print “Fail” Else Print “Pass”
endif

A

Pass

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

How many times will count++ execute when i = 3, in this pseudocode?

int count = 0;
int N = 4;
for (int i = 0; i < N; i++)
for (int j = 0; j < i; j++)
count++;

A

3

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

At the end of obj, what is the time complexity of inserting in this pseudocode?

void DynamicArrayAppend(DynamicArray obj, const void input) {
if (obj->logicalSize == obj->capacity - 1) {
obj->capacity *= 2;
obj->internalArray = realloc(obj->internalArray, obj->capacity * obj->itemSize);
}
obj->logicalSize += 1; DynamicArraySet(obj, obj->logicalSize - 1, input);
}

DynamicArray *obj = DynamicArrayCreate(sizeof(int));for (int i = 0; i < n; i++) {
int number = rand() % 10; DynamicArrayAppend(obj, &number);
}

A

O(1) or O(n)

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

What is the time complexity of this pseudocode?
double sumCol(double table[][], int numRows, int numCols, int col)
{
double cSum = 0;
for (int row = 0; row < numRows; row++)
{
cSum += table[row][col];
}
return cSum;
}

A

O(n)

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

What is the time complexity of this pseudocode?

Algorithm Algo1(A)
Input: An array A storing n ≥ 1 integers
Output: The sum of the elements in A

s=A[1]
for i=1 to n do
s=s+A[i]
return s

A

O(n)

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

What is the time complexity of this pseudocode?

Algorithm Algo3(A, B)
Input: Arrays A and B, each of them storing n≥1 integers
Output: Count array B[i], where B[i] equals the sum of A[1] to A[i], i=1 to n
c=0
for i=1 to n do
for j=1 to n do
s=A[1]
for k=1 to j do
s=s+A[k]
if B[i]=s then
c=c+1
return c

A

O(n^3)

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

What is an attribute of a bubble sort algorithm?

A

Ideal for small number of n

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

What is a characteristic of quick sort?

A

Recursively breaks down a problem into two or more subproblems of the same or related type

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

Which Big-O notation represents the time complexity of a bubble sort?

A

O(n^2)

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

What is the typical run time for an insertion sort?

A

O(n^2)

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

A large set of floating point numbers that are in range from 0.0 to 1.0 and are uniformly distributed across the range need to be sorted.

Which sort procedure is useful when the input is uniformly distributed over the range?

A

Bucket

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

How many buckets are needed when sorting 13 numbers that have 15 digits each, using the radix-sort algorithm?

A

10

17
Q

Four words were added to an initially empty linked list in the following order: orange, carrot, banana, and apple.

Which word is at the beginning of the list?

A

“orange”

18
Q

Which type of sorting algorithm is demonstrated in this pseudocode?

for i from 0 to N - 1
if a[i] > a[i + 1]
swap( a[i], a[i + 1])
end

A

Bubble

19
Q

Which type of sorting algorithm is demonstrated in this pseudocode?

def shortSort(alist):
exchanges = True
passnum = len(alist)-1
while passnum > 0 and exchanges:
exchanges = False
for i in range(passnum):
if alist[i]>alist[i+1]:
exchanges = True
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
passnum = passnum-1

A

Bubble

20
Q

Which type of sorting algorithm is demonstrated in this code?

int partition( void *a, int low, int high )
{
int left, right;
void *pivot_item;
pivot_item = a[low];
pivot = left = low;
right = high;
while ( left < right ) {
/ Move left while item < pivot /
while( a[left] <= pivot_item ) left++;
/ Move right while item > pivot /
while( a[right] > pivot_item ) right–;
if ( left < right ) SWAP(a,left,right);
}
/ right is final position for the pivot /
a[low] = a[right];
a[right] = pivot_item;
return right;
}

A

Quick