Assess your skills in designing sorting algorithms in pseudocode by answering questions focused on logic, flow, and key concepts. This quiz aims to strengthen your understanding of algorithm structure, efficiency, and common pitfalls in sorting algorithm design.
In the following pseudocode for a sorting algorithm, which statement correctly swaps two elements 'A[i]' and 'A[j]' without losing data: A[i] = A[j], A[j] = A[i], temp = A[i]; A[i] = A[j]; A[j] = temp; A[i] = temp; or A[i] = A[j] + A[i];?
Explanation: The correct answer uses a temporary variable to store the value of A[i] before overwriting it, ensuring no data is lost during the swap. The first distractor incorrectly tries to swap the variables by direct assignment, which will lose the original value of A[i]. The second distractor only assigns the value of 'temp' to A[i] without completing the swap. The third distractor's addition approach does not work universally and may lead to incorrect data.
Which pseudocode sorting algorithm is typically easiest to implement and understand for small datasets, using repeated comparison of adjacent elements and swapping them if they are out of order?
Explanation: Bubble Sort is known for its simplicity and is usually chosen for educational examples due to its straightforward logic of comparing and swapping adjacent values. 'Quick Short' is a typo and does not refer to an actual algorithm. 'Binary Sort' is not a standard sorting algorithm. 'Bucket Sort' is more complex and suited for specific data types or large data distributions.
In pseudocode for a selection sort, what is the primary purpose of the inner loop that runs from the current position+1 to the end of the list?
Explanation: The inner loop in selection sort checks each unsorted element to find the minimum value to swap into the current position. Counting elements is unrelated to sorting here. Insertion happens after selection, not during this loop. Reversing the list is not the function of this loop in selection sort.
When designing a while loop for insertion sort in pseudocode, which condition correctly continues moving an element left as long as it is smaller than the previous element and the index is greater than zero?
Explanation: The correct answer ensures the loop only runs when 'j' is above zero and the current element is smaller than the previous one, enabling proper insertion. The first distractor incorrectly uses 'or', which could cause out-of-bounds errors. The second uses the wrong comparison and continues in the wrong direction. The third combines conditions incorrectly, failing for standard insertion sort behavior.
Given the pseudocode: 'for i from 1 to n-1: for j from i+1 to n: if A[j] u003C A[i] then swap A[j] and A[i] end if', which sorting algorithm does this most closely resemble, and what is its primary flaw?
Explanation: The pseudocode closely imitates selection sort, but its flaw is swapping every time a smaller element is found instead of waiting to find the overall smallest before a single swap. Bubble Sort relies on adjacent comparisons, not the described pattern. Merge Sort would require splitting and merging, which are missing. Quick Sort depends on partitioning, not evident here.