-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBubble_Sort.java
More file actions
77 lines (49 loc) · 1.43 KB
/
Bubble_Sort.java
File metadata and controls
77 lines (49 loc) · 1.43 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package sort.bubbleSort;
//재귀를 이용한 버블 정렬 구현
public class Bubble_Sort {
//bubbleSort: 버블정렬 메서드
private static void bubbleSort(int[] arr) {
//재귀함수 호출
//arr: 배열
//arr.length-1: 배열의 마지막 인덱스
bubbleSort(arr, arr.length-1);
}//bubbleSort 종료
//bubbleSort: 버블 정렬 메서드의 재귀함수
private static void bubbleSort(int[] arr, int last) {
//마지막 인덱스가 0보다 클 때까지
if(last > 0) {
//마지막 인덱스까지 배열방을 돈다.
for(int i=1; i <= last; i++) {
//내 앞쪽에 있는 것이 나보다 클때
if(arr[i-1] > arr[i]) {
//자리 바꾸기
swap(arr, i-1, i);
}
}
//마지막 인덱스를 제외하고 다시 버블 정렬 재귀 호출
bubbleSort(arr, last-1);
}
}//bubbleSort 종료
//swap 메서드: 자리바꾸기(스왓)
private static void swap(int[] arr, int source, int target) {
int tmp = arr[source];
arr[source] = arr[target];
arr[target] = tmp;
}//swap 종료
//printArray: 배열 출력 메서드
private static void printArray(int[] arr) {
for (int data : arr) {
System.out.print(data + ",");
}
System.out.println();
}//printArray 종료
//main 실행 함수
public static void main(String[] args) {
int[] arr = {3,5,4,2,1};
System.out.println("===========정렬 전============");
printArray(arr);
System.out.println("===========정렬 후============");
bubbleSort(arr);
printArray(arr);
}//main 종료
}//Bubble_Sort 종료