JS 100제 문제 50 버블정렬 구현하기

이민정·2021년 4월 11일
0

JS100제

목록 보기
39/66


<풀이 코드>

function bubble(arr) {
	  let result = arr.slice(); 
	
	  for (let i = 0; i < result.length - 1; i++) {
	    for (let j = 0; j< result.length -i; j++) {
	      if (result[j] > result[j + 1]) {
	         var n = result[j];
	    	  result[j] = result[j + 1];
	    	  result[j+1] = n;
	      }
	    }
	  }
	  
	  return result;
	}

	const items = prompt('입력해주세요.').split(' ').map((n) => {
	  return parseInt(n, 10);
	});

	console.log(bubble(items));
profile
공부하자~!

0개의 댓글