버블정렬 알고리즘

·2023년 5월 5일

자바 알고리즘

목록 보기
1/2
import java.util.Scanner;
public class Kimjin_bubble_sort{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n=0;
        int k=0;
        
        n=sc.nextInt();
        k=sc.nextInt();
        int[] number = new int[n];
        //배열입력
        for(int i=0; i<number.length; i++){
            number[i]=sc.nextInt();
        }
        sc.close();
        int min=0;
        int count=0;
        for(int i=0; i<number.length; i++){
            for(int j=0; j<number.length-1; j++){
                if(number[j]>number[j+1]){
                    min=number[j+1];
                    number[j+1]=number[j];
                    number[j]=min;
                    count++;
                }
                if(k==count){
                    System.out.printf("%d %d", number[j], number[j+1]);
                    return;
                }
            }
        }
        // for(int i=0; i<number.length; i++){
        //     for(int j=0; j<i; j++){
        //         if(number[j]>number[j+1]){
        //             min=number[j+1];
        //             number[j+1]=number[j];
        //             number[j]=min;
        //             count++;
        //         }
        //         if(k==count){
        //             System.out.printf("%d %d", number[j], number[j+1]);
        //             return;
        //         }
        //     }
        // }
        System.out.println(-1);
    }
}



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

//문제 해석 : swap이 일어나지 않은 루프 알아내기
// == for문으로 A배열에 버블정렬을 한 횟수
// ex) A = {10, 1, 5, 2, 3}
// 0 >> A = {10, 1, 5, 2, 3}
// 1 >> A = {1, 5, 2, 3, 10}
// 2 >> A = {1, 2, 3, 5, 10}
//배열의 시작을 1부터 라고 했으므로 +1   =>> 3 출력

public class Main{
    public static void main(String[] args) throws IOException {
        //N이 500,000이하이고, A배열의 값의 범위가 1,000,000 이하이므로, 일반적인
				//방식으로 구하면 시간초과가 뜬다. ( => 버퍼사용 )
				BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
				//mData 클래스 타입의 배열 A를 생성한다. >> 입력
        mData[] A = new mData[N];
        for(int i=0; i<N; i++) {
        	A[i] = new mData(Integer.parseInt(br.readLine()), i);
        }
				//정렬한다.
        Arrays.sort(A);
				//정렬 전 index - 정렬 된 후 index 값을 뺀다.

				//ex) A = {10, 1, 5, 2, 3}   (before)
        //         0   1  2  3  4

				//         1, 2, 3, 5, 10  (인덱스의 순서는 이대로 고정...)

				//         1, 3, 4, 2, 0   (정렬 전 인덱스)
				//         0, 1, 2, 3, 4   (정렬 후 인덱스)
				//         1, 2, 2,-1,-4   (뺄셈 결과)
				// 이 중 가장 큰 값(2) + 1  = 3 (result)
        int max = 0;
        for(int i=0; i<N; i++) {
        	if(max<A[i].index-i) {
        		max = A[i].index-i;
        	}
        }
        System.out.println(max+1);
    }
}
//mData라는 class 생성. compareTo 메소드를 사용하기 위해 Comparable 인터페이스를 상속함.
//=> String, Date, Time 타입 정렬가능 ...
//value랑 index로 이루어진 세트 ...
class mData implements Comparable<mData>{
	int value;
	int index;
	//오버로딩 생성자
	public mData(int value, int index) {
		super();
		this.value = value;
		this.index = index;
	}
	@Override
	public int compareTo(mData o) {
		return this.value - o.value;
	}
}
profile
이것저것 공부하기

0개의 댓글