📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    In a Selection Sort algorithm, the inner loop finds the

    index of the minimum element in the unsorted part. Which line correctly completes the if condition to update min_idx? def selection_sort(arr):     n = len(arr)     for i in range(n - 1):         min_idx = i         for j in range(i + 1, n):             if __________: # Line to complete                 min_idx = j         arr[i], arr[min_idx] = arr[min_idx], arr[i]
    A arr[j] < arr[min_idx] Correct Answer Incorrect Answer
    B arr[j] > arr[min_idx] Correct Answer Incorrect Answer
    C j < min_idx Correct Answer Incorrect Answer
    D arr[j] == arr[min_idx] Correct Answer Incorrect Answer
    E arr[i] < arr[j] Correct Answer Incorrect Answer

    Solution

    Correct Answer: A • Code Analysis: o min_idx initially holds the index of the current assumed minimum (which is i). o The inner loop (for j in range(i + 1, n)) iterates through the rest of the unsorted array. o The if condition needs to check if the element at the current index j is smaller than the element currently considered the minimum (at min_idx). If it is, then j becomes the new min_idx. • Explanation of Correct Answer (A): arr[j] < arr[min_idx] o This condition directly compares the value at the current index j with the value at the min_idx found so far. If arr[j] is indeed smaller, it means we've found a new minimum, and min_idx should be updated to j. This is the core logic for finding the minimum element.

    Practice Next
    More IT Operating System Questions
    ask-question