😚 버블정렬
🙂문제
🙂풀이
public int[] solution(int n, int[] arr){
for(int i=0;i<n-1;i++){
if(arr[j] > arr[j+1]){
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
🙂소스코드
package algolecture;
import java.util.Scanner;
public class Main45 {
public int[] solution(int n , int[] arr){
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;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) {
Main45 T = new Main45();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<arr.length;i++)
arr[i] = sc.nextInt();
for(int x : T.solution(n,arr))
System.out.print(x + " ");
}
}