-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubble.java
More file actions
27 lines (24 loc) ยท 859 Bytes
/
Bubble.java
File metadata and controls
27 lines (24 loc) ยท 859 Bytes
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
package basic.sort.impl;
import basic.sort.Sort;
/*
๋ฒ๋ธ์ํธ
๋ง์น ๊ณต๊ธฐ๋ฐฉ์ธ์ด ๋ ์ค๋ฅด ๋ฏ์ด ์ ๋ ฌํ๋ ๋ฐฉ์
ํด๋น ๊ฐ ์ผ์ด์ค ๋ณ ๊ฒฐ๊ณผ๋ฌผ๋ก๋ ์ ๋ ฌ๋์ง ์์ ๋ฐฐ์ด์์ ๊ฐ์ฅ ํฐ ์๊ฐ ๋งจ ๋ค๋ก ๋ฐฐ์น
ํญ์ O(N^2)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง.
ํต์ฌ ํฌ์ธํธ Case๋ฅผ ๋ฐ๋ณตํ๋ฉด์ ๊ฐ์ฅ ํฐ ์๊ฐ ๋ค์ ๋ฐฐ์น
*/
public class Bubble implements Sort {
@Override
public void sort(int[] array) {
for(int i= 0; i<array.length-1;i++){
System.out.println("Case "+i);
//๋ฒ๋ธ ์ํธ์ ๊ฒฐ๊ณผ๋ฌผ๋ก๋ ํฐ ์๊ฐ ๋งจ ๋ค์ ๋ฐฐ์น๊ฐ ๋๊ธฐ ๋๋ฌธ์
for(int j = 0 ;j<array.length-1-i;j++){
if(array[j]>array[j+1]){
swap(array,j,j+1);
printAllArray(array, 0);
}
}
}
}
}