[코테6_2]버블정렬

byeol·2022년 12월 19일
0

코딩테스트

목록 보기
38/42

✔ 내 답 -> 정답->강의랑 답 같음


그림 출처: https://gmlwjd9405.github.io/2018/05/06/algorithm-bubble-sort.html

import java.util.*;

public class Main{
 public static int[] solution(int n, int[] arr){
   for(int i=0;i<=n-2;i++) {
         for(int j=0;j<=n-2-i;j++){
            if(arr[j]>arr[j+1]) {
                   int tmp = arr[j];
                   arr[j]=arr[j+1];
                   arr[j+1]=tmp;
                     }           
                 } 
	   }
   
   return arr;	 
 }

 public static void main(String[] args){
   Scanner kb = new Scanner(System.in);
   int n = kb.nextInt();
   int[] arr = new int[n];
   for(int i=0;i<n;i++) arr[i]=kb.nextInt();
   for(int x : solution(n,arr)) System.out.print(x+" ");  
 }
}

profile
꾸준하게 Ready, Set, Go!

0개의 댓글