package datastructure.chap06.selection;
import java.util.Arrays;
import java.util.Scanner;
public class 소트_인사이드_1427 {
public static void sort(int[] arr) {
// swap 대상을 타겟팅하는 루프
for (int i = 0; i < arr.length - 1; i++) {
// 최소값 탐색
int max = i;
for (int j = i+1; j < arr.length; j++) {
if(arr[j] > arr[max]) {
max = j; // 최댓값 인덱스 갱신
}
}
// swap
int temp = arr[i] ;
arr[i] = arr[max] ;
arr[max] = temp;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
int arr[] = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
arr[i] = Integer.parseInt(""+str.charAt(i));
}
sort(arr); // 선택정렬
for (int i : arr) {
System.out.print(i);
}
}
}
=== 선생님 풀이===
package datastructure.chap06.selection;
import java.util.Scanner;
// 백준 1427
public class 선택정렬_문제01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
int[] A = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
A[i] = Integer.parseInt("" + str.charAt(i));
}
for (int i = 0; i < A.length - 1; i++) {
int max = i;
for (int j = i + 1; j < A.length; j++) {
if (A[j] > A[max]) {
max = j;
}
}
int temp = A[i];
A[i] = A[max];
A[max] = temp;
}
for (int i = 0; i < A.length; i++) {
System.out.print(A[i]);
}
}
}
'코딩테스트(연습)' 카테고리의 다른 글
22.06.13_백준_오큰수_17298 (버퍼드리더활용) (0) | 2022.06.13 |
---|---|
22.06.13(백준_ATM_11399) (0) | 2022.06.13 |
22.06.13(백준_수정렬하기_2750번) (0) | 2022.06.13 |
java_자료구조(QUEUE)문제_백준2164_22.06.09(day13) (0) | 2022.06.09 |
java_자료구조(stack)문제 (백준 1874)_22.06.09(day13) (0) | 2022.06.09 |