버블 정렬 알고리즘

public class Main {
    public static void main(String[] args) {
        int[] rotto = {7, 44, 16, 32, 1, 22};

        int temp = 0;
        for( int i = 0; i < rotto.length-1 ; i++) {
            for( int j = 0; j <rotto.length-(i+1) ; j++) {
                if(rotto[j] > rotto[j+1]) {
                    temp = rotto[j];
                    rotto[j] = rotto[j+1];
                    rotto[j+1] = temp;
                }
            }
        }

        for(int rot: rotto) {
            System.out.print(rot);
            System.out.print(' ');
        }
        // 1 7 16 22 32 44 
    }
}

 

API를 이용한 정렬

자바가 제공하는 유틸리티 클래스를 이용하여 좀 더 쉽게 정렬을 처리할 수 있다. java.util 이라는 패키지에 Arrays라는 클래스를 이용하면 다음과 같이 정렬 작업이 매우 간단하게 처리된다.

public class Main {
    public static void main(String[] args) {
        int[] rotto = {7, 44, 16, 32, 1, 22};
        java.util.Arrays.sort(rotto);

        for(int rot: rotto) {
            System.out.print(rot);
            System.out.print(' ');
        }
        // 1 7 16 22 32 44
    }
}

 

728x90

'Language > JAVA' 카테고리의 다른 글

[JAVA] 클래스 Class (2)  (0) 2023.10.18
[JAVA] 클래스 Class (1)  (0) 2023.10.18
[JAVA] 참조 변수와 null  (0) 2023.10.18
[JAVA] 배열  (0) 2023.10.18
[JAVA] 이름이 있는 break  (0) 2023.10.18

https://www.acmicpc.net/problem/1427

 

1427번: 소트인사이드

첫째 줄에 정렬하려고 하는 수 N이 주어진다. N은 1,000,000,000보다 작거나 같은 자연수이다.

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim();

const arr = input.split("").map(i =>  Number(i));

arr.sort(function(a,b) {
    return b - a;
});

console.log(arr.join(""));
728x90

문제 설명

배열 array의 i번째 숫자부터 j번째 숫자까지 자르고 정렬했을 때, k번째에 있는 수를 구하려 합니다.

예를 들어 array가 [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3이라면

  1. array의 2번째부터 5번째까지 자르면 [5, 2, 6, 3]입니다.
  2. 1에서 나온 배열을 정렬하면 [2, 3, 5, 6]입니다.
  3. 2에서 나온 배열의 3번째 숫자는 5입니다.

배열 array, [i, j, k]를 원소로 가진 2차원 배열 commands가 매개변수로 주어질 때, commands의 모든 원소에 대해 앞서 설명한 연산을 적용했을 때 나온 결과를 배열에 담아 return 하도록 solution 함수를 작성해주세요.

제한사항
  • array의 길이는 1 이상 100 이하입니다.
  • array의 각 원소는 1 이상 100 이하입니다.
  • commands의 길이는 1 이상 50 이하입니다.
  • commands의 각 원소는 길이가 3입니다.
입출력 예

입출력 예 설명

[1, 5, 2, 6, 3, 7, 4]를 2번째부터 5번째까지 자른 후 정렬합니다. [2, 3, 5, 6]의 세 번째 숫자는 5입니다.
[1, 5, 2, 6, 3, 7, 4]를 4번째부터 4번째까지 자른 후 정렬합니다. [6]의 첫 번째 숫자는 6입니다.
[1, 5, 2, 6, 3, 7, 4]를 1번째부터 7번째까지 자릅니다. [1, 2, 3, 4, 5, 6, 7]의 세 번째 숫자는 3입니다.

 


내 코드

function solution(array, commands) {
    const answer = [];
    for(let idx = 0 ; idx < commands.length ; idx++){
        let temp = array;
        let i = commands[idx][0];
        let j = commands[idx][1];
        let k = commands[idx][2];

        temp = temp.slice(i-1,j);
        temp.sort(function(a,b){
            return a-b;
        });    
        
        answer.push(temp[k-1]);
    }
    
    return answer;
}
728x90

프로그래머스 - 완전탐색 Lv1

모의고사 ( _JS )

문제 설명

 

제한 조건

 

입출력 예시

 

https://programmers.co.kr/learn/courses/30/lessons/42840?language=javascript 

 

코딩테스트 연습 - 모의고사

수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다. 1번 수포자가 찍는

programmers.co.kr


코드

2022.01.11.

function solution(answers) {
    var answer = [];
    
    const answer1 = [ 1,2,3,4,5 ];
    const answer2 = [ 2,1,2,3,2,4,2,5];
    const answer3 = [ 3,3,1,1,2,2,4,4,5,5 ];
    
    const Correct1 = compare(answers, answer1);
    const Correct2 = compare(answers, answer2);
    const Correct3 = compare(answers, answer3);

    const CorrectArray = [ Correct1, Correct2, Correct3 ];
    
    const correctMax = Math.max(...CorrectArray);
    
    CorrectArray.forEach(function(answers, index){
        if( correctMax === answers ){
            answer.push(index+1);
        }         
    });
    
    return answer;
}

function compare(answers, studentAnswer){
  
    let correct = 0;
    
    answers.forEach(function(answer, index){
        index = index % studentAnswer.length
        if( answer === studentAnswer[index]){
            correct++;
        }
    
    });
    return correct;
};

코드 설명

728x90

+ Recent posts