일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- Android
- 예외처리
- Firebase
- xocde
- 버블정렬
- 백준
- deeplink
- 연결리스트
- text to speech
- IOS
- Swift
- Android Studio
- 준코딩
- FLUTTER
- 플러터
- 커스텀팝업
- C언어
- customPopup
- storyboard
- 안드로이드
- Xcode
- android java
- label
- 링크드리스트
- 자바
- TextField
- BAEKJOON
- 안드로이드스튜디오
- 보호와 보안
- swift baekjoon
- Today
- Total
준코딩
버블정렬코드 (스택, 큐 , 정렬) 본문
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
int[] arr = new int[5];
int num = 0;
int i = 0;
Scanner scan = new Scanner(System.in);
System.out.println("공백입력불가");
for (i = 0; i < arr.length; i++) {
while (true) {
System.out.print((i + 1) + "번째 숫자 입력: ");
try {
arr[i] = Integer.parseInt(scan.nextLine());
} catch (NumberFormatException e) {
System.out.println("오류가 발생했습니다.");
continue;
} catch (Exception e) {
System.out.println("예기치 못한 오류 발생");
}
break;
}
}
while (true) {
System.out.println("1.스택 2.큐 3.정렬");
try {
num = Integer.parseInt(scan.nextLine());
if (num > 3 || num <= 0) {
System.out.println("1~3 사이의 정수만 입력가능합니다");
continue;
}
} catch (NumberFormatException e) {
System.out.println("오류가 발생했습니다.");
continue;
} catch (Exception e) {
System.out.println("예기치 못한 오류 발생");
}
break;
}
if (num == 1) {
System.out.println("스택");
for (i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
} else if (num == 2) {
System.out.println("큐");
output(arr, i);
}
else {
System.out.println("정렬");
System.out.print("정렬전 데이터:");
output(arr, i);
System.out.println();
System.out.print("정렬후 데이터:");
for (i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
output(arr, i);
}
}
public static void output(int arr[], int i) {
for (i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
'프로그래밍 > 자바' 카테고리의 다른 글
중앙값 구하기 (0) | 2019.01.09 |
---|---|
최대값 구하기 (0) | 2019.01.09 |
BubbleSort 버블정렬 (0) | 2018.12.10 |
예외처리 (0) | 2018.11.29 |
객체지향 프로그래밍-2 (0) | 2018.11.29 |