Skip to content
Onsite

Selection Sort

EasyP2Coding

Sort an array of integers in ascending order using selection sort and return the sorted array.

Selection sort grows a sorted prefix: on each step it finds the minimum of the unsorted suffix and swaps it into the next position of the prefix. Write the algorithm yourself instead of calling a built-in sort.

Example 1

Input
nums = [64,25,12,22,11]
Output
[11,12,22,25,64]

Example 2

Input
nums = [4,4,1]
Output
[1,4,4]

Example 3

Input
nums = [7]
Output
[7]

Constraints

  • 0 <= nums.length <= 1000
  • -10^4 <= nums[i] <= 10^4

Hints

Run checks the 3 visible tests, Submit all 9.