diff --git a/en/Data Structures/Graph/Bellman-Ford.md b/en/Data Structures/Graph/Bellman-Ford.md index b5212d1f..1dda9ba6 100644 --- a/en/Data Structures/Graph/Bellman-Ford.md +++ b/en/Data Structures/Graph/Bellman-Ford.md @@ -11,7 +11,7 @@ Given a weighted directed graph G(V,E) and a source vertex s ∈ V, determine fo - Create an array dist[] of size |V| with all values as infinite except dist[s]. - Repeat the following |V| - 1 times. Where |V| is number of vertices. - Create another loop to go through each edge (u, v) in E and do the following: - 1. dist[v] = minimum(dist[v], dist[u] + weight of edge). + 1. dist[v] = minimum(dist[v], dist[u] + weight of edge). - Lastly iterate through all edges on last time to make sure there are no negatively weighted cycles. #### Time Complexity @@ -30,7 +30,7 @@ O(V^2) ``` # of vertices in graph = 5 [A, B, C, D, E] - # of edges in graph = 8 + # of edges in graph = 8 edges [A->B, A->C, B->C, B->D, B->E, D->C, D->B, E->D] weight [ -1, 4, 3, 2, 2, 5, 1, -4 ] @@ -38,44 +38,44 @@ O(V^2) - // edge A->B - graph->edge[0].src = A - graph->edge[0].dest = B - graph->edge[0].weight = -1 - - // edge A->C - graph->edge[1].src = A - graph->edge[1].dest = C - graph->edge[1].weight = 4 - - // edge B->C - graph->edge[2].src = B - graph->edge[2].dest = C - graph->edge[2].weight = 3 - - // edge B->D - graph->edge[3].src = B - graph->edge[3].dest = D - graph->edge[3].weight = 2 - - // edge B->E - graph->edge[4].src = B - graph->edge[4].dest = E - graph->edge[4].weight = 2 - - // edge D->C + // edge A->B + graph->edge[0].src = A + graph->edge[0].dest = B + graph->edge[0].weight = -1 + + // edge A->C + graph->edge[1].src = A + graph->edge[1].dest = C + graph->edge[1].weight = 4 + + // edge B->C + graph->edge[2].src = B + graph->edge[2].dest = C + graph->edge[2].weight = 3 + + // edge B->D + graph->edge[3].src = B + graph->edge[3].dest = D + graph->edge[3].weight = 2 + + // edge B->E + graph->edge[4].src = B + graph->edge[4].dest = E + graph->edge[4].weight = 2 + + // edge D->C graph->edge[5].src = D - graph->edge[5].dest = C - graph->edge[5].weight = 5 - - // edge D->B + graph->edge[5].dest = C + graph->edge[5].weight = 5 + + // edge D->B graph->edge[6].src = D - graph->edge[6].dest = B - graph->edge[6].weight = 1 - - // edge E->D + graph->edge[6].dest = B + graph->edge[6].weight = 1 + + // edge E->D graph->edge[7].src = E - graph->edge[7].dest = D + graph->edge[7].dest = D graph->edge[7].weight = -3 for source = A @@ -86,8 +86,7 @@ O(V^2) C 2 A->B->C = -1 + 3 D -2 A->B->E->D = -1 + 2 + -3 E 1 A->B->E = -1 + 2 - ``` - +``` #### Code Implementation Links @@ -103,5 +102,6 @@ O(V^2) #### Others Sources Used: + - https://www.geeksforgeeks.org/bellman-ford-algorithm-dp-23/ - https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm diff --git "a/ko/\352\270\260\354\264\210 \354\210\230\355\225\231/\353\223\261\353\271\204\354\210\230\354\227\264.md" "b/ko/\352\270\260\354\264\210 \354\210\230\355\225\231/\353\223\261\353\271\204\354\210\230\354\227\264.md" index 016de1e4..71c9c6cf 100644 --- "a/ko/\352\270\260\354\264\210 \354\210\230\355\225\231/\353\223\261\353\271\204\354\210\230\354\227\264.md" +++ "b/ko/\352\270\260\354\264\210 \354\210\230\355\225\231/\353\223\261\353\271\204\354\210\230\354\227\264.md" @@ -23,7 +23,7 @@ **등비수열의 n번째 항 공식:** -`a`가 초항이고, `r`이 공비일 때, n번째 항은: +가 초항, 이 공비일 때, 번째 항은:

@@ -37,13 +37,11 @@ **등비수열에 관련된 문제를 풀기 위한 일반적인 공식:** -`a`가 초항, `r`이 공비일 때: +가 초항, 이 공비일 때: -- 등비수열의 n번째 항 = `a * r^(n-1)`. -- 기하평균 = `n개 항의 곱의 n제곱근`. -- 등비급수 (r < 1) = `[a (1 – r^n)] / [1 – r]`. -- 등비급수 (r > 1) = `[a (r^n – 1)] / [r – 1]`. -- 무한등비급수 (r < 1) = `(a) / (1 – r)`. +- 등비급수 (r < 1) = +- 등비급수 (r > 1) = +- 무한등비급수 (r < 1) = ## 영상 URL diff --git "a/ko/\352\270\260\354\264\210 \354\210\230\355\225\231/\353\223\261\354\260\250\354\210\230\354\227\264.md" "b/ko/\352\270\260\354\264\210 \354\210\230\355\225\231/\353\223\261\354\260\250\354\210\230\354\227\264.md" index a760d77b..03a96763 100644 --- "a/ko/\352\270\260\354\264\210 \354\210\230\355\225\231/\353\223\261\354\260\250\354\210\230\354\227\264.md" +++ "b/ko/\352\270\260\354\264\210 \354\210\230\355\225\231/\353\223\261\354\260\250\354\210\230\354\227\264.md" @@ -16,7 +16,7 @@ **등차수열의 n번째 항 공식:** -`a`가 초항이고, `d`가 공차일 때, n번째 항은: +가 초항, 가 공차일 때, 번째 항은:

@@ -30,11 +30,10 @@ **등차수열에 관련된 물제를 풀기 위한 일반적인 공식:** -`a`가 초항, `d`가 공차일 때: +가 초항, 가 공차일 때: -- 등차수열의 n번째 항 = `a + d*(n-1)`. -- 산술평균 = `전체 항의 합 / 항의 개수`. -- 등차급수 = `0.5 * n * (초항 + 말항)` = `0.5 * n * [2a + (n-1) d]`. +- 산술평균 = `전체 항의 합 / 항의 개수` +- 등차급수 = `n * (초항 + 말항) / 2` = ## 영상 URL diff --git "a/ko/\352\270\260\354\264\210 \354\210\230\355\225\231/\354\236\220\353\246\277\354\210\230.md" "b/ko/\352\270\260\354\264\210 \354\210\230\355\225\231/\354\236\220\353\246\277\354\210\230.md" index 3640aced..09c2f4fe 100644 --- "a/ko/\352\270\260\354\264\210 \354\210\230\355\225\231/\354\236\220\353\246\277\354\210\230.md" +++ "b/ko/\352\270\260\354\264\210 \354\210\230\355\225\231/\354\236\220\353\246\277\354\210\230.md" @@ -24,7 +24,7 @@ N = 10000 [양수] 3. N을 10으로 나눈다. 4. N이 0이 될 때까지 위의 과정을 반복한다. -**알고리즘 분석:** 위 방법에서 수행되는 작업의 수는 숫자 N의 자릿수와 같다는 사실을 쉽게 알 수 있다. 따라서 이 방법의 시간 복잡도는 $O(자릿수)$이다. +**알고리즘 분석:** 위 방법에서 수행되는 작업의 수는 숫자 N의 자릿수와 같다는 사실을 쉽게 알 수 있다. 따라서 이 방법의 시간 복잡도는 이다. (는 숫자 N의 자릿수) **모의 실행:** `N = 58964`라고 할 때, diff --git "a/ko/\352\270\260\354\264\210 \354\210\230\355\225\231/\355\217\211\352\267\240\352\260\222.md" "b/ko/\352\270\260\354\264\210 \354\210\230\355\225\231/\355\217\211\352\267\240\352\260\222.md" index 746a0ae9..7212ef98 100644 --- "a/ko/\352\270\260\354\264\210 \354\210\230\355\225\231/\355\217\211\352\267\240\352\260\222.md" +++ "b/ko/\352\270\260\354\264\210 \354\210\230\355\225\231/\355\217\211\352\267\240\352\260\222.md" @@ -47,13 +47,13 @@ sum = 160 count = 7 ``` -유효숫자를 무시하면: `sum / count = `22.857142 +유효숫자를 무시하면 22.857142 -유효숫자를 고려하면: `sum / count = 23` +유효숫자를 고려하면 23 ### 5단계 -22.857142나 `23`을 반환한다. +22.857142나 23을 반환한다. ## 구현 diff --git "a/ko/\354\236\220\353\243\214\352\265\254\354\241\260/\352\267\270\353\236\230\355\224\204/\353\262\250\353\250\274-\355\217\254\353\223\234 \354\225\214\352\263\240\353\246\254\354\246\230.md" "b/ko/\354\236\220\353\243\214\352\265\254\354\241\260/\352\267\270\353\236\230\355\224\204/\353\262\250\353\250\274-\355\217\254\353\223\234 \354\225\214\352\263\240\353\246\254\354\246\230.md" index a2228016..83ab1cf8 100644 --- "a/ko/\354\236\220\353\243\214\352\265\254\354\241\260/\352\267\270\353\236\230\355\224\204/\353\262\250\353\250\274-\355\217\254\353\223\234 \354\225\214\352\263\240\353\246\254\354\246\230.md" +++ "b/ko/\354\236\220\353\243\214\352\265\254\354\241\260/\352\267\270\353\236\230\355\224\204/\353\262\250\353\250\274-\355\217\254\353\223\234 \354\225\214\352\263\240\353\246\254\354\246\230.md" @@ -2,15 +2,15 @@ ## 문제 -방향 가중치 그래프 $G(V,E)$와 시작점 $s \in V$가 주어졌을 때, 각 점 $v \in V$에 대하여 $s$와 $v$를 잇는 가장 짧은 경로를 구하라. ($V$는 꼭짓점의 집합, $E$는 간선의 집합) +방향 가중치 그래프 와 시작점 가 주어졌을 때, 각 점 에 대하여 를 잇는 가장 짧은 경로를 구하라. (는 꼭짓점의 집합, 는 간선의 집합) ## 절차 1. 시작점에서 모든 꼭짓점까지의 거리를 무한대로 초기화한다. 2. 시작점으로의 거리를 0으로 초기화한다. -3. `dist[s]`를 제외한 모든 값을 무한대로 하는 크기가 $|V|$인 `dist`라는 배열을 만든다. -4. 다음을 $|V|-1$회 반복한다. -5. $E$에 속한 모든 간선 `(u,v)`에 대해 다음을 반복한다: +3. `dist[s]`를 제외한 모든 값을 무한대로 하는 크기가 인 `dist`라는 배열을 만든다. +4. 다음을 회 반복한다. +5. 에 속한 모든 간선 `(u,v)`에 대해 다음을 반복한다: ``` dist[v] = minimum(dist[v], dist[u] + weight of edge) @@ -20,11 +20,11 @@ ## 시간 복잡도 -$O(VE)$ + ## 공간 복잡도 -$O(V^2)$ + ## 만든 사람 @@ -41,7 +41,6 @@ $O(V^2)$ 시작점: [ A, A, B, B, B, D, D, E ] - 꼭짓점 개수 = 5 간선 개수 = 8 @@ -87,7 +86,7 @@ $O(V^2)$ for source = A - Vertex Distance from Source + 꼭짓점 시작점으로부터의 거리 A 0 (A->A) B -1 (A->B) C 2 (A->B->C = -1 + 3) @@ -98,9 +97,9 @@ $O(V^2)$ ## 구현 - [Java](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/BellmanFord.java) -- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/Dynamic%20Programming/Bellman-Ford.cpp) -- [Python](https://github.com/TheAlgorithms/Python/blob/master/data_structures/graph/bellman_ford.py) -- [C](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/Bellman-Ford.c) +- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/bellman_ford.cpp) +- [Python](https://github.com/TheAlgorithms/Python/blob/master/graphs/bellman_ford.py) +- [C](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/bellman_ford.c) ## 영상 URL diff --git "a/ko/\354\236\220\353\243\214\352\265\254\354\241\260/\353\260\260\354\227\264/\353\260\260\354\227\264.md" "b/ko/\354\236\220\353\243\214\352\265\254\354\241\260/\353\260\260\354\227\264/\353\260\260\354\227\264.md" index a817bf7a..35612e38 100644 --- "a/ko/\354\236\220\353\243\214\352\265\254\354\241\260/\353\260\260\354\227\264/\353\260\260\354\227\264.md" +++ "b/ko/\354\236\220\353\243\214\352\265\254\354\241\260/\353\260\260\354\227\264/\353\260\260\354\227\264.md" @@ -2,16 +2,13 @@ 배열은 프로그래밍에서 가장 기본적인 데이터 구조이다. 배열은 정적 배열과 동적 배열로 나눠진다. 정적 배열은 요소의 수가 고정되어 있고, 각각은 메모리의 동일한 공간을 차지한다. 즉, 정적 배열이 차지하는 메모리는 컴파일 시간에 결정되지만, 동적 배열의 경우 크기가 고정되지 않는다. -배열 요소의 값은 `O(1)` 시간 안에 검색할 수 있다. +배열 요소의 값은 시간 안에 검색할 수 있다. 모든 배열은 연속된 메모리 주소를 가진다. 우리는 인덱스로 각 요소에 접근할 수 있다. 가장 낮은 인덱스는 첫 요소에 해당하고 가장 높은 인덱스는 마지막 요소에 해당한다. -## YouTube - -- [The Coding Train](https://youtu.be/NptnmWvkbTw) -- [Simple Snippets (C++)](https://youtu.be/ibeGtDEQGz0) - ## 영상 URL - [GeeksforGeeks](https://www.geeksforgeeks.org/introduction-to-arrays/) +- [The Coding Train](https://youtu.be/NptnmWvkbTw) +- [Simple Snippets (C++)](https://youtu.be/ibeGtDEQGz0) diff --git "a/ko/\354\236\220\353\243\214\352\265\254\354\241\260/\354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270/\353\213\250\354\235\274 \354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270.md" "b/ko/\354\236\220\353\243\214\352\265\254\354\241\260/\354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270/\353\213\250\354\235\274 \354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270.md" index f57161cd..07b725f5 100644 --- "a/ko/\354\236\220\353\243\214\352\265\254\354\241\260/\354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270/\353\213\250\354\235\274 \354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270.md" +++ "b/ko/\354\236\220\353\243\214\352\265\254\354\241\260/\354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270/\353\213\250\354\235\274 \354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270.md" @@ -14,25 +14,26 @@ ### 시간 복잡도 -| 작업 | 평균 | 최악 | -| ---- | ------ | ------ | -| 접근 | $O(n)$ | $O(n)$ | -| 탐색 | $O(n)$ | $O(n)$ | -| 삽입 | $O(1)$ | $O(1)$ | -| 제거 | $O(1)$ | $O(1)$ | +| 작업 | 평균 | 최악 | +| ---- | ---------------------------------------------------------------------- | ---------------------------------------------------------------------- | +| 접근 | | | +| 탐색 | | | +| 삽입 | | | +| 제거 | | | ## 예시 ```java class LinkedList { - Node head; // Pointer to the first element - Node tail; // Optional. Points to the last element +<<<<<<< HEAD + Node head; // 첫 원소를 가리키는 포인터 + Node tail; // (Optional) 마지막 원소를 가리키는 포인터 - int length; // Optional + int length; // (Optional) 전체 노드 수 class Node { - int data; // Node data. Can be int, string, float, templates, etc - Node next; // Pointer to the next node on the list + int data; // 노드의 데이터 + Node next; // 다음 노드를 가리키는 포인터 } } ``` diff --git "a/ko/\354\236\220\353\243\214\352\265\254\354\241\260/\354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270/\354\235\264\354\244\221 \354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270.md" "b/ko/\354\236\220\353\243\214\352\265\254\354\241\260/\354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270/\354\235\264\354\244\221 \354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270.md" index 823272ed..d28a2cfa 100644 --- "a/ko/\354\236\220\353\243\214\352\265\254\354\241\260/\354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270/\354\235\264\354\244\221 \354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270.md" +++ "b/ko/\354\236\220\353\243\214\352\265\254\354\241\260/\354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270/\354\235\264\354\244\221 \354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270.md" @@ -18,26 +18,25 @@ ### 시간 복잡도 -| 작업 | 평균 | 최악 | -| ---- | ------ | ------ | -| 접근 | $Θ(n)$ | $O(n)$ | -| 탐색 | $Θ(n)$ | $O(n)$ | -| 삽입 | $Θ(1)$ | $O(1)$ | -| 제거 | $Θ(1)$ | $O(1)$ | +| 작업 | 평균 | 최악 | +| ---- | --------------------------------------------------------------------------- | ---------------------------------------------------------------------- | +| 접근 | | | +| 탐색 | | | +| 삽입 | | | +| 제거 | | | ## 예시 ```java class LinkedList { + Node head; // 첫 원소를 가리키는 포인터 + Node tail; // (Optional) 마지막 원소를 가리키는 포인터 - Node head; // Pointer to the first element - Node tail; // Optional. Points to the last element - - int length; // Optional + int length; // (Optional) 전체 노드 수 class Node { - int data; // Node data. Can be int, string, float, templates, etc - Node next; // Pointer to the next node on the list + int data; // 노드의 데이터 + Node next; // 다음 노드를 가리키는 포인터 Node prev; Node(int data) { @@ -52,19 +51,19 @@ class LinkedList { ```java public void push(int new_data) { - /* 1. allocate node - 2. put in the data */ + /* 1. 노드를 만든다 + 2. 데이터를 입력한다다 */ Node new_Node = new Node(new_data); - /* 3. Make next of new node as head and previous as NULL */ + /* 3. 새로운 노드의 next 변수를 head로, prev 변수를 NULL로 설정한다 */ new_Node.next = head; new_Node.prev = null; - /* 4. change prev of head node to new node */ + /* 4. head의 prev 변수를 새로운 노드로 변경한다 */ if (head != null) head.prev = new_Node; - /* 5. move the head to point to the new node */ + /* 5. head를 새로운 노드로 변경한다 */ head = new_Node; } ``` @@ -75,28 +74,28 @@ class LinkedList { ```java public void InsertAfter(Node prev_Node, int new_data) { - /*1. check if the given prev_node is NULL */ + /*1. 주어진 prev_Node가 NULL인지 확인한다 */ if (prev_Node == null) { System.out.println("The given previous node cannot be NULL "); return; } - /* 2. allocate node - 3. put in the data */ - Node new_node = new Node(new_data); + /* 2. 노드를 만든다 + 3. 데이터를 입력한다 */ + Node new_Node = new Node(new_data); - /* 4. Make next of new node as next of prev_node */ - new_node.next = prev_Node.next; + /* 4. 새로운 노드의 next 변수를 prev_Node의 next 변수값으로 설정한다 */ + new_Node.next = prev_Node.next; - /* 5. Make the next of prev_node as new_node */ - prev_Node.next = new_node; + /* 5. prev_Node의 next 변수를 새로운 노드로 변경한다 */ + prev_Node.next = new_Node; - /* 6. Make prev_node as previous of new_node */ - new_node.prev = prev_Node; + /* 6. 새로운 노드의 prev 변수를 prev_Node로 설정한다 */ + new_Node.prev = prev_Node; - /* 7. Change previous of new_node's next node */ - if (new_node.next != null) - new_node.next.prev = new_node; + /* 7. 새로운 노드의 다음 노드의 prev 변수를 새로운 노드로 변경한다 */ + if (new_Node.next != null) + new_Node.next.prev = new_Node; } } ``` diff --git "a/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\352\263\204\354\210\230 \354\240\225\353\240\254.md" "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\352\263\204\354\210\230 \354\240\225\353\240\254.md" new file mode 100644 index 00000000..a388f7e0 --- /dev/null +++ "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\352\263\204\354\210\230 \354\240\225\353\240\254.md" @@ -0,0 +1,59 @@ +# 계수 정렬 + +## 문제 + +n개 원소로 구성된 배열이 주어졌을 때, 이 배열을 정렬하는 함수를 구하라. + +## 절차 + +- Find out the maximum element (let's call it `max`) from the given array. +- Initialize an array of length `max+1` with all elements set to 0 to store the array's count. +- Store the count of each element at their respective index in the array's count. +- Store cumulative sum of the elements of the count array. It helps in placing the elements into the correct index of the sorted array. +- Find the index of each element of the original array in the array's count. This gives the cumulative count. +- Place the element at the index calculated and decrease its count by one. + +## 시간 복잡도 + +- (는 음수가 아닌 key 값의 범위) + +## 공간 복잡도 + +- (는 음수가 아닌 key 값의 범위) + +## 만든 사람 + +- [해롤드 H. 수어드](https://en.wikipedia.org/wiki/Harold_H._Seward) + +## 예시 + +``` +countingSort(array, size) + max <- find largest element in array + initialize count array with all zeros + for j <- 0 to size + find the total count of each unique element and + store the count at jth index in count array + for i <- 1 to max + find the cumulative sum and store it in count array itself + for j <- size down to 1 + restore the elements to array + decrease count of each element restored by 1 +``` + +## 구현 + +- [C](https://github.com/TheAlgorithms/C/blob/master/sorting/counting_sort.c) +- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/counting_sort.cpp) +- [JavaScript](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/CountingSort.js) +- [Matlab](https://github.com/TheAlgorithms/MATLAB-Octave/blob/master/algorithms/sorting/counting_sort.m) +- [Python](https://github.com/TheAlgorithms/Python/blob/master/sorts/counting_sort.py) +- [Rust](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/counting_sort.rs) + +## 영상 URL + +- [GeeksforGeeks](https://www.youtube.com/watch?v=7zuGmKfUt7s) + +## 기타 + +- [David Galles (University of San Francisco)](https://www.cs.usfca.edu/~galles/visualization/CountingSort.html) diff --git "a/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\352\270\260\354\210\230 \354\240\225\353\240\254.md" "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\352\270\260\354\210\230 \354\240\225\353\240\254.md" new file mode 100644 index 00000000..1048889e --- /dev/null +++ "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\352\270\260\354\210\230 \354\240\225\353\240\254.md" @@ -0,0 +1,51 @@ +# 기수 정렬 + +The lower bound for Comparison based sorting algorithm (Merge Sort, Heap Sort, Quick-Sort .. etc) is `Ω(nlogn)`, i.e., they cannot do better than `nlogn`. + +Counting sort is a linear time sorting algorithm that sort in `O(n+k)` time when elements are in the range from 1 to k. + +What if the elements are in the range from 1 to n2? We can’t use counting sort because counting sort will take `O(n2)` which is worse than comparison-based sorting algorithms. Can we sort such an array in linear time? + +Radix Sort is the answer. The idea of Radix Sort is to do digit by digit sort starting from least significant digit to most significant digit. Radix sort uses counting sort as a subroutine to sort. + +## 절차 + +Do following for each digit i where i varies from least significant digit to the most significant digit. +Sort input array using counting sort (or any stable sort) according to the i’th digit. + +## 예시 + +Original, unsorted list: +`170, 45, 75, 90, 802, 24, 2, 66` + +Sorting by least significant digit (1s place) gives: + +[*Notice that we keep 802 before 2, because 802 occurred +before 2 in the original list, and similarly for pairs +170 & 90 and 45 & 75.] + +Sorting by next digit (10s place) gives: + +[*Notice that 802 again comes before 2 as 802 comes before 2 in the previous list.] + +`802, 2, 24, 45, 66, 170, 75, 90` + +Sorting by the most significant digit (100s place) gives: +`2, 24, 45, 66, 75, 90, 170, 802` + +## 시간 복잡도 + +Let there be d digits in input integers. Radix Sort takes `O(d*(n+b))` time where b is the base for representing numbers, for example, for the decimal system, b is 10. +What is the value of d? If `k` is the maximum possible value, then d would be `O(logb(k))`. So overall time complexity is `O((n+b) * logb(k))`. Which looks more than the +time complexity of comparison-based sorting algorithms for a large k. Let us first limit k. Let k <= nc where c is a constant. In that case, the complexity becomes +`O(n logb(n))`. But it still doesn’t beat comparison-based sorting algorithms. + +## Is Radix Sort preferable to Comparison based sorting algorithms like Quick-Sort? + +If we have `log2n` bits for every digit, the running time of Radix appears to be better than Quick Sort for a wide range of input numbers. The constant factors hidden in +asymptotic notation are higher for Radix Sort and Quick-Sort uses hardware caches more effectively. Also, Radix sort uses counting sort as a subroutine and counting sort +takes extra space to sort numbers. + +## 영상 URL + +- [](https://youtu.be/nu4gDuFabIM) diff --git "a/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\353\262\204\353\270\224 \354\240\225\353\240\254 (\354\236\254\352\267\200 \353\262\204\354\240\204).md" "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\353\262\204\353\270\224 \354\240\225\353\240\254 (\354\236\254\352\267\200 \353\262\204\354\240\204).md" new file mode 100644 index 00000000..2ef2515e --- /dev/null +++ "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\353\262\204\353\270\224 \354\240\225\353\240\254 (\354\236\254\352\267\200 \353\262\204\354\240\204).md" @@ -0,0 +1,79 @@ +# 버블 정렬 (재귀 버전) + +Bubble Sort is one of the simplest sorting algorithms that compares two elements at a time and swaps them if they are in the wrong order. This process is repeated until the entire sequence is in order. + +## 절차 + +Base case: If the size of the array is 1, return. + +- We need to fix the last element of the current sub-array. For this, iterate over the entire array using normal Bubble Sort, and perform swapping. +- Next, call the function on the entire array excluding the last element(which was fixed by the iteration in the above step) +- Repeat until Base Case is reached. + +## 시간 복잡도 + +- 최선: +- 평균: + +## 공간 복잡도 + +- 최악: (참고: 기존 버블 정렬의 공간 복잡도는 ) + +## 예시 + +Let the given array be: `{5, 3, 2, 1, 4}` + +**First Iteration:** + +- {`5`, `3`, 2, 1, 4} -> {`3`, `5`, 2, 1, 4} Swap since `5 > 3` +- {3, `5`, `2`, 1, 4} -> {3, `2`, `5`, 1, 4} Swap since `5 > 2` +- {3, 2, `5`, `1`, 4} -> {3, 2, `1`, `5`, 4} Swap since `5 > 1` +- {3, 2, 1, `5`, `4`} -> {3, 2, 1, `4`, `5`} Swap since `5 > 4` + +This iteration has fixed the position of 5. Now, we will consider the array up to index 3. + +**Second Iteration:** + +- {`3`, `2`, 1, 4, 5} -> {`2`, `3`, 1, 4, 5} Swap since `3 > 2` +- {2, `3`, `1`, 4, 5} -> {2, `1`, `3`, 4, 5} Swap since `3 > 1` +- {2, 1, `3`, `4`, 5}; As `3 < 4`, do not swap + +Note: As we check one less element with every iteration, we do not need elements at index 3 and 4 i.e., `4` and `5`, as 5 is already in order. Formally, for an array with `n` integers, we consider elements only up to index `n - i`, where `i` is the iteration number. + +**Third Iteration:** + +- {`2`, `1`, 3, 4, 5} -> {`1`, `2`, 3, 4, 5} Swap since `1 > 2` +- {1, `2`, `3`, 4, 5}; As `2 < 3`, do not swap + +**Fourth Iteration:** + +- {`1`, `2`, 3, 4, 5}; As `1 < 2`, do not swap + +**Fifth Iteration:** + +- {`1`, 2, 3, 4, 5}; As the size of the array is 1, return. + +Note: This is the base case. + +## Pseudo Code + +``` +void bubbleSort(arr[], n) + if(n==1) + return; + + for(i = 0; i arr[i+1]) + swap(arr[i], arr[i+1]) + + bubbleSort(arr, n-1) +``` + +## 구현 + +- [Java](https://github.com/TheAlgorithms/Java/blob/master/Sorts/BubbleSortRecursion.java) +- [C](https://github.com/TheAlgorithms/C/blob/master/sorting/bubble_sort_recursion.c) + +## 영상 URL + +- [Programming Tutorials](https://www.youtube.com/watch?v=gDMDVLBfCI0) diff --git "a/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\353\262\204\353\270\224 \354\240\225\353\240\254.md" "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\353\262\204\353\270\224 \354\240\225\353\240\254.md" new file mode 100644 index 00000000..a24025dd --- /dev/null +++ "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\353\262\204\353\270\224 \354\240\225\353\240\254.md" @@ -0,0 +1,98 @@ +# 버블 정렬 + +## 문제 + +n개 원소로 구성된 배열이 주어졌을 때, 이 배열을 정렬하는 함수를 구하라. + +## 절차 + +- select the first element of the array +- compare it with its next element +- if it is larger than the next element then swap them +- else do nothing +- keep doing this for every index of the array +- repeat the above process n times. + +## 시간 복잡도 + +- 최악: +- 최선: +- 평균: + +## 공간 복잡도 + +- 최악: + +## 만든 사람 + +- [케네스 아이버슨](https://ko.wikipedia.org/wiki/%EC%BC%80%EB%84%A4%EC%8A%A4_%EC%95%84%EC%9D%B4%EB%B2%84%EC%8A%A8): "버블 정렬"이라는 용어를 1962년에 처음으로 사용했다. + +## 예시 + +``` +arr[] = {10, 80, 40, 30} +Indexes: 0 1 2 3 + +1. Index = 0, Number = 10 +2. 10 < 80, do nothing and continue + +3. Index = 1, Number = 80 +4. 80 > 40, swap 80 and 40 +5. The array now is {10, 40, 80, 30} + +6. Index = 2, Number = 80 +7. 80 > 30, swap 80 and 30 +8. The array now is {10, 40, 30, 80} + +Repeat the Above Steps again + +arr[] = {10, 40, 30, 80} +Indexes: 0 1 2 3 + +1. Index = 0, Number = 10 +2. 10 < 40, do nothing and continue + +3. Index = 1, Number = 40 +4. 40 > 30, swap 40 and 30 +5. The array now is {10, 30, 40, 80} + +6. Index = 2, Number = 40 +7. 40 < 80, do nothing +8. The array now is {10, 30, 40, 80} + +Repeat the Above Steps again + +arr[] = {10, 30, 40, 80} +Indexes: 0 1 2 3 + +1. Index = 0, Number = 10 +2. 10 < 30, do nothing and continue + +3. Index = 1, Number = 30 +4. 30 < 40, do nothing and continue + +5. Index = 2, Number = 40 +6. 40 < 80, do nothing + +Since there are no swaps in above steps, it means the array is sorted and we can stop here. +``` + +## 구현 + +- [Java](https://github.com/TheAlgorithms/Java/blob/master/Sorts/BubbleSort.java) +- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/bubble_sort.cpp) +- [Python](https://github.com/TheAlgorithms/Python/blob/master/sorts/bubble_sort.py) +- [C#](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms/Sorters/Comparison/BubbleSorter.cs) +- [Go](https://github.com/TheAlgorithms/Go/blob/master/sorts/bubblesort.go) +- [Ruby](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bubble_sort.rb) +- [C](https://github.com/TheAlgorithms/C/blob/master/sorting/bubble_sort.c) +- [Scala](https://github.com/TheAlgorithms/Scala/blob/master/src/main/scala/Sort/BubbleSort.scala) +- [JavaScript](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/BubbleSort.js) + +## 영상 URL + +- [mycodeschool](https://www.youtube.com/watch?v=Jdtq5uKz-w4) + +## 기타 + +- [Tute Board](https://boardhub.github.io/tute/?wd=bubbleSortAlgo2) diff --git "a/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\353\263\221\355\225\251 \354\240\225\353\240\254.md" "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\353\263\221\355\225\251 \354\240\225\353\240\254.md" new file mode 100644 index 00000000..93669e29 --- /dev/null +++ "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\353\263\221\355\225\251 \354\240\225\353\240\254.md" @@ -0,0 +1,45 @@ +# 병합 정렬 (분할 정복 알고리즘) + +## 문제 + +n개 원소로 구성된 배열이 주어졌을 때, 이 배열을 정렬하는 함수를 구하라. + +## 절차 + +- Find a mid point and divide the array into to halves based on the mid point +- Recursively call the merge sort function for both the halves +- Merge the two sorted halves to get the sorted array + +## 시간 복잡도 + +- + +## 공간 복잡도 + +- + +## 예시 + +``` +arr = [1, 3, 9, 5, 0, 2] + +Divide the array in two halves [1, 3, 9] and [5, 0, 2] + +Recursively call merge sort function for both these halves which will provide sorted halves +=> [1, 3, 9] & [0, 2, 5] + +Now merge both these halves to get the sorted array [0, 1, 2, 3, 5, 9] +``` + +## 구현 + +- [Java](https://github.com/TheAlgorithms/Java/blob/master/Sorts/MergeSort.java) +- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/merge_sort.cpp) +- [Python](https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_sort.py) +- [C#](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms/Sorters/Comparison/MergeSorter.cs) +- [C](https://github.com/TheAlgorithms/C/blob/master/sorting/merge_sort.c) +- [Ruby](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/merge_sort.rb) + +## 영상 URL + +- [CS50](https://www.youtube.com/watch?v=EeQ8pwjQxTM) diff --git "a/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\354\202\275\354\236\205 \354\240\225\353\240\254.md" "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\354\202\275\354\236\205 \354\240\225\353\240\254.md" new file mode 100644 index 00000000..0d66ae1a --- /dev/null +++ "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\354\202\275\354\236\205 \354\240\225\353\240\254.md" @@ -0,0 +1,66 @@ +# 삽입 정렬 + +## 문제 + +n개 원소로 구성된 배열이 주어졌을 때, 이 배열을 정렬하는 함수를 구하라. + +## 절차 + +- Define a "key" index, the subarray to the left of which is sorted. +- Initiate "key" as 1, ie. the second element of array(as there is only one element to left of the second element, which can be considered as sorted array with one element). +- If value of element at (key - 1) position is less than value of element at (key) position; increment "key". +- Else move elements of sorted subarray that are greater than value of element at "key" to one position ahead of their current position. Put the value of element at "key" in the newly created void. + +## 시간 복잡도 + +- 최악 + - 비교: + - 교환: +- 최선 + - 비교: + - 교환: + +## 공간 복잡도 + +- 최악: ([In-place 알고리즘](https://en.wikipedia.org/wiki/In-place_algorithm)으로, 추가적인 메모리 할당이 필요하지 않다) + +## 예시 + +``` + +12, 11, 13, 5, 6 + +Let us loop for i = 1 (second element of the array) to 4 (Size of input array) + +i = 1. +Since 11 is smaller than 12, move 12 and insert 11 before 12 +11, 12, 13, 5, 6 + +i = 2. +13 will remain at its position as all elements in sorted subarray are smaller than 13 +11, 12, 13, 5, 6 + +i = 3. +5 will move to the beginning, +and all other elements from 11 to 13 will move one position ahead of their current position. +5, 11, 12, 13, 6 + +i = 4. +6 will move to position after 5, +and elements from 11 to 13 will move one position ahead of their current position. +5, 6, 11, 12, 13 -- sorted array +``` + +## 구현 + +- [Java](https://github.com/TheAlgorithms/Java/blob/master/Sorts/InsertionSort.java) +- [C](https://github.com/TheAlgorithms/C/blob/master/sorting/insertion_sort.c) +- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/insertion_sort.cpp) +- [C#](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms/Sorters/Comparison/InsertionSorter.cs) +- [Scala](https://github.com/TheAlgorithms/Scala/blob/master/src/main/scala/Sort/InsertionSort.scala) +- [Python](https://github.com/TheAlgorithms/Python/blob/master/sorts/insertion_sort.py) +- [Ruby](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/insertion_sort.rb) + +## 영상 URL + +- [CS50](https://www.youtube.com/watch?v=DFG-XuyPYUQ) diff --git "a/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\354\204\240\355\203\235 \354\240\225\353\240\254.md" "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\354\204\240\355\203\235 \354\240\225\353\240\254.md" new file mode 100644 index 00000000..073421eb --- /dev/null +++ "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\354\204\240\355\203\235 \354\240\225\353\240\254.md" @@ -0,0 +1,67 @@ +# 선택 정렬 + +## 문제 + +n개 원소로 구성된 배열이 주어졌을 때, 이 배열을 정렬하는 함수를 구하라. + +## 절차 + +- select the smallest element from the array +- put it at the beginning of the array +- then select the smallest array from the remaining unsorted list +- append it to the sorted array at the beginning +- keep doing this for every element of the array +- repeat the above process n times + +## 시간 복잡도 + +- 최악: +- 최선: +- 평균: + +## 공간 복잡도 + +- 최악: + +## 예시 + +``` +arr[] = {80, 10, 40, 30} +Indexes: 0 1 2 3 + +1. Index = 0 + Select the minimum number from the array (between index 0-3), ie, 10 +2. Swap 10 and 80 (arr[0]) +3. The array now is {10, 80, 40, 30} + +4. Index = 1 + Select the minimum number from the array (between index 1-3), ie, 30 +5. Swap 30 and 80 (arr[1]) +6. The array now is {10, 30, 40, 80} + +7. Index = 2 + Select the minimum number from the array (between index 2-3), ie, 40 +8. Swap 40 and 40 (arr[2]) +9. The array now is {10, 30, 40, 80} + +The array is now sorted. +``` + +## 구현 + +- [Java](https://github.com/TheAlgorithms/Java/blob/master/Sorts/SelectionSort.java) +- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/Sorting/Selection%20Sort.cpp) +- [Python](https://github.com/TheAlgorithms/Python/blob/master/sorts/selection_sort.py) +- [Go](https://github.com/TheAlgorithms/Go/blob/master/sorts/selection_sort.go) +- [Ruby](https://github.com/TheAlgorithms/Ruby/blob/master/Sorting/selection_sort.rb) +- [C](https://github.com/TheAlgorithms/C/blob/master/sorting/SelectionSort.c) +- [Scala](https://github.com/TheAlgorithms/Scala/blob/master/src/main/scala/Sort/SelectionSort.scala) +- [JavaScript](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/selectionSort.js) + +## 영상 URL + +- [CS50](https://www.youtube.com/watch?v=f8hXR_Hvybo) + +## 기타 + +- [Tute Board](https://boardhub.github.io/tute/?wd=selectSortAlgo2) diff --git "a/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\354\205\270 \354\240\225\353\240\254.md" "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\354\205\270 \354\240\225\353\240\254.md" new file mode 100644 index 00000000..9579258f --- /dev/null +++ "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\354\205\270 \354\240\225\353\240\254.md" @@ -0,0 +1,68 @@ +# 셸 정렬 + +## 문제 + +n개 원소로 구성된 배열이 주어졌을 때, 이 배열을 정렬하는 함수를 구하라. + +## 절차 + +- start with the initial gap, g +- go through the first (n - g) elements in the array +- compare the element with the next element that is g distance away +- swap the two elements if the first element is bigger +- decrease the gap and repeat until gap = 1 + +## 시간 복잡도 + +셸 정렬의 시간 복잡도는 gap sequences에 따라 다르다. 아래 시간 복잡도는 의 gap sequences를 가정한다. + +- 최악: +- 최선: +- 평균: + +## 공간 복잡도 + +- 최악: + +## 만든 사람 + +- [도널드 셸](https://en.wikipedia.org/wiki/Donald_Shell) + +## 예시 + +``` +arr[] = {61, 109, 149, 111, 34, 2, 24, 119} +Initial Gap: 4 + +1. Index = 0, Next element index = 4 +2. 61 > 34, swap 61 and 34 +3. The array is now {34, 109, 149, 111, 61, 2, 24, 119} + +4. Index = 1, Next element index = 5 +5. 109 > 2, swap 109 and 2 +6. The array is now {34, 2, 149, 111, 61, 109, 24, 119} + +7. Index = 2, Next element index = 6 +8. 149 > 24, swap 149 and 24 +9. The array is now {34, 2, 24, 111, 61, 109, 149, 119} + +10. Index = 3, Next element index = 7 +11. 111 < 119, do nothing and continue + +12. Divide the gap by 2 and repeat until gap = 1 +``` + +## 구현 + +- [Java](https://github.com/TheAlgorithms/Java/blob/master/Sorts/ShellSort.java) +- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/Sorting/Shell%20Sort.cpp) +- [Python](https://github.com/TheAlgorithms/Python/blob/master/sorts/shell_sort.py) +- [C#](https://github.com/TheAlgorithms/C-Sharp/blob/master/sorts/shell_sort.cs) +- [Go](https://github.com/TheAlgorithms/Go/blob/master/sorts/shell_sort.go) +- [Ruby](https://github.com/TheAlgorithms/Ruby/blob/master/Sorting/shell_sort.rb) +- [C](https://github.com/TheAlgorithms/C/blob/master/sorting/shellSort.c) +- [JavaScript](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/shellSort.js) + +## 영상 URL + +- [Computer Education for All](https://www.youtube.com/watch?v=H8NiFkGu2PY) diff --git "a/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\355\200\265 \354\240\225\353\240\254.md" "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\355\200\265 \354\240\225\353\240\254.md" new file mode 100644 index 00000000..e9ff369b --- /dev/null +++ "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\355\200\265 \354\240\225\353\240\254.md" @@ -0,0 +1,79 @@ +# 퀵 정렬 + +## 문제 + +n개 원소로 구성된 배열이 주어졌을 때, 이 배열을 정렬하는 함수를 구하라. + +## 절차 + +- Make the right-most index value pivot +- partition the array using pivot value +- quicksort left partition recursively +- quicksort right partition recursively + +## 시간 복잡도 + +- 최악: +- 최선: +- 평균: + +## 공간 복잡도 + +- 최악: + +## 만든 사람 + +- [토니 호어](https://ko.wikipedia.org/wiki/%ED%86%A0%EB%8B%88_%ED%98%B8%EC%96%B4) + +## 예시 + +``` +arr[] = {10, 80, 30, 90, 40, 50, 70} +Indexes: 0 1 2 3 4 5 6 + +low = 0, high = 6, pivot = arr[h] = 70 +Initialize index of smaller element, i = -1 + +Traverse elements from j = low to high-1 +j = 0 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j]) +i = 0 +arr[] = {10, 80, 30, 90, 40, 50, 70} // No change as i and j + // are same + +j = 1 : Since arr[j] > pivot, do nothing +// No change in i and arr[] + +j = 2 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j]) +i = 1 +arr[] = {10, 30, 80, 90, 40, 50, 70} // We swap 80 and 30 + +j = 3 : Since arr[j] > pivot, do nothing +// No change in i and arr[] + +j = 4 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j]) +i = 2 +arr[] = {10, 30, 40, 90, 80, 50, 70} // 80 and 40 Swapped +j = 5 : Since arr[j] <= pivot, do i++ and swap arr[i] with arr[j] +i = 3 +arr[] = {10, 30, 40, 50, 80, 90, 70} // 90 and 50 Swapped + +We come out of loop because j is now equal to high-1. +Finally we place pivot at correct position by swapping +arr[i+1] and arr[high] (or pivot) +arr[] = {10, 30, 40, 50, 70, 90, 80} // 80 and 70 Swapped + +Now 70 is at its correct place. All elements smaller than +70 are before it and all elements greater than 70 are after +it. +``` + +## 구현 + +- [Java](https://github.com/TheAlgorithms/Java/blob/master/Sorts/QuickSort.java) +- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/Sorting/Quick%20Sort.cpp) +- [Python](https://github.com/TheAlgorithms/Python/blob/master/sorts/quick_sort.py) +- [Ruby](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/quicksort.rb) + +## 영상 URL + +- [mycodeschool](https://www.youtube.com/watch?v=COk73cpQbFQ) diff --git "a/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\355\236\231 \354\240\225\353\240\254.md" "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\355\236\231 \354\240\225\353\240\254.md" new file mode 100644 index 00000000..898c08e8 --- /dev/null +++ "b/ko/\354\240\225\353\240\254 \354\225\214\352\263\240\353\246\254\354\246\230/\355\236\231 \354\240\225\353\240\254.md" @@ -0,0 +1,70 @@ +# 힙 정렬 + +## 문제 + +n개 원소로 구성된 배열이 주어졌을 때, 이 배열을 정렬하는 함수를 구하라. + +## 절차 + +- Build a max heap from the input data. +- At this point, the largest item is stored at the root of the heap. Replace it with the last item of the heap followed by reducing the size of heap by 1. Finally, heapify the root of tree. +- Repeat above steps while size of heap is greater than 1. + +## 시간 복잡도 + +- 최악: +- 최선: + distinct keys일 때 , + equal keys일 때 +- 평균: + +## 공간 복잡도 + +- 최악: + +## 예시 + +``` +Input data: 4, 10, 3, 5, 1 + 4(0) + / \ + 10(1) 3(2) + / \ +5(3) 1(4) + +The numbers in bracket represent the indices in the array +representation of data. + +Applying heapify procedure to index 1: + 4(0) + / \ + 10(1) 3(2) + / \ +5(3) 1(4) + +Applying heapify procedure to index 0: + 10(0) + / \ + 5(1) 3(2) + / \ +4(3) 1(4) +The heapify procedure calls itself recursively to build heap +in top down manner. +``` + +![heap-image](https://upload.wikimedia.org/wikipedia/commons/1/1b/Sorting_heapsort_anim.gif 'Heap Sort') + +## 구현 + +- [Java](https://github.com/TheAlgorithms/Java/blob/master/Sorts/HeapSort.java) +- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/heap_sort.cpp) +- [Python](https://github.com/TheAlgorithms/Python/blob/master/sorts/heap_sort.py) +- [Go](https://github.com/TheAlgorithms/Go/blob/master/sorts/heapsort.go) +- [Ruby](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/heap_sort.rb) +- [C#](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms/Sorters/Comparison/HeapSorter.cs) +- [C](https://github.com/TheAlgorithms/C/blob/master/sorting/heap_sort.c) +- [JavaScript](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/HeapSort.js) + +## 영상 URL + +[GeeksforGeeks](https://www.youtube.com/watch?v=MtQL_ll5KhQ) diff --git "a/ko/\355\203\220\354\203\211 \354\225\214\352\263\240\353\246\254\354\246\230/\354\204\240\355\230\225 \355\203\220\354\203\211.md" "b/ko/\355\203\220\354\203\211 \354\225\214\352\263\240\353\246\254\354\246\230/\354\204\240\355\230\225 \355\203\220\354\203\211.md" new file mode 100644 index 00000000..27fdc324 --- /dev/null +++ "b/ko/\355\203\220\354\203\211 \354\225\214\352\263\240\353\246\254\354\246\230/\354\204\240\355\230\225 \355\203\220\354\203\211.md" @@ -0,0 +1,59 @@ +# 선형 탐색 + +## 문제 + +n개 원소로 구성된 배열이 주어졌을 때, 특정 원소의 인덱스를 찾는 함수를 구하라. + +## 절차 + +1. 배열의 첫 원소와 찾고자 하는 원소를 비교한다. +2. 만약 같다면 그 인덱스를 반환한다. + - 만약 다르다면, 다음 원소와 찾고자 하는 원소를 비교한다. +3. 만약 찾고자 하는 원소가 없다면 -1을 반환한다. + +## 시간 복잡도 + +- 최악: +- 최선: (첫 원소가 찾고자 하는 원소일 때) + +## 공간 복잡도 + +- + +## 예시 + +``` +arr = [1, 3, 9, 5, 0, 2] +target = 5 + +1과 5가 같지 않으므로 다음 원소와 목표를 비교한다. +3과 5가 같지 않으므로 다음 원소와 목표를 비교한다. +9과 5가 같지 않으므로 다음 원소와 목표를 비교한다. +5가 4번째 위치에 있으므로 3을 반환한다. +``` + +``` +arr = [1, 3, 9, 5, 0, 2] +target = 6 + +1과 6이 같지 않으므로 다음 원소와 목표를 비교한다. +3과 6이 같지 않으므로 다음 원소와 목표를 비교한다. +9과 6이 같지 않으므로 다음 원소와 목표를 비교한다. +5와 6이 같지 않으므로 다음 원소와 목표를 비교한다. +0과 6이 같지 않으므로 다음 원소와 목표를 비교한다. +마지막 원소가 목표와 같지 않으므로 6이 배열에 들어있지 않다고 판단하여 -1을 반환한다. +``` + +## 구현 + +- [Java](https://github.com/TheAlgorithms/Java/blob/master/Searches/LinearSearch.java) +- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/Search/Linear%20Search.cpp) +- [Python](https://github.com/TheAlgorithms/Python/blob/master/searches/linear_search.py) + +## 영상 URL + +[CS50](https://www.youtube.com/watch?v=CX2CYIJLwfg) + +## 기타 + +- [Tute Board](https://boardhub.github.io/tute/?wd=linearSearchAlgo) diff --git "a/ko/\355\203\220\354\203\211 \354\225\214\352\263\240\353\246\254\354\246\230/\354\235\264\354\247\204 \355\203\220\354\203\211.md" "b/ko/\355\203\220\354\203\211 \354\225\214\352\263\240\353\246\254\354\246\230/\354\235\264\354\247\204 \355\203\220\354\203\211.md" new file mode 100644 index 00000000..ddb5dde1 --- /dev/null +++ "b/ko/\355\203\220\354\203\211 \354\225\214\352\263\240\353\246\254\354\246\230/\354\235\264\354\247\204 \355\203\220\354\203\211.md" @@ -0,0 +1,69 @@ +# 이진 탐색 (분할 정복 알고리즘) + +## 문제 + +n개 원소로 구성된 **정렬된** 배열이 주어졌을 때, 특정 원소의 인덱스를 찾는 함수를 구하라. + +## 절차 + +배열을 반복적으로 절반으로 나누어 배열을 탐색한다. + +1. 주어진 배열의 중간 원소를 고르고, 인덱스의 최소값과 최대값을 저장한다. +2. 만약 중간 원소가 찾고자 하는 원소와 같다면 중간 원소의 인덱스를 반환한다. + - 만약 중간 원소가 찾고자 하는 원소보다 크다면, 배열의 왼쪽 절반만 고려한다. + - 만약 중간 원소가 찾고자 하는 원소보다 작다면, 배열의 오른쪽 절반만 고려한다. +3. 만약 찾고자 하는 원소가 없다면 -1을 반환한다. + +## 시간 복잡도 + +- 최악: +- 최선: (중간 원소가 찾고자 하는 원소일 때) + +## 공간 복잡도 + +- 반복문 사용 시: +- 재귀적인 방법 사용 시: + +## 예시 + +``` +arr = [1, 2, 3, 4, 5, 6, 7] +target = 2 + +중간 원소는 4로, 목표인 2보다 크다. 그러므로 배열의 왼쪽 절반을 탐색한다. + +arr = [1, 2, 3] + +중간 원소가 목표와 같으므로 이 원소의 인덱스를 반환한다. +``` + +``` +arr = [1, 2, 3, 4, 5, 6, 7] +target = 9 + +중간 원소는 4로, 목표인 9보다 작다. 그러므로 배열의 오른쪽 절반을 탐색한다. + +arr = [5, 6, 7] + +중간 원소는 6으로, 목표보다 작다. 그러므로 오른쪽 절반을 탐색한다. + +arr = [7] + +유일한 원소가 목표와 같지 않으므로 9가 배열에 들어있지 않다고 판단하여 -1을 반환한다. +``` + +## 구현 + +- [Java](https://github.com/TheAlgorithms/Java/blob/master/Searches/BinarySearch.java) +- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/Search/Binary%20Search.cpp) +- [Python](https://github.com/TheAlgorithms/Python/blob/master/searches/binary_search.py) +- [C#](https://github.com/TheAlgorithms/C-Sharp/blob/master/searches/binary_search.cs) +- [C](https://github.com/TheAlgorithms/C/blob/master/searching/Binary_Search.c) + +## 영상 URL + +- [CS50](https://www.youtube.com/watch?v=5xlIPT1FRcA) + +## 기타 + +- [Tute Board](https://boardhub.github.io/tute/?wd=binarySearchAlgo2) diff --git "a/ko/\355\203\220\354\203\211 \354\225\214\352\263\240\353\246\254\354\246\230/\354\247\200\354\210\230 \355\203\220\354\203\211.md" "b/ko/\355\203\220\354\203\211 \354\225\214\352\263\240\353\246\254\354\246\230/\354\247\200\354\210\230 \355\203\220\354\203\211.md" new file mode 100644 index 00000000..e3580eef --- /dev/null +++ "b/ko/\355\203\220\354\203\211 \354\225\214\352\263\240\353\246\254\354\246\230/\354\247\200\354\210\230 \355\203\220\354\203\211.md" @@ -0,0 +1,62 @@ +# 지수 탐색 + +## 알아야 하는 내용 + +- [이진 탐색](./이진%20탐색.md) + +## 문제 + +n개 원소로 구성된 **정렬된** 배열이 주어졌을 때, 특정 원소의 인덱스를 찾는 함수를 구하라. + +## 절차 + +1. 인덱스를 2배씩 늘려가며 찾고자 하는 원소가 속하는 범위를 찾는다. +2. 만약 찾고자 하는 원소가 속하는 범위를 찾는다면, 해당 범위에서 이진 탐색을 수행한다. +3. 만약 찾고자 하는 원소가 속하는 범위가 없다면 -1을 반환한다. + +## 시간 복잡도 + +- 최악: ($i$는 찾고자 하는 원소의 인덱스) +- 최선: + +$i$가 찾고자 하는 원소의 인덱스라고 할 때, 탐색을 $\lceil \log i \rceil$번 수행하기 때문에 찾고자 하는 원소가 속하는 범위를 찾는 과정의 복잡도는 $O(\log i)$이다. + +이진 탐색을 수행하는 과정의 복잡도는 이진 탐색의 복잡도와 동일하게 $O(\log i)$이다. (참고: [이진 탐색](./이진%20탐색.md)) + +그러므로, 지수 탐색의 복잡도는 다음과 같이 쓸 수 있다. + + + +## 예시 + +``` +arr = [1, 2, 3, 4, 5, 6, 7, ... 998, 999, 1000] +target = 998 + +1. 찾고자 하는 원소가 속하는 범위 탐색 + arr[1]이 998보다 작으므로 인덱스를 2배 늘린다. + arr[2]이 998보다 작으므로 인덱스를 2배 늘린다. + arr[4]이 998보다 작으므로 인덱스를 2배 늘린다. + arr[8]이 998보다 작으므로 인덱스를 2배 늘린다. + arr[16]이 998보다 작으므로 인덱스를 2배 늘린다. + arr[32]이 998보다 작으므로 인덱스를 2배 늘린다. + arr[64]이 998보다 작으므로 인덱스를 2배 늘린다. + arr[128]이 998보다 작으므로 인덱스를 2배 늘린다. + arr[256]이 998보다 작으므로 인덱스를 2배 늘린다. + arr[512]이 998보다 작으므로 인덱스를 2배 늘린다. + arr[1024]가 존재하지 않으므로 512 이상의 범위에 대해 이진 탐색을 수행한다. + +2. 이진 탐색 +``` + +## 이진 탐색과의 비교 + +논의의 편의를 위해 덜 이론적인 예시를 살펴보자. 1,000,000개 원소로 이루어진 배열에서 4번째에 위치한 원소를 찾는다고 할 때, + +- 이진 탐색은 배열의 중간부터 시작하여 많은 반복문을 지나야 4번째 원소에 도착한다. +- 지수 탐색은 2번 만에 4번째 원소에 도착한다. + +## 구현 + +- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/exponential_search.cpp) +- [JavaScript](https://github.com/TheAlgorithms/Javascript/blob/master/Search/ExponentialSearch.js)