Given below is the code for Selection sort procedure:
SELECTIONSORT( numList, n)
Step 1: SET i=0
Step 2: WHILE i< n REPEAT STEPS 3 to 11
Step 3: SET min = i, flag = 0
Step 4: _______________
Step 5: WHILE j<="" p="">
Step 6: ___________________________
Step 7: min=j
Step 8: __________
Step 9: IF flag=1 THEN
Step 10: swap(numList[i],numList[min])
Step 11: ________________________
Arrange the options given below in order to fill in the blanks in the above code.
A. SET i=i+1
B. SET j= i+1
C. IF numList[j] < numList[min] THEN
D. flag=1
Given below is the code for Selection sort procedure:
SELECTIONSORT( numList, n)
Step 1: SET i=0
Step 2: WHILE i< n REPEAT STEPS 3 to 11
Step 3: SET min = i, flag = 0
Step 4: _______________
Step 5: WHILE j<="" p="">
Step 6: ___________________________
Step 7: min=j
Step 8: __________
Step 9: IF flag=1 THEN
Step 10: swap(numList[i],numList[min])
Step 11: ________________________
Arrange the options given below in order to fill in the blanks in the above code.
A. SET i=i+1
B. SET j= i+1
C. IF numList[j] < numList[min] THEN
D. flag=1
Solution
Step 4: B. SET j = i+1, which starts the inner loop just after position i.
Step 6: C. IF numList[j] < numList[min] THEN, which compares the current element with the smallest found so far.
Step 8: D. flag=1, which records that a smaller element was found (Step 7 has already set min = j).
Step 11: A. SET i = i+1, which moves to the next position after the swap.
Step 9 swaps only when flag=1, so flag must be set inside the IF, after min=j. This is why D comes after C and not before it.
Related questions:
2026: 29 May Shift 2