Javascript/프로그래머스Lv0
중앙값 구하기
정호나
2024. 6. 7. 16:38
sort함수 쓰지 않고 코딩구현하는 연습
//1. 정렬
//1-1 배열에서 제일 작은 값
//1-2 찾으면 새 배열에 넣어라
//1-3 원래 배열에 넣은 것 지우기
//1-4 만약 원래 배역 길이만큼 반복했으면 1-1로 돌아가기
//2. 가운데 값 꺼내기
// input [9, -1, 0 ]
function solution(array) {
let arrayCnt = 0;
let newArray = [] ;
while(arrayCnt < array.length){
let minNumber = 1000;
//1-1
let cnt = 0;
while (cnt < array.length)
{
if (minNumber > array[cnt]){
minNumber = array[cnt];
}
cnt++;
}
//1-2
newArray.push(minNumber);
//1-3
let cnt2 = 0;
while(cnt2 < array.length){
if (minNumber === array[cnt2])
{
array[cnt2] = 1000;
break;
}
cnt2++;
}
arrayCnt ++;
}
console.log("New Array : ", newArray);
//2. 가운데 값 꺼내기
return newArray[Math.floor(array.length / 2)];
}
}