Sorting

Sorting

8 questions · Fundamental Engineering

Practice

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array indexes start at 1.

Counting Sort is a non-comparison-based sorting algorithm. It is suitable for sorting a collection of objects according to keys that are small positive integers by means of counting the occurrences of key values in all data and then using those counts to place the values in their correct sorted positions.
The function countSort below describes a simple variant of counting sort algorithm. The function countSort receives 2 arguments as follows: arr, the array of small positive integers to be sorted, and M, the max value for the range of the array (from 1 to M), and returns the sorted array. Element count[k] of local array count holds the frequency of key value k in the range 1 to M.

For instance, when the function countSort is called as countSort({3, 6, 1, 5, 3, 4, 5}, 6), the value of array count is {1, 0, 2, 1, 2, 1}, and the function returns {1, 3, 3, 4, 5, 5, 6}.

[Program]
○ integer []: countSort(integer []: arr, integer M)
integer []: count ← {M zeros}
integer: i, j, k
integer: n ← the number of elements of arr
integer []: result ← {n zeros}
// counting each element of arr
for (increase i from 1 to n by 1)
k ← arr[i]
count[k] ← count[k] + 1
endfor
// forming array result from each key value and its count
i ← 1
for (increase k from 1 to M by 1)
if (A)
for (increase j from 1 to count[k] by 1)
result[i] ← k
B
endfor
endif
endfor
return result // sorted array

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.

The bubble sort compares adjacent elements in an array and swaps them if they are out of order. It makes multiple passes through an array. The bubble sort can be modified to stop early if it finds that the array has become sorted. The function quickBubble is modified from conventional bubble sort, which sorts the elements in ascending order, to recognize a sorted array and stop early. For example, the sorting of the unordered array {18, 1, 8, 6, 2, 9, 12, 14, 7, 11} is completed in six passes.

[Program]
○ integer []: quickBubble(integer []: array)
integer []: arraySorted ← array
boolean: exchange
integer: maxIdx, i
exchange ← true
maxIdx ← (the number of elements in arraySorted) - 1
while (A and B)
exchange ← false
for (increase i from 1 to maxIdx by 1)
if (arraySorted[i] > arraySorted[i + 1])
exchange ← true
swap the values of arraySorted[i] and arraySorted[i + 1]
endif
endfor
maxIdx ← maxIdx - 1
endwhile
return arraySorted

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A in the description and B in the program. Here, the array index starts at 0.

The procedure sort receives an integer array arr and prints all the integers in arr in ascending order, separated by commas. The number of elements in arr is ≥ 1. The values of all array elements are in the range of 0-10.

If arr is {9, 3, 2, 0, 9, 3, 0, 1, 5, 3, 8}, at the end of the procedure, it outputs "0, 0, 1, 2, 3, 3, 3, 5, 8, 9, 9, " and the values of the elements of array s will be {A}

[Program]
○ sort(integer []: arr) // prints all the elements in arr in ascending order
integer []: s ← {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
integer: i, j
for (increase i from 0 to (the number of elements in arr) - 1 by 1)
s[arr[i]] ← s[arr[i]] + 1
endfor
for (increase i from 0 to 10 by 1)
if (s[i] > 0)
for (increase j from 0 to s[i] - 1 by 1)
output B and ", "
endfor
endif
endfor
return

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array indexes start at 1.

The procedure sort sorts an integer array containing certain number (≥ 2) of elements in ascending order.

[Program]
○ sort(integer []: arg)
Integer []: A ← arg
integer: i, k
for (increase i from 2 to the number of elements in A by 1)
k ← i
while (k > 1)
if (A[k - 1] ≤ A[k])
exit the while block
endif
A
B
endwhile
endfor
output A

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A through C in the program. Here, the array index starts at 1.

The program sorts the data in ascending order using the selection sort algorithm. The algorithm repeatedly selects the smallest element from the unsorted portion of the array and swaps it with the first element of the unsorted portion until the entire array is sorted.

[Program]
integer []: data ← {12, 11, 13, 5, 6}
integer: i, j, temp, minPos
integer: size ← the number of elements in data
for (increase i from 1 to (size - 1) by 1)
minPos ← i
for (increase j from A to size by 1)
if (data[j] B data[minPos])
minPos ← j
endif
endfor
temp ← C
C ← data[minPos]
data[minPos] ← temp
endfor

Answer group

OptionABC

From the answer group below, select the correct answer to be inserted into blank in the description. Here, the array index starts at 1.

When the function binSort is called as binSort(blank), the array of the return value contains no undefined elements, and its values are arranged in ascending order.

[Program]
○ integer []: binSort(integer []: data)
integer: n ← the number of elements in data
integer []: bins ← {n undefined values}
integer: i
for (increase i from 1 to n by 1)
bins[data[i]] ← data[i]
endfor
return bins

Answer group

From the answer group below, select the correct answer to be inserted into blank in the description. Here, the array index starts at 1.

The function merge receives the integer-type arrays data1 and data2, which are sorted in ascending order, and returns an integer-type array sorted in ascending order that is made by merging them.
When the function merge is called as merge({2, 3}, {1, 4}), the line /*** α ***/ is blank.

[Program]
○ integer []: merge(integer []: data1, integer []: data2)
integer: n1 ← the number of elements in data1
integer: n2 ← the number of elements in data2
integer []: work ← {(n1 + n2) undefined}
integer: i ← 1
integer: j ← 1
integer: k ← 1
while ((i ≤ n1) and (j ≤ n2))
if (data1[i] ≤ data2[j])
work[k] ← data1[i]
i ← i + 1
else
work[k] ← data2[j]
j ← j + 1
endif
k ← k + 1
endwhile
while (i ≤ n1)
work[k] ← data1[i]
i ← i + 1
k ← k + 1
endwhile
while (j ≤ n2)
work[k] ← data2[j] /*** α ***/
j ← j + 1
k ← k + 1
endwhile
return work

Answer group

From the answer group below, select the correct answer to be inserted into blank in the description. Here, the array index starts at 1.

The following procedure sort sorts the elements of the global integer-type array data, from the index given by the argument first to the index given by the argument last, in ascending order. Here, first < last. When the procedure sort is called as sort(1, 5), the output when the line /*** α ***/ is executed for the first time is “blank”.

[Program]
global: integer []: data ← {2, 1, 3, 5, 4}
○ sort(integer: first, integer: last)
integer: pivot, i, j
pivot ← data[the quotient of (first + last) ÷ 2]
i ← first
j ← last
while (true)
while (data[i] < pivot)
i ← i + 1
endwhile
while (pivot < data[j])
j ← j - 1
endwhile
if (i ≥ j)
terminate the iteration process
endif
swap the values of data[i] and data[j]
i ← i + 1
j ← j - 1
endwhile
output the values of all elements of data in the order of the indexes, separated by spaces /*** α ***/
if (first < i - 1)
sort(first, i - 1)
endif
if (j + 1 < last)
sort(j + 1, last)
endif

Answer group