Continue learning
Practice related DSA questions in the same topic.
Return the input sorted from smallest to largest by selecting the remaining minimum.
Return the input sorted from smallest to largest by selecting the remaining minimum.
def solve(values, target): a = values.copy() i = 0 while ((i + 1) < len(a)): smallest = i j = (i + 1) while (j < len(a)): if (a[j] < a[smallest]): smallest = j j = (j + 1) if (smallest != i): a[i], a[smallest] = a[smallest], a[i] i = (i + 1) return aCompare candidate 1 with minimum position 0.
Compare candidate 1 with minimum position 0.
Select the shortest unlined-up student for each new place.
After each pass the prefix is final. Search the unplaced suffix for its smallest value.
O(n²) time · O(n) returned copy, O(1) auxiliary auxiliary space
Worked example · {"values":[5,1,4,2],"target":0} ⇒ [1,2,4,5]
Edge case 1 · {"values":[],"target":0} ⇒ []
Edge case 2 · {"values":[3,3],"target":0} ⇒ [3,3]
Swapping immediately whenever a smaller item appears wastes writes.
Why can the minimum swap disturb stability?