BootCamp 32day

GyeongNamΒ·2024λ…„ 1μ›” 1일
0

BootCamp

λͺ©λ‘ 보기
30/49
post-thumbnail

πŸ“… 2023λ…„ 12μ›” 28일

[μ•Œκ³ λ¦¬μ¦˜ 1일차]


32일차: μ£Όμš” μ•Œκ³ λ¦¬μ¦˜ (1)

Heap Sort

νž™ μ •λ ¬(heap sort)λŠ” νž™(heap)을 μ‚¬μš©ν•˜μ—¬ μ •λ ¬ν•˜λŠ” μ•Œκ³ λ¦¬μ¦˜μ΄λ‹€.
νž™μ€ "λΆ€λͺ¨μ˜ 값이 μžμ‹μ˜ 값보닀 항상 크닀 or μž‘λ‹€"λΌλŠ” 쑰건을 λ§Œμ‘±ν•˜λŠ” μ™„μ „μ΄μ§„νŠΈλ¦¬.

  • μž₯점
    λΉ λ₯΄λ‹€.
    좔가적인 배열이 ν•„μš”ν•˜μ§€ μ•Šλ‹€.

  • 단점
    퀡이 ν‰κ· μ μœΌλ‘œ 더 λΉ λ₯΄λ‹€.
    λΆˆμ•ˆμ • 정렬이닀.

public static void main(String[] args) {
	int[] HeapArr = {7,6,5,8,3,5,9,1,7};
	// μ΅œμ†Œ νž™κ΅¬μ„± : n/2번만큼 heapify μˆ˜ν–‰
    for(int i = HeapArr.length/2-1; i>=0; i--){
		heapify(HeapArr, i, HeapArr.length);
	}
	System.out.println(Arrays.toString(HeapArr));
	// 졜초 νž™κ΅¬μ„± 이후에 root λ…Έλ“œμ™€ λ§ˆμ§€λ§‰ λ…Έλ“œμ™€ change
	for(int i =HeapArr.length-1; i>0; i--){
		swap(HeapArr, 0, i);
		heapify(HeapArr, 0, i);
	}
	System.out.println(Arrays.toString(HeapArr));
}
static void heapify( int[] HeapArr, int root, int arr_size){
	int left = 2 * root + 1;
	int right = 2 * root + 2;
	int temp = root;
	if(left<arr_size && HeapArr[left] > HeapArr[temp]){
		temp = left;
	}
	if(right<arr_size && HeapArr[right] > HeapArr[temp]){
		temp = right;
	}
	if(temp != root){
		swap(HeapArr, root, temp);
		heapify(HeapArr, temp, arr_size);
	}
}
static void swap(int[] HeapArr, int x, int y){
	int num = HeapArr[x];
	HeapArr[x] = HeapArr[y];
	HeapArr[y] = num;
}

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ 문제 : 더 맡게
ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ 문제 : λ””μŠ€ν¬ 컨트둀러
ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ 문제 : 이쀑 μš°μ„ μˆœμœ„ν

μ™„μ „ 탐색 : DFS (깊이 μš°μ„  탐색)

public class DFS_ListGraph {
    static List<List<Integer>> list = new ArrayList<>();
    static boolean[] visited;
    public static void main(String[] args) {
        int [][] input =  {
                {0,1},
                {0,2},
                {1,3},
                {2,3},
                {2,4},
        };
        visited = new boolean[input.length];
        for(int i = 0; i<input.length; i++){
            list.add(new ArrayList<>());
        }
        for (int[] a : input ){
            addEdge(a[0],a[1]);
        }
        System.out.println(list);
        System.out.println();
        dfs(1);
    }
    static void addEdge(int a , int b){
        list.get(a).add(b);
        list.get(b).add(a);
    }
    static void dfs(int start) {
        System.out.println(start);
        visited[start] = true;
        for (int i = 0; i<list.get(start).size(); i++) {
            if(!visited[list.get(start).get(i)]) {
                dfs(list.get(start).get(i));
            }
        }
    }
}
public class DFS_ArrayGraph {
    static int[][] graph;
    static boolean[] visited;
    public static void main(String[] args) {
        graph = new int[][]{
                {0, 1, 1, 0, 0},
                {1, 0, 0, 1, 0},
                {1, 0, 0, 1, 1},
                {0, 1, 1, 0, 0},
                {0, 0, 1, 0, 0}
        };
        visited = new boolean[graph.length];
        dfs( 0);
    }
    static void dfs(int start) {
        System.out.println(start);
        visited[start] = true;
        for (int i = 0; i<graph[start].length; i++) {
            if(!visited[i] && graph[start][i] == 1) {
                dfs(i);
            }
        }
    }
}

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ 문제 : λ„€νŠΈμ›Œν¬
ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ 문제 : νƒ€κ²Ÿ λ„˜λ²„


github java μ‹€μŠ΅ λ‚΄μš©

profile
503 Service Unavailable Error

0개의 λŒ“κΈ€