seunghyun Note
[프로그래머스] k번째수 with JS 본문
728x90
반응형
링크 : https://school.programmers.co.kr/learn/courses/30/lessons/42748
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
2024.01.05 - [스터디/알고리즘 & 자료구조] - 알고리즘 & 자료구조 8주차 스터디 [sort]
알고리즘 & 자료구조 8주차 스터디 [sort]
May 27, 2023 🐰 🤯 오늘 배울 개념은 SortSort!!!! 재밌는것. : bubble pop, insertion, selection, 앞으로 배울 sorting들의 실시간 변화를 볼 수 있다. sort Algorithmes는 무엇일까? 정렬 알고리즘은 컬렉션의 항목을
cojjangsh.tistory.com
sort를 공부하면서 관련 문제를 찾아 봤다 ㅎㅎ!!
문제 풀이
✨ Split and save New arrays! (Main)
function solution(array, commands) {
var answer = [];
for (let i = 0; i < commands.length; i++) {
let start = commands[i][0] - 1;
let end = commands[i][1];
// slice 를 통해 새로운 배열에 저장시킨다.
let tmp_array = array.slice(start, end);
//새롭게 분할한 배열들을 sorting function에 넣기
/*-------------------------------------------*/
//use Bubble Sort!
Bubblesort(tmp_array);
/*-------------------------------------------*/
//use Insertion Sort!
InsertionSort(tmp_array);
/*-------------------------------------------*/
//use Selection Sort!
selectionSort(tmp_array);
/*-------------------------------------------*/
// console.log(tmp_array);
// 정렬된 데이터에 key 값에 맞게 push 후 반환하기.
answer.push(tmp_array[commands[i][2] - 1]);
}
return answer;
}
🫧 bubble sort function
//Bubble Sort!
function Bubblesort(array) {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length - 1; j++) {
if (array[j] > array[j + 1]) {
swapFunction(array, j, j + 1);
}
}
}
}
🤖 insertion sort function
// Insertion Sort!
function InsertionSort(array) {
var currentVal;
for (var i = 1; i < array.length; i++) {
currentVal = array[i];
for (var j = i - 1; j >= 0 && array[j] > currentVal; j--) {
array[j + 1] = array[j];
}
array[j + 1] = currentVal;
}
}
👉🏻 selection sort function
// Selection Sort!
function selectionSort(arr) {
for (let i = 0; i < arr.length; i++) {
var min = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
swapFunction(arr, i, min);
}
return arr;
}
🥷🏻 swap function
function swapFunction(arr, idx1, idx2) {
var temp = arr[idx1];
arr[idx1] = arr[idx2];
arr[idx2] = temp;
}
🎁 최종 소스 코드
// swap function
function swapFunction(arr, idx1, idx2) {
var temp = arr[idx1];
arr[idx1] = arr[idx2];
arr[idx2] = temp;
}
//Bubble Sort!
function Bubblesort(array) {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length - 1; j++) {
if (array[j] > array[j + 1]) {
swapFunction(array, j, j + 1);
}
}
}
}
// Insertion Sort!
function InsertionSort(array) {
var currentVal;
for (var i = 1; i < array.length; i++) {
currentVal = array[i];
for (var j = i - 1; j >= 0 && array[j] > currentVal; j--) {
array[j + 1] = array[j];
}
array[j + 1] = currentVal;
}
}
// Selection Sort!
function selectionSort(arr) {
for (let i = 0; i < arr.length; i++) {
var min = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
swapFunction(arr, i, min);
}
return arr;
}
function solution(array, commands) {
var answer = [];
for (let i = 0; i < commands.length; i++) {
let start = commands[i][0] - 1;
let end = commands[i][1];
// slice 를 통해 새로운 배열에 저장시킨다.
let tmp_array = array.slice(start, end);
// console.log(tmp_array);
/*-------------------------------------------*/
//use Bubble Sort!
Bubblesort(tmp_array);
/*-------------------------------------------*/
//use Insertion Sort!
InsertionSort(tmp_array);
/*-------------------------------------------*/
//use Selection Sort!
selectionSort(tmp_array);
/*-------------------------------------------*/
// console.log(tmp_array);
answer.push(tmp_array[commands[i][2] - 1]);
}
return answer;
}
가끔씩은.. 이렇게 정렬 공부하는 것도 좋은거 같다!
728x90
반응형
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] - 핸드폰 번호 가리기 with JS (0) | 2024.01.09 |
---|---|
[프로그래머스] - 가장 큰 수 with JS (0) | 2024.01.06 |
[프로그래머스] 피보나치 수 with JS (1) | 2024.01.06 |
[프로그래머스] 하샤드 수 with JS (0) | 2024.01.06 |
[프로그래머스] H-Index with JS (0) | 2024.01.06 |