by Nathan
Bubble sort 본문
1. code
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 | import java.util.Arrays; public class CrunchifyBubbleSort { public static void main(String[] args) { int crunchifyArry[] = { 15, 3, 9, 7, 19, 8, 1, 5 }; log("============ Ascending Order result:" + Arrays.toString(CrunchifyBubbleSortAsceMethod(crunchifyArry)) + "\n"); log("============ Descending Order result:" + Arrays.toString(CrunchifyBubbleSortDescMethod(crunchifyArry)) + "\n"); } // Bubble Sort Algorithm in Ascending Order public static int[] CrunchifyBubbleSortAsceMethod(int[] crunchifyArr) { int temp; for (int i = 0; i < crunchifyArr.length - 1; i++) { for (int j = 1; j < crunchifyArr.length - i; j++) { if (crunchifyArr[j - 1] > crunchifyArr[j]) { temp = crunchifyArr[j - 1]; crunchifyArr[j - 1] = crunchifyArr[j]; crunchifyArr[j] = temp; } } log("Iteration " + (i + 1) + ": " + Arrays.toString(crunchifyArr)); } return crunchifyArr; } // Bubble Sort Algorithm in Descending Order public static int[] CrunchifyBubbleSortDescMethod(int[] crunchifyArr) { int temp; for (int i = 0; i < crunchifyArr.length - 1; i++) { for (int j = 1; j < crunchifyArr.length - i; j++) { if (crunchifyArr[j - 1] < crunchifyArr[j]) { temp = crunchifyArr[j - 1]; crunchifyArr[j - 1] = crunchifyArr[j]; crunchifyArr[j] = temp; } } log("Iteration " + (i + 1) + ": " + Arrays.toString(crunchifyArr)); } return crunchifyArr; } // Simple log util private static void log(String result) { System.out.println(result); } } | cs |
2. Result
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Iteration 1: [3, 9, 7, 15, 8, 1, 5, 19] Iteration 2: [3, 7, 9, 8, 1, 5, 15, 19] Iteration 3: [3, 7, 8, 1, 5, 9, 15, 19] Iteration 4: [3, 7, 1, 5, 8, 9, 15, 19] Iteration 5: [3, 1, 5, 7, 8, 9, 15, 19] Iteration 6: [1, 3, 5, 7, 8, 9, 15, 19] Iteration 7: [1, 3, 5, 7, 8, 9, 15, 19] ============ Ascending Order result:[1, 3, 5, 7, 8, 9, 15, 19] Iteration 1: [3, 5, 7, 8, 9, 15, 19, 1] Iteration 2: [5, 7, 8, 9, 15, 19, 3, 1] Iteration 3: [7, 8, 9, 15, 19, 5, 3, 1] Iteration 4: [8, 9, 15, 19, 7, 5, 3, 1] Iteration 5: [9, 15, 19, 8, 7, 5, 3, 1] Iteration 6: [15, 19, 9, 8, 7, 5, 3, 1] Iteration 7: [19, 15, 9, 8, 7, 5, 3, 1] ============ Descending Order result:[19, 15, 9, 8, 7, 5, 3, 1] | cs |
'Programming > JAVA' 카테고리의 다른 글
엑셀 다운로드(gradle, poi3.7) (0) | 2018.07.30 |
---|---|
엑셀 업로드(gradle, poi3.7) (0) | 2018.07.30 |
HTTP request (0) | 2017.03.20 |
StringTokenizer (0) | 2017.03.17 |
request.getRemoteAddr() ipv4로 받기 (0) | 2016.06.13 |
Comments