교육/알고리즘_기본

📌 기본 정렬(선택, 삽입, 버블)

Jedy_Kim 2024. 7. 30. 18:04
728x90

🎯정렬 : 특정 기준을 적용하여 나열함

🎯 선택정렬 : 최솟값을 앞으로 이동시킴

| 14 4 8 23 11 5 3 2 4 9

-> 가장 작은 값을 찾아서 위치를 바꿈

2 | 4 8 23 11 5 3 14 4 9

2 3 | 8 23 11 5 4 14 4 9

2 3 4 | 23 11 5 8 14 4 9

2 3 4 4 | 11 5 8 14 23 9

2 3 4 4 5 | 11 8 14 23 9

2 3 4 4 5 8 | 11 14 23 9

2 3 4 4 5 8 9 | 11 14 23

2 3 4 4 5 8 9 11 | 14 23

2 3 4 4 5 8 9 11 14 | 23

2 3 4 4 5 8 9 11 14 23 |

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
public class Sorts {
 
    private static int[] array = {1448231153249};
 
    public static void main(String[] args) {
        SelectionSort(array);
        PrintAll(array);
    }
 
    public static void PrintAll(int[] array) {
        forint ar : array) {
            System.out.print(ar + " ");
        }
    }
 
    public static void SelectionSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIdx = i;
            for (int j = i; j < array.length; j++) {
                if (array[j] < array[minIdx]) {
                    minIdx = j;
                }
            }
 
            int tmp = array[i];
            array[i] = array[minIdx];
            array[minIdx] = tmp;
        }
    }
 
}
cs

🎯 삽입정렬 : 원소를 차례대로 정렬된 배열에 삽입시킴

14 4 8 23 11 5 3 2 4 9

4 | 14 8 23 11 5 3 2 4 9

4 8 14 | 23 11 5 3 2 4 9

4 8 14 23 | 11 5 3 2 4 9

4 8 14 11 | 23 5 3 2 4 9

4 8 14 11 23 | 5 3 2 4 9

4 8 11 14 23 | 5 3 2 4 9

...

2 3 4 4 5 8 9 11 14 23 |

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
public class Sorts {
 
    private static int[] array = {1448231153249};
 
    public static void main(String[] args) {
        InsertionSort(array);
        PrintAll(array);
    }
 
    public static void PrintAll(int[] array) {
        forint ar : array) {
            System.out.print(ar + " ");
        }
    }
    
    public static void InsertionSort(int[] array) {
 
        for (int i = 1; i < array.length; i++) {
            for (int j = i; j > 0; j--) {
                if (array[j] < array[j - 1]) {
                    int tmp = array[j];
                    array[j] = array[j - 1];
                    array[j - 1= tmp;
                } else {
                    break;
                }
            }
        }
 
    } 
 
}
cs

🎯 버블정렬 : 인접한 원소를 비교하여  큰 수를 뒤로 보냄

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
public class Sorts {
 
    private static int[] array = {1448231153249};
 
    public static void main(String[] args) {
        BubbleSort(array);
        PrintAll(array);
    }
 
    public static void PrintAll(int[] array) {
        forint ar : array) {
            System.out.print(ar + " ");
        }
    } 
 
    public static void BubbleSort(int[] array) {
 
        for ( int i = array.length - 1; i > 0; i-- ) {
            for (int j = 0; j < i; j++) {
                if (array[j] > array[j + 1]) {
                    int tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1= tmp;
                }
            }
        }
 
    }
 
}
cs

 

관련 문제

- 선택정렬

https://www.acmicpc.net/problem/2750

- 삽입정렬

https://www.acmicpc.net/problem/23968

- 버블정렬

https://www.acmicpc.net/problem/2750

- 문자열 내 마음대로 정렬하기

https://school.programmers.co.kr/learn/courses/30/lessons/12915

-가장 큰 수

https://school.programmers.co.kr/learn/courses/30/lessons/42746

- K번째 수

https://school.programmers.co.kr/learn/courses/30/lessons/42748

반응형

'교육 > 알고리즘_기본' 카테고리의 다른 글

📌 깊이 우선 탐색(Depth First Search, DFS)  (0) 2024.08.08
📌 문자열  (0) 2024.07.25
📌 자료구조 : Set, Map  (0) 2024.07.18
📌 재귀함수  (0) 2024.07.18
📌 배열(개념)  (0) 2024.07.11