AlgoFrames

Implement selection sort

Standard · Write code · about 10 min

Sort nums in ascending order with selection sort and return it. Each pass finds the smallest unsorted value and swaps it into the first unsorted position.

Example 1

Input:

[[4,2,5,1]]

Output:

[1,2,4,5]

Example 2

Input:

[[3,1,2]]

Output:

[1,2,3]

Constraints

  • 0 ≤ nums.length ≤ 1,000
  • -1,000 ≤ nums[i] ≤ 1,000

Learn the idea first: Selection sort, a visual lesson.

More Selection sort problems