selectionSort

Hard · Array Manipulations · 1 test cases

Implement the selection sort algorithm to sort an array of numbers in ascending order.

Examples

selectionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]) → [1, 1, 2, 2, 4, 8, 32, 43, 43, 55, 63, 92, 123, 123, 234, 345, 5643]

Starter code

function selectionSort(arr) {

}

Complexity of the optimal solution

Time O(n²), space O(1). Selection sort iterates with a main loop (n times) and for each iteration, it uses a nested loop to find the minimum element in the unsorted part. This results in a time complexity of O(n²). The algorithm sorts the array in-place, using constant extra space.

Solve selectionSort in the browser