Selection Sort

Problem

Write a program that sorts in ascending order when N numbers are input and outputs them.
The sorting method is selection sort.

input

  • The first line is given a natural number N (1<=N<=100)
  • In the second line, N natural numbers are entered with a space between them. Each natural number is in the range of integers
6

13 5 11 7 23 15

output

Prints a sequence sorted in ascending order.

5 7 11 13 15 23

Solution

function solution(arr) {
	let answer = arr;

	for (let i = 0; i < arr.length - 1; i++) {
		let idx = i;
		for (let j = i + 1; j < arr.length; j++) {
			if (arr[j] < arr[idx]) idx = j;
		}

		[arr[i], arr[idx]] = [arr[idx], arr[i]];
	}

	return answer;
}