by Nathan

Bubble sort 본문

Programming/JAVA

Bubble sort

넷쓴 2017. 3. 17. 13:26



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[] = { 1539719815 };
        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: [3971581519]
Iteration 2: [3798151519]
Iteration 3: [3781591519]
Iteration 4: [3715891519]
Iteration 5: [3157891519]
Iteration 6: [1357891519]
Iteration 7: [1357891519]
============ Ascending Order result:[1357891519]
 
Iteration 1: [3578915191]
Iteration 2: [5789151931]
Iteration 3: [7891519531]
Iteration 4: [8915197531]
Iteration 5: [9151987531]
Iteration 6: [1519987531]
Iteration 7: [1915987531]
============ Descending Order result:[1915987531]
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