Pseudocode for Common Algorithms Quiz Quiz

Challenge your understanding of pseudocode for popular algorithms, covering sorting, searching, and key programming concepts. Assess your ability to interpret and apply pseudocode as it relates to algorithmic problem-solving.

  1. Identifying Sorting Logic

    Given the following pseudocode: FOR i from 1 to N-1, IF arr[i] u003E arr[i+1], swap arr[i] and arr[i+1], END FOR. What sorting algorithm does this pseudocode represent?

    1. Merge Sort
    2. Insertion Sort
    3. Selection Sort
    4. Bubble Sort

    Explanation: This pseudocode reflects the core idea of Bubble Sort, where adjacent elements are compared and swapped to 'bubble' larger values towards the end. Selection Sort finds the minimum to place in each position, which is not shown. Insertion Sort builds a sorted section by shifting elements, while Merge Sort splits and merges arrays, neither of which matches the presented logic. Only Bubble Sort fits the repeated adjacent swapping shown.

  2. Tracing Linear Search

    In the pseudocode: FOR each element in list, IF element equals target, RETURN index, END FOR, RETURN -1. What will be returned if the target is not present in the list?

    1. length of list
    2. null
    3. 0
    4. -1

    Explanation: The correct answer is -1, which signals that the target was not found after checking all elements. Returning 0 could be confused with a valid index. Returning null depends on the language but is not specified here. Returning the length of the list is not standard and would be misleading. The pseudocode clearly specifies RETURN -1 in the absence of a found element.

  3. Algorithm Time Complexity Recognition

    If a pseudocode involves repeatedly dividing a sorted list in half to find a target, which algorithm does this best describe?

    1. Linear Search
    2. Bubble Search
    3. Quick Sort
    4. Binary Search

    Explanation: Binary Search is characterized by dividing the search space in half each time, especially in sorted lists. Linear Search does not divide lists; it checks elements one by one. Quick Sort also divides arrays but for sorting, not searching. ‘Bubble Search’ is not a recognized algorithm, possibly a mix-up with Bubble Sort. Binary Search is the only correct logic match.

  4. Pseudocode for Finding the Maximum

    Which pseudocode correctly finds the largest number in a list called nums?

    1. Set temp to 0; FOR i in nums, add i to temp if i is even
    2. Set max to length of nums; FOR i=0 to max, IF nums[i] u003C max THEN set max to nums[i]
    3. Set max to nums[0]; FOR each num in nums, IF num u003E max THEN set max to num
    4. Set min to 0; FOR each num in nums, IF num u003C min THEN set min to num

    Explanation: The correct pseudocode initializes a variable with the first list element and updates it if a larger value is found, ensuring the largest is found after one pass. The second option finds the minimum, not the maximum. The third option incorrectly uses indices and a confusing max update. The fourth is unrelated, as it sums even numbers rather than finding a maximum.

  5. Understanding Recursive Factorial

    Consider this pseudocode: IF n equals 0 THEN RETURN 1 ELSE RETURN n times factorial of n-1. What concept is demonstrated here?

    1. Recursion
    2. Sorting
    3. Selection
    4. Iteration

    Explanation: This pseudocode exemplifies recursion, where a function calls itself to solve a smaller instance of the same problem, as seen with factorials. Iteration involves looping, which is not shown here. Selection usually refers to conditional branching but lacks the self-calling aspect. Sorting relates to rearranging items, not recursive calculation. Only recursion fits the behavior described.