Sorting

Sorting

5 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