Bubble Sort

Problem

Write a program that sorts in ascending order when N numbers are input and outputs them.
The sorting method is bubble 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, j = 0; i < arr.length - 1; i++) {
		if (arr[i] > arr[i + 1]) {
			[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]];
			i = 0;
		}
	}

	return answer;
}
function solution(arr) {
	let answer = arr;

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

	return answer;
}