<.Special Sort>
: 음의 정수는 앞쪽에 양의 정수는 뒷쪽에 있어야 한다. 또한 양의 정수와 음의 정수에는 변함이 없어야 한다.
- 앞에서 배운 버블정렬로 하면 된다.
- 아예 종속되지 않은 다른 두 개의 for문을 만들어서, 첫 번째 for문엔 음일 경우에 push, 두 번째 for문엔 양일 경우에 push해서 정렬해도 된다.
<script> function solution(arr){ let answer=arr; for(let i=0; i<arr.length-1; i++){ for(let j=0; j<arr.length-i-1; j++){ if(arr[j]>0 && arr[j+1]<0){ [arr[j], arr[j+1]]=[arr[j+1], arr[j]]; } } } return answer; } let arr=[1, 2, 3, -3, -2, 5, 6, -6]; console.log(solution(arr)); </script>
<script> function solution(arr){ let answer=[]; for(let x of arr){ if(x<0) answer.push(x); } for(let x of arr){ if(x>0) answer.push(x); } return answer; } let arr=[1, 2, 3, -3, -2, 5, 6, -6]; console.log(solution(arr)); </script>