├── .gitignore ├── Assignments ├── Week 1 │ ├── 김현영 │ │ ├── BubbleSort_김현영.c │ │ ├── InsertionSort_김현영.c │ │ ├── MergeSort(LinkedList)_김현영.c │ │ ├── MergeSort(array)_김현영.c │ │ ├── QuickSort_김현영.c │ │ ├── SelectionSort_김현영.c │ │ └── 김현영_1주차_보고서.pdf │ ├── 박상민 │ │ ├── BubbleSort_박상민.c │ │ ├── InsertionSort_박상민.c │ │ ├── MergeSort_박상민.c │ │ ├── QuickSort_박상민.c │ │ ├── SelectionSort_박상민.c │ │ └── 박상민_1주차_보고서.pdf │ ├── 이기은 │ │ ├── BubbleSort_이기은.c │ │ ├── InsertionSort_이기은.c │ │ ├── MergeSort_이기은.c │ │ ├── QuickSort_이기은.c │ │ ├── SelectionSort_이기은.c │ │ └── 이기은_1주차_보고서.pdf │ ├── 이다예 │ │ ├── BubbleSort_이다예.c │ │ ├── InsertionSort_이다예.c │ │ ├── SelectionSort_이다예.c │ │ └── 이다예_1주차_보고서.pdf │ ├── 이승훈 │ │ ├── BubbleSort_이승훈.c │ │ ├── InsertionSort_이승훈.c │ │ ├── MergeSort_이승훈.c │ │ ├── QuickSort_이승훈.c │ │ ├── SelectionSort_이승훈.c │ │ └── 이승훈_1주차_보고서.pdf │ ├── 이우식 │ │ ├── BubbleSort_이우식.c │ │ ├── InsertionSort_이우식.c │ │ ├── QuickSort_이우식.c │ │ ├── SelectionSort_이우식.c │ │ └── 이우식_1주차_보고서.pdf │ ├── 전선영 │ │ ├── BubbleSort_전선영.c │ │ ├── InsertionSort_전선영.c │ │ ├── MergeSort(TimeCheck)_전선영.c │ │ ├── MergeSort_전선영.c │ │ ├── QuickSort_전선영.c │ │ ├── SelectionSort_전선영.c │ │ └── 전선영_1주차_보고서.pdf │ ├── 정지호 │ │ ├── BubbleSort_정지호.c │ │ ├── InsertSort_정지호.c │ │ ├── MergeSort_정지호.c │ │ ├── QuickSort_정지호.c │ │ ├── SelectionSort_정지호.c │ │ ├── 정지호_1주차_보고서.pdf │ │ └── 정지호_1주차_보고서.xlsx │ └── 허정민 │ │ ├── BubbleSort_허정민.c │ │ ├── InsertionSort_허정민.c │ │ ├── MergeSort_허정민.c │ │ ├── QuickSort_허정민.c │ │ ├── SelectionSort_허정민.c │ │ └── 허정민_1주차_보고서.pdf ├── Week 2 │ ├── 김현영 │ │ ├── BST_김현영.c │ │ ├── Baekjoon_11286_김현영.c │ │ ├── Baekjoon_5639_김현영.c │ │ └── HeapSort_김현영.c │ ├── 박상민 │ │ ├── bst 삭제,insert,print.c │ │ ├── heap_sort.c │ │ ├── 상미인.c │ │ └── 절댓값힙.c │ ├── 이승훈 │ │ ├── BST.c │ │ └── Heap.c │ ├── 이우식 │ │ ├── ABS.c │ │ ├── Bst.c │ │ └── Hsort.c │ ├── 전선영 │ │ ├── 11286번_전선영.c │ │ ├── 5693번_전선영.c │ │ └── HeapSort_전선영.c │ ├── 정지호 │ │ ├── BST_정지호.c │ │ ├── Beakjoon_5936_정지호.c │ │ ├── HeapSort_정지호.c │ │ └── beakjoon_11286_정지호.c │ └── 허정민 │ │ ├── AbsoluteHeap_허정민.c │ │ ├── BST_허정민.c │ │ └── HeapSort_허정민.c └── Week 3 │ └── 전선영 │ └── AVL_Tree_전선영.c ├── CMakeLists.txt ├── Lecture Notes ├── 알고리즘 0강.pdf ├── 알고리즘 1강.pdf ├── 알고리즘 2강.pdf └── 알고리즘 3강.pdf ├── README.md ├── Sample Codes ├── Week 1 │ ├── BubbleSort.c │ ├── InsertionSort.c │ ├── MergeSort(Array).c │ ├── MergeSort(LinkedList).c │ ├── Power.c │ ├── QuickSort(Array).c │ └── SelectionSort.c ├── Week 2 │ ├── AbsoluteHeap.c │ ├── BST.c │ ├── BST_Baekjoon.c │ ├── HeapSort.c │ ├── MaxHeap.c │ └── MinHeap.c ├── Week 3 │ └── AVLTree.c └── Week 4 │ ├── DuplicateKeysMergeSort.c │ ├── DuplicateKeysQuickSort.c │ └── PaperFold.c └── main.c /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/clion,clion+all 3 | # Edit at https://www.gitignore.io/?templates=clion,clion+all 4 | 5 | ### CLion ### 6 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 7 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 8 | 9 | # User-specific stuff 10 | .idea/**/workspace.xml 11 | .idea/**/tasks.xml 12 | .idea/**/usage.statistics.xml 13 | .idea/**/dictionaries 14 | .idea/**/shelf 15 | 16 | # Generated files 17 | .idea/**/contentModel.xml 18 | 19 | # Sensitive or high-churn files 20 | .idea/**/dataSources/ 21 | .idea/**/dataSources.ids 22 | .idea/**/dataSources.local.xml 23 | .idea/**/sqlDataSources.xml 24 | .idea/**/dynamic.xml 25 | .idea/**/uiDesigner.xml 26 | .idea/**/dbnavigator.xml 27 | 28 | # Gradle 29 | .idea/**/gradle.xml 30 | .idea/**/libraries 31 | 32 | # Gradle and Maven with auto-import 33 | # When using Gradle or Maven with auto-import, you should exclude module files, 34 | # since they will be recreated, and may cause churn. Uncomment if using 35 | # auto-import. 36 | # .idea/modules.xml 37 | # .idea/*.iml 38 | # .idea/modules 39 | # *.iml 40 | # *.ipr 41 | 42 | # CMake 43 | cmake-build-*/ 44 | 45 | # Mongo Explorer plugin 46 | .idea/**/mongoSettings.xml 47 | 48 | # File-based project format 49 | *.iws 50 | 51 | # IntelliJ 52 | out/ 53 | 54 | # mpeltonen/sbt-idea plugin 55 | .idea_modules/ 56 | 57 | # JIRA plugin 58 | atlassian-ide-plugin.xml 59 | 60 | # Cursive Clojure plugin 61 | .idea/replstate.xml 62 | 63 | # Crashlytics plugin (for Android Studio and IntelliJ) 64 | com_crashlytics_export_strings.xml 65 | crashlytics.properties 66 | crashlytics-build.properties 67 | fabric.properties 68 | 69 | # Editor-based Rest Client 70 | .idea/httpRequests 71 | 72 | # Android studio 3.1+ serialized cache file 73 | .idea/caches/build_file_checksums.ser 74 | 75 | ### CLion Patch ### 76 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 77 | 78 | # *.iml 79 | # modules.xml 80 | # .idea/misc.xml 81 | # *.ipr 82 | 83 | # Sonarlint plugin 84 | .idea/sonarlint 85 | 86 | ### CLion+all ### 87 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 88 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 89 | 90 | # User-specific stuff 91 | 92 | # Generated files 93 | 94 | # Sensitive or high-churn files 95 | 96 | # Gradle 97 | 98 | # Gradle and Maven with auto-import 99 | # When using Gradle or Maven with auto-import, you should exclude module files, 100 | # since they will be recreated, and may cause churn. Uncomment if using 101 | # auto-import. 102 | # .idea/modules.xml 103 | # .idea/*.iml 104 | # .idea/modules 105 | # *.iml 106 | # *.ipr 107 | 108 | # CMake 109 | 110 | # Mongo Explorer plugin 111 | 112 | # File-based project format 113 | 114 | # IntelliJ 115 | 116 | # mpeltonen/sbt-idea plugin 117 | 118 | # JIRA plugin 119 | 120 | # Cursive Clojure plugin 121 | 122 | # Crashlytics plugin (for Android Studio and IntelliJ) 123 | 124 | # Editor-based Rest Client 125 | 126 | # Android studio 3.1+ serialized cache file 127 | 128 | ### CLion+all Patch ### 129 | # Ignores the whole .idea folder and all .iml files 130 | # See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 131 | 132 | .idea/ 133 | 134 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 135 | 136 | *.iml 137 | modules.xml 138 | .idea/misc.xml 139 | *.ipr 140 | 141 | # Sonarlint plugin 142 | 143 | # End of https://www.gitignore.io/api/clion,clion+all -------------------------------------------------------------------------------- /Assignments/Week 1/김현영/BubbleSort_김현영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int i, j, n, tmp; 6 | int* arr; 7 | 8 | scanf("%d", &n); 9 | arr = (int*)malloc(sizeof(int) * n); 10 | 11 | for (i = 0; i < n; i++) 12 | scanf("%d", &arr[i]); 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | for (j = 0; j < n - 1; j++) 17 | { 18 | if (arr[j] > arr[j + 1]) 19 | { 20 | tmp = arr[j]; 21 | arr[j] = arr[j + 1]; 22 | arr[j + 1] = tmp; 23 | } 24 | } 25 | } 26 | 27 | for (i = 0; i < n; i++) 28 | printf("%d ", arr[i]); 29 | 30 | 31 | 32 | free(arr); 33 | } 34 | -------------------------------------------------------------------------------- /Assignments/Week 1/김현영/InsertionSort_김현영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int i, j, k, n,index, tmp; 6 | int *arr; 7 | 8 | scanf("%d", &n); 9 | arr = (int*)malloc(sizeof(int)*n); 10 | for (i = 0; i < n; i++) 11 | scanf("%d", &arr[i]); 12 | 13 | for (i = 1; i < n; i++) 14 | { 15 | index = i; 16 | for (j = i-1; j > -1; j--) 17 | { 18 | if (arr[index] < arr[j]) 19 | { 20 | tmp = arr[index]; 21 | arr[index] = arr[j]; 22 | arr[j] = tmp; 23 | index--; 24 | } 25 | } 26 | } 27 | 28 | 29 | for (i = 0; i < n; i++) 30 | printf("%d ", arr[i]); 31 | 32 | free(arr); 33 | 34 | } -------------------------------------------------------------------------------- /Assignments/Week 1/김현영/MergeSort(LinkedList)_김현영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | typedef struct Node { 6 | int data; 7 | struct Node *next; 8 | }Node; 9 | 10 | Node* create_node(int data) { 11 | Node *node = (Node*)malloc(sizeof(Node)); 12 | node->data = data; 13 | node->next = NULL; 14 | return node; 15 | } 16 | void makelist(Node** arr); 17 | Node* merge_sort(Node* left, Node* right, int first, int end); 18 | Node* Divide(Node* arr, int first, int end); 19 | 20 | int main() 21 | { 22 | int i, n; 23 | Node *arr = NULL; 24 | scanf("%d", &n); 25 | for (i = 0; i < n; i++) 26 | makelist(&arr); 27 | 28 | arr = Divide(arr, 0, n - 1); 29 | while (arr) { 30 | printf("%d ", arr->data); 31 | arr = arr->next; 32 | } 33 | free(arr); 34 | } 35 | 36 | void makelist(Node** arr) 37 | { 38 | int num; 39 | Node* p = *arr; 40 | scanf("%d", &num); 41 | if (p == NULL) 42 | * arr = create_node(num); 43 | else { 44 | while (p->next) 45 | p = p->next; 46 | p->next = create_node(num); 47 | } 48 | } 49 | 50 | Node* merge_sort(Node* left, Node* right, int first, int end) 51 | { 52 | Node* tmp = NULL, * p = tmp; 53 | int size, mid; 54 | while (1) { 55 | if (left->data < right->data) 56 | { 57 | if (tmp == NULL) { 58 | tmp = create_node(left->data); 59 | p = tmp; 60 | } 61 | else 62 | { 63 | tmp->next = create_node(left->data); 64 | tmp = tmp->next; 65 | } 66 | left = left->next; 67 | } 68 | else 69 | { 70 | if (tmp == NULL) { 71 | tmp = create_node(right->data); 72 | p = tmp; 73 | } 74 | else 75 | { 76 | tmp->next = create_node(right->data); 77 | tmp = tmp->next; 78 | } 79 | right = right->next; 80 | } 81 | if (left == NULL || right == NULL) break; 82 | } 83 | while (left) 84 | { 85 | tmp->next = create_node(left->data); 86 | tmp = tmp->next; 87 | left = left->next; 88 | } 89 | 90 | while (right) { 91 | tmp->next = create_node(right->data); 92 | tmp = tmp->next; 93 | right = right->next; 94 | } 95 | return p; 96 | } 97 | 98 | Node* Divide(Node* arr, int first, int end) { 99 | int i; 100 | int mid = (end + first) / 2; 101 | Node* left, * right, * temp; 102 | if (end - first == 0) return arr; 103 | left = arr; 104 | right = arr; 105 | for (i = first; i < mid; i++) { 106 | right = right->next; 107 | arr = arr->next; 108 | } 109 | right = right->next; 110 | arr->next = NULL; 111 | 112 | left = Divide(left, first, mid); 113 | right = Divide(right, mid + 1, end); 114 | left = merge_sort(left, right, first, end); 115 | return left; 116 | } -------------------------------------------------------------------------------- /Assignments/Week 1/김현영/MergeSort(array)_김현영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void Divide(int *arr, int front, int end); 5 | void merge_sort(int *arr, int front, int end); 6 | 7 | int main() { 8 | int i, j, n, tmp; 9 | int *arr; 10 | 11 | scanf("%d", &n); 12 | arr = (int*)malloc(sizeof(int)*n); 13 | 14 | for (i = 0; i < n; i++) 15 | scanf("%d", &arr[i]); 16 | 17 | Divide(arr, 0, n - 1); 18 | 19 | for (i = 0; i < n; i++) 20 | printf("%d ", arr[i]); 21 | 22 | free(arr); 23 | } 24 | 25 | 26 | void Divide(int *arr, int first, int end) 27 | { 28 | int mid; 29 | mid = (end + first) / 2; 30 | if (end - first == 0) return; 31 | 32 | Divide(arr, first, mid); 33 | Divide(arr, mid + 1, end); 34 | merge_sort(arr, first, end); 35 | } 36 | void merge_sort(int *arr, int first, int end) 37 | { 38 | int i = 0, size, mid, f, m, *tmp; 39 | size = end - first + 1; 40 | mid = (end + first) / 2 + 1; 41 | f = first; 42 | m = mid; 43 | tmp = (int*)malloc(sizeof(int)*size); 44 | 45 | while (f < mid && m <= end) 46 | { 47 | if (arr[f] > arr[m]) 48 | tmp[i++] = arr[m++]; 49 | else 50 | tmp[i++] = arr[f++]; 51 | } 52 | 53 | while (f < mid) 54 | tmp[i++] = arr[f++]; 55 | while (m <= end) 56 | tmp[i++] = arr[m++]; 57 | 58 | for (i = 0; i < size; i++) 59 | arr[first++] = tmp[i]; 60 | 61 | free(tmp); 62 | } 63 | -------------------------------------------------------------------------------- /Assignments/Week 1/김현영/QuickSort_김현영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void quick(int* arr, int left, int right); 5 | 6 | int main() { 7 | int i, n, * arr; 8 | scanf("%d ", &n); 9 | arr = (int*)malloc(sizeof(int) * n); 10 | for (i = 0; i < n; i++) 11 | scanf("%d", &arr[i]); 12 | 13 | 14 | quick(arr, 0, n - 1); 15 | 16 | for (i = 0; i < n; i++) 17 | printf("%d ", arr[i]); 18 | 19 | } 20 | 21 | void quick(int* arr, int left, int right) 22 | { 23 | int i, temp, pivot, l = left + 1, r = right; 24 | int size = right - left + 1; 25 | 26 | if (size == 1) return; 27 | 28 | pivot = arr[left]; 29 | 30 | while (l < r) { 31 | if (arr[r] > pivot) r--; 32 | if (arr[l] <= pivot) l++; 33 | if (r < l) break; 34 | if (arr[l] > pivot && arr[r] <= pivot) 35 | { 36 | temp = arr[l]; 37 | arr[l] = arr[r]; 38 | arr[r] = temp; 39 | l++; r--; 40 | } 41 | } 42 | 43 | if ((l == r && (pivot > arr[l])) || (r < l)) 44 | { 45 | temp = arr[r]; 46 | arr[r] = arr[left]; 47 | arr[left] = temp; 48 | } 49 | if (r != left) {quick(arr, left, r - 1);} 50 | if (r == l) { quick(arr, r, right); } 51 | else quick(arr, r + 1, right); 52 | 53 | } -------------------------------------------------------------------------------- /Assignments/Week 1/김현영/SelectionSort_김현영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int i, j, n, min, min_index = 0, tmp; 6 | int *arr; 7 | 8 | scanf("%d", &n); 9 | arr = (int*)malloc(sizeof(int)*n); 10 | for (i = 0; i < n; i++) 11 | scanf("%d", &arr[i]); 12 | 13 | for (i = 0; i < n; i++) 14 | { 15 | min = arr[i]; 16 | min_index = i; 17 | for (j = i; j < n; j++) 18 | { 19 | if (arr[j] < min) 20 | { 21 | min = arr[j]; 22 | min_index = j; 23 | } 24 | } 25 | tmp = arr[i]; 26 | arr[i] = min; 27 | arr[min_index] = tmp; 28 | } 29 | 30 | for (i = 0; i < n; i++) 31 | printf("%d ", arr[i]); 32 | 33 | free(arr); 34 | } -------------------------------------------------------------------------------- /Assignments/Week 1/김현영/김현영_1주차_보고서.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Assignments/Week 1/김현영/김현영_1주차_보고서.pdf -------------------------------------------------------------------------------- /Assignments/Week 1/박상민/BubbleSort_박상민.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int *arr, n, i, j, tmp = 0; 4 | scanf("%d", &n); 5 | arr = (int*)malloc(sizeof(int)*n); 6 | for (i = 0; i < n; i++) { 7 | scanf("%d", &arr[i]); 8 | } 9 | 10 | for (i = 0; i < n - 1; i++) { 11 | for (j = 0; j < n - i - 1; j++) { 12 | if (arr[j] > arr[j + 1]) { 13 | tmp = arr[j]; 14 | arr[j] = arr[j + 1]; 15 | arr[j + 1] = tmp; 16 | } 17 | } 18 | } 19 | for (i = 0; i < n; i++) { 20 | printf(" %d", arr[i]); 21 | } 22 | } -------------------------------------------------------------------------------- /Assignments/Week 1/박상민/InsertionSort_박상민.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int *arr, n, i, j, tmp = 0; 4 | int key = 0; 5 | scanf("%d", &n); 6 | arr = (int*)malloc(sizeof(int)*n); 7 | for (i = 0; i < n; i++) { 8 | scanf("%d", &arr[i]); 9 | } 10 | for (i = 1; i < n; i++) { 11 | key = arr[i]; 12 | for (j = i - 1; j >= 0; j--) { 13 | if (arr[j] >= key) { 14 | tmp = arr[j]; 15 | arr[j] = arr[j + 1]; 16 | arr[j + 1] = tmp; 17 | } 18 | else { 19 | arr[j + 1] = key; 20 | } 21 | } 22 | } 23 | for (i = 0; i < n; i++) { 24 | printf(" %d", arr[i]); 25 | } 26 | } -------------------------------------------------------------------------------- /Assignments/Week 1/박상민/MergeSort_박상민.c: -------------------------------------------------------------------------------- 1 | #include 2 | void combine(int *arr, int left, int right) { 3 | int mid = (right + left) / 2 + 1; 4 | int i = 0, *tmp, n = right - left + 1; 5 | int l = left, r = right, m = mid; 6 | tmp = (int*)malloc(sizeof(int)*n); 7 | while (l < mid && m <= right) { 8 | if (arr[l] > arr[m])tmp[i++] = arr[m++]; 9 | else tmp[i++] = arr[l++]; 10 | } 11 | while (l < mid) tmp[i++] = arr[l++]; 12 | while (m <= right) tmp[i++] = arr[m++]; 13 | for (i = 0; i < n; i++) arr[left++] = tmp[i]; 14 | free(tmp); 15 | } 16 | void merge(int *arr, int left, int right) { 17 | int mid = (right + left) / 2; 18 | if (right - left < 1)return; 19 | merge(arr, left, mid); 20 | merge(arr, mid + 1, right); 21 | combine(arr, left, right); 22 | } 23 | int main() { 24 | int *arr, i, n; 25 | scanf("%d", &n); 26 | arr = (int*)malloc(sizeof(int)*n); 27 | for (i = 0; i < n; i++) { 28 | scanf("%d", &arr[i]); 29 | } 30 | merge(arr, 0, n - 1); 31 | for (i = 0; i < n; i++) { 32 | printf(" %d", arr[i]); 33 | } 34 | free(arr); 35 | } -------------------------------------------------------------------------------- /Assignments/Week 1/박상민/QuickSort_박상민.c: -------------------------------------------------------------------------------- 1 | #include 2 | void swap(int *arr, int i, int j) { 3 | int tmp; 4 | tmp = arr[i]; 5 | arr[i] = arr[j]; 6 | arr[j] = tmp; 7 | } 8 | void print_arr(int *arr, int n) { 9 | int i = 0; 10 | for (i = 0; i= left && arr[high] > pivot) high--; 21 | if (low <= high)swap(arr, low, high); 22 | } 23 | swap(arr, low, high); 24 | return high; 25 | } 26 | void quick(int *arr, int left, int right) { 27 | int n = right - left + 1; 28 | if (right > left) { 29 | int a = quick_sort(arr, left, right); 30 | quick(arr, left, a - 1); 31 | quick(arr, a + 1, right); 32 | } 33 | } 34 | int main() { 35 | int *arr, n, i; 36 | scanf("%d", &n); arr = (int*)malloc(sizeof(int)*n); 37 | for (i = 0; i < n; i++) scanf("%d", &arr[i]); 38 | quick(arr, 0, n - 1); 39 | for (i = 0; i < n; i++) printf(" %d", arr[i]); 40 | free(arr); 41 | } -------------------------------------------------------------------------------- /Assignments/Week 1/박상민/SelectionSort_박상민.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int *arr, n, i, j, tmp = 0; 4 | int min = 0; 5 | scanf("%d", &n); 6 | arr = (int*)malloc(sizeof(int)*n); 7 | for (i = 0; i < n; i++) { 8 | scanf("%d", &arr[i]); 9 | } 10 | for (i = 0; i < n - 1; i++) { 11 | min = arr[i]; 12 | for (j = i; j < n; j++) { 13 | if (arr[min] > arr[j]) { 14 | min = j; 15 | } 16 | } 17 | printf("%d\n", arr[min]); 18 | tmp = arr[min]; 19 | arr[min] = arr[i]; 20 | arr[i] = tmp; 21 | } 22 | for (i = 0; i < n; i++) { 23 | printf(" %d", arr[i]); 24 | } 25 | } -------------------------------------------------------------------------------- /Assignments/Week 1/박상민/박상민_1주차_보고서.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Assignments/Week 1/박상민/박상민_1주차_보고서.pdf -------------------------------------------------------------------------------- /Assignments/Week 1/이기은/BubbleSort_이기은.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by KieunLEE on 2019-07-12. 3 | // 4 | #include 5 | #include 6 | 7 | int* bubbleSort(int* arr, int N){ 8 | int tmp; 9 | 10 | for(int i = 0 ; i < N - 1 ; i++){ 11 | for(int j = 0 ; j < N - i - 1 ; j++){ 12 | if(arr[j] > arr[j+1]){ 13 | tmp = arr[j]; 14 | arr[j] = arr[j+1]; 15 | arr[j+1] = tmp; 16 | } 17 | } 18 | } 19 | 20 | return arr; 21 | } 22 | 23 | int main() 24 | { 25 | int *arr; 26 | int N; 27 | 28 | scanf("%d", &N); 29 | 30 | arr = (int*)malloc(sizeof(int)*N); 31 | 32 | for(int i = 0 ; i < N ; i++){ 33 | scanf("%d", &arr[i]); 34 | } 35 | 36 | bubbleSort(arr, N); 37 | 38 | for(int i = 0 ; i < N ; i++){ 39 | printf("%d\n", arr[i]); 40 | } 41 | 42 | free(arr); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Assignments/Week 1/이기은/InsertionSort_이기은.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by KieunLEE on 2019-07-12. 3 | // 4 | #include 5 | #include 6 | 7 | int* insertSort(int* arr, int N){ 8 | int key = 0, j; 9 | 10 | for(int i = 1 ; i < N; i++){ 11 | key = arr[i]; 12 | for(j = i - 1 ; j >= 0 && key < arr[j] ; j--){ 13 | arr[j+1] = arr[j]; 14 | } 15 | arr[j+1] = key; 16 | } 17 | 18 | return arr; 19 | } 20 | 21 | int main() 22 | { 23 | int *arr; 24 | int N; 25 | 26 | scanf("%d", &N); 27 | 28 | arr = (int*)malloc(sizeof(int)*N); 29 | 30 | for(int i =0 ;i 5 | #include 6 | 7 | void merge(int* arr, int left, int right){ 8 | int* tmp; 9 | int mid = (left+right)/2 + 1; 10 | int len = (right - left + 1); 11 | int i = 0, l = left, m = mid ; 12 | 13 | tmp = (int*)malloc(sizeof(int)*len); 14 | 15 | while(l 5 | #include 6 | 7 | 8 | int sort(int* arr, int left, int right){ 9 | int pivot, tmp, length; 10 | int leftStart = left; 11 | int rightStart = right; 12 | 13 | pivot = arr[left]; 14 | length = right-left+1; 15 | 16 | while(leftStartpivot){ 21 | rightStart--; 22 | } 23 | tmp = arr[leftStart]; 24 | arr[leftStart] = arr[rightStart]; 25 | arr[rightStart] = tmp; 26 | } 27 | arr[leftStart] = pivot; 28 | 29 | return leftStart; 30 | } 31 | 32 | void quickSort(int *arr, int left, int right){ 33 | if(left 5 | #include 6 | 7 | int* selectSort(int* arr, int N){ 8 | int min=0, tmp =0, index =0; 9 | 10 | for(int i = 0 ; iarr[j]) { 15 | min = arr[j]; 16 | index = j; 17 | } 18 | } 19 | tmp=arr[i]; 20 | arr[i] = min; 21 | arr[index] = tmp; 22 | } 23 | 24 | return arr; 25 | } 26 | 27 | int main() 28 | { 29 | int *arr; 30 | int N; 31 | 32 | scanf("%d", &N); 33 | 34 | arr = (int*)malloc(sizeof(int)*N); 35 | 36 | for(int i =0 ; i 3 | #include 4 | #include 5 | #include 6 | 7 | int main() { 8 | int N, *arr = NULL; 9 | int i, j, tmp; 10 | 11 | scanf("%d", &N); 12 | arr = (int *)malloc(N * sizeof(int)); 13 | if (arr == NULL) return -1; 14 | for (i = 0;i < N;i++) 15 | scanf("%d", &arr[i]); 16 | 17 | for (i = 0;i < N;i++) { 18 | for (j = 0;j < N - i - 1;j++) 19 | if (arr[j] > arr[j + 1]) { 20 | tmp = arr[j]; 21 | arr[j] = arr[j + 1]; 22 | arr[j + 1] = tmp; 23 | } 24 | } 25 | for (i = 0;i < N;i++) 26 | printf("%d\n", arr[i]); 27 | 28 | free(arr); 29 | } -------------------------------------------------------------------------------- /Assignments/Week 1/이다예/InsertionSort_이다예.c: -------------------------------------------------------------------------------- 1 | #pragma warning(disable:4996) 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() { 8 | int N, *arr = NULL; 9 | int i, j, key, tmp; 10 | 11 | scanf("%d", &N); 12 | arr = (int *)malloc(N * sizeof(int)); 13 | if (arr == NULL) return -1; 14 | for (i = 0;i < N;i++) 15 | scanf("%d", &arr[i]); 16 | 17 | for (i = 1;i < N;i++) { 18 | key = arr[i]; 19 | for (j = i;j > 0;j--) { 20 | if (key < arr[j - 1]) 21 | arr[j] = arr[j - 1]; 22 | else 23 | break; 24 | } 25 | arr[j] = key; 26 | } 27 | for (i = 0;i < N;i++) 28 | printf("%d\n", arr[i]); 29 | 30 | free(arr); 31 | } -------------------------------------------------------------------------------- /Assignments/Week 1/이다예/SelectionSort_이다예.c: -------------------------------------------------------------------------------- 1 | #pragma warning(disable:4996) 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() { 8 | int N, *arr = NULL; 9 | int i, j, min, min_index, tmp; 10 | 11 | scanf("%d", &N); 12 | arr = (int *)malloc(N * sizeof(int)); 13 | if (arr == NULL) return -1; 14 | for (i = 0;i < N;i++) 15 | scanf("%d", &arr[i]); 16 | 17 | for (i = 0;i < N - 1;i++) { 18 | min = arr[i]; 19 | for (j = i;j < N;j++) { 20 | if (arr[j] <= min) { 21 | min = arr[j]; 22 | min_index = j; 23 | } 24 | } 25 | tmp = arr[i]; 26 | arr[i] = min; 27 | arr[min_index] = tmp; 28 | } 29 | for (i = 0;i < N;i++) 30 | printf("%d\n", arr[i]); 31 | 32 | free(arr); 33 | } -------------------------------------------------------------------------------- /Assignments/Week 1/이다예/이다예_1주차_보고서.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Assignments/Week 1/이다예/이다예_1주차_보고서.pdf -------------------------------------------------------------------------------- /Assignments/Week 1/이승훈/BubbleSort_이승훈.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void swap(int *a, int *b); 7 | 8 | 9 | int main() { 10 | int i, j, N, *p, *q, *r; 11 | 12 | LARGE_INTEGER frequency; 13 | LARGE_INTEGER t1, t2; 14 | double elapsed_time; 15 | 16 | QueryPerformanceFrequency(&frequency); 17 | 18 | scanf("%d", &N); 19 | p = (int *)malloc(sizeof(int)*N); 20 | q = (int *)malloc(sizeof(int)*N); 21 | r = (int *)malloc(sizeof(int)*N); 22 | for (i = 0; i < N; i++) p[i] = rand() % 1000; 23 | for (i = 0; i < N; i++) q[i] = i; 24 | for (i = 0; i < N; i++) r[i] = N - i; 25 | 26 | srand(time(NULL)); 27 | QueryPerformanceCounter(&t1); 28 | for (i = 0; i < N; i++) { 29 | for (j = 0; j < N - i - 1; j++) { 30 | if (p[j] > p[j + 1]) swap(&p[j], &p[j + 1]); 31 | } 32 | } 33 | QueryPerformanceCounter(&t2); 34 | 35 | elapsed_time = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart; //get elapsed time in millisec 36 | printf("Time by selection sort: %lf ms\n", elapsed_time); 37 | 38 | QueryPerformanceCounter(&t1); 39 | for (i = 0; i < N; i++) { 40 | for (j = 0; j < N - i - 1; j++) { 41 | if (q[j] > q[j + 1]) swap(&q[j], &q[j + 1]); 42 | } 43 | } 44 | QueryPerformanceCounter(&t2); 45 | 46 | elapsed_time = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart; //get elapsed time in millisec 47 | printf("Time by selection sort: %lf ms\n", elapsed_time); 48 | 49 | QueryPerformanceCounter(&t1); 50 | for (i = 0; i < N; i++) { 51 | for (j = 0; j < N - i - 1; j++) { 52 | if (r[j] > r[j + 1]) swap(&r[j], &r[j + 1]); 53 | } 54 | } 55 | QueryPerformanceCounter(&t2); 56 | 57 | elapsed_time = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart; //get elapsed time in millisec 58 | printf("Time by selection sort: %lf ms\n", elapsed_time); 59 | 60 | 61 | 62 | 63 | 64 | return 0; 65 | } 66 | 67 | void swap(int *a, int *b) { 68 | int temp; 69 | temp = *a; 70 | *a = *b; 71 | *b = temp; 72 | } -------------------------------------------------------------------------------- /Assignments/Week 1/이승훈/InsertionSort_이승훈.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | void Insert(int *a, int N); 6 | 7 | int main() { 8 | int i, j, N, *p, index, *q, *r; 9 | 10 | LARGE_INTEGER frequency; 11 | LARGE_INTEGER t1, t2; 12 | double elapsed_time; 13 | 14 | QueryPerformanceFrequency(&frequency); 15 | 16 | scanf("%d", &N); 17 | p = (int *)malloc(sizeof(int)*N); 18 | q = (int *)malloc(sizeof(int)*N); 19 | r = (int *)malloc(sizeof(int)*N); 20 | for (i = 0; i < N; i++) p[i] = rand() % 1000; 21 | for (i = 0; i < N; i++) q[i] = i; 22 | for (i = 0; i < N; i++) r[i] = N - i; 23 | srand(time(NULL)); 24 | 25 | QueryPerformanceCounter(&t1); 26 | Insert(p, N); 27 | QueryPerformanceCounter(&t2); 28 | 29 | elapsed_time = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart; //get elapsed time in millisec 30 | printf("Time by selection sort: %lf ms\n", elapsed_time); 31 | 32 | QueryPerformanceCounter(&t1); 33 | Insert(q, N); 34 | QueryPerformanceCounter(&t2); 35 | 36 | elapsed_time = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart; //get elapsed time in millisec 37 | printf("Time by selection sort: %lf ms\n", elapsed_time); 38 | 39 | 40 | QueryPerformanceCounter(&t1); 41 | Insert(r, N); 42 | QueryPerformanceCounter(&t2); 43 | 44 | elapsed_time = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart; //get elapsed time in millisec 45 | printf("Time by selection sort: %lf ms\n", elapsed_time); 46 | 47 | 48 | 49 | return 0; 50 | } 51 | 52 | void Insert(int *p, int N) { 53 | int i, j, index; 54 | for (i = 1; i < N; i++) { 55 | index = p[i]; 56 | for (j = i - 1; j >= 0; j--) { 57 | if (p[j] > index) { 58 | p[j + 1] = p[j]; 59 | continue; 60 | } 61 | break; 62 | } 63 | p[j + 1] = index; 64 | } 65 | 66 | 67 | } 68 | -------------------------------------------------------------------------------- /Assignments/Week 1/이승훈/MergeSort_이승훈.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void MergeSort(int *st, int p, int r); 7 | void Merge(int *st, int p, int q, int r); 8 | 9 | int main() { 10 | int i, N, *p, *q, *r; 11 | 12 | LARGE_INTEGER frequency; 13 | LARGE_INTEGER t1, t2; 14 | double elapsed_time; 15 | 16 | QueryPerformanceFrequency(&frequency); 17 | 18 | scanf("%d", &N); 19 | p = (int *)malloc(sizeof(int)*N); 20 | q = (int *)malloc(sizeof(int)*N); 21 | r = (int *)malloc(sizeof(int)*N); 22 | srand(time(NULL)); 23 | for (i = 0; i < N; i++) p[i] = rand() % 1000; 24 | for (i = 0; i < N; i++) q[i] = i; 25 | for (i = 0; i < N; i++) r[i] = N - i; 26 | 27 | QueryPerformanceCounter(&t1); 28 | MergeSort(p, 0, N - 1); 29 | QueryPerformanceCounter(&t2); 30 | 31 | elapsed_time = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart; //get elapsed time in millisec 32 | printf("Time by selection sort: %lf ms\n", elapsed_time); 33 | 34 | 35 | QueryPerformanceCounter(&t1); 36 | MergeSort(q, 0, N - 1); 37 | QueryPerformanceCounter(&t2); 38 | 39 | elapsed_time = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart; //get elapsed time in millisec 40 | printf("Time by selection sort: %lf ms\n", elapsed_time); 41 | 42 | QueryPerformanceCounter(&t1); 43 | MergeSort(r, 0, N - 1); 44 | QueryPerformanceCounter(&t2); 45 | 46 | elapsed_time = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart; //get elapsed time in millisec 47 | printf("Time by selection sort: %lf ms\n", elapsed_time); 48 | 49 | return 0; 50 | } 51 | 52 | void MergeSort(int *st, int p, int r) { 53 | int q; 54 | if (p < r) { 55 | q = (p + r) / 2; 56 | MergeSort(st, p, q); 57 | MergeSort(st, q + 1, r); 58 | Merge(st, p, q, r); 59 | } 60 | } 61 | void Merge(int *st, int p, int q, int r) { 62 | int i = p, j = q + 1, k = p; 63 | int *tmp = (int *)malloc(sizeof(int)*r+1); 64 | while (i <= q && j <= r) { 65 | if (st[i] <= st[j]) tmp[k++] = st[i++]; 66 | else tmp[k++] = st[j++]; 67 | } 68 | while (i <= q) tmp[k++] = st[i++]; 69 | while (j <= r) tmp[k++] = st[j++]; 70 | for (int a = p; a <= r; a++) st[a] = tmp[a]; 71 | free(tmp); 72 | } 73 | -------------------------------------------------------------------------------- /Assignments/Week 1/이승훈/QuickSort_이승훈.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void Swap(int *st, int a, int b); 7 | int Pivot_(int *st, int a, int b); 8 | void QuickSort(int *st, int a, int b); 9 | 10 | int main() { 11 | int N, i; 12 | int *p, *q, *r; 13 | 14 | LARGE_INTEGER frequency; 15 | LARGE_INTEGER t1, t2; 16 | double elapsed_time; 17 | 18 | QueryPerformanceFrequency(&frequency); 19 | 20 | scanf("%d", &N); 21 | p = (int *)malloc(sizeof(int)*N); 22 | q = (int *)malloc(sizeof(int)*N); 23 | r = (int *)malloc(sizeof(int)*N); 24 | 25 | srand(time(NULL)); 26 | 27 | for (i = 0; i < N; i++) p[i] = rand() % 1000; 28 | for (i = 0; i < N; i++) q[i] = i; 29 | for (i = 0; i < N; i++) r[i] = N - i; 30 | //p[i]=rand()%1000; 31 | 32 | QueryPerformanceCounter(&t1); 33 | QuickSort(p, 0, N - 1); 34 | QueryPerformanceCounter(&t2); 35 | 36 | elapsed_time = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart; //get elapsed time in millisec 37 | printf("Time by selection sort: %lf ms\n", elapsed_time); 38 | 39 | QueryPerformanceCounter(&t1); 40 | QuickSort(q, 0, N - 1); 41 | QueryPerformanceCounter(&t2); 42 | 43 | elapsed_time = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart; //get elapsed time in millisec 44 | printf("Time by selection sort: %lf ms\n", elapsed_time); 45 | 46 | QueryPerformanceCounter(&t1); 47 | QuickSort(r, 0, N - 1); 48 | QueryPerformanceCounter(&t2); 49 | 50 | elapsed_time = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart; //get elapsed time in millisec 51 | printf("Time by selection sort: %lf ms\n", elapsed_time); 52 | 53 | 54 | 55 | 56 | 57 | 58 | return 0; 59 | } 60 | 61 | void Swap(int *st, int a, int b){ 62 | int temp = st[a]; 63 | st[a] = st[b]; 64 | st[b] = temp; 65 | } 66 | int Pivot_(int *st, int a, int b) { 67 | int pivot = st[a], low = a + 1, high = b; 68 | 69 | while (low <= high) { 70 | while (pivot >= st[low] && low <= b){ 71 | low++; 72 | } 73 | while (pivot <= st[high] && high >= (a + 1)) { 74 | high--; 75 | } 76 | if (low <= high) { 77 | Swap(st, low, high); 78 | } 79 | } 80 | Swap(st, a, high); 81 | return high; 82 | } 83 | 84 | void QuickSort(int *st, int a, int b) { 85 | int pivot; 86 | if (a <= b) { 87 | pivot = Pivot_(st, a, b); 88 | QuickSort(st, a, pivot - 1); 89 | QuickSort(st, pivot + 1, b); 90 | } 91 | } -------------------------------------------------------------------------------- /Assignments/Week 1/이승훈/SelectionSort_이승훈.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void Swap(int *a, int *b); 7 | 8 | int main() { 9 | int i, j, N, *p, index = 0, min, *q, *r; 10 | 11 | LARGE_INTEGER frequency; 12 | LARGE_INTEGER t1, t2; 13 | double elapsed_time; 14 | 15 | QueryPerformanceFrequency(&frequency); 16 | 17 | scanf("%d", &N); 18 | p = (int *)malloc(sizeof(int)*N); 19 | q = (int *)malloc(sizeof(int)*N); 20 | r = (int *)malloc(sizeof(int)*N); 21 | for (i = 0; i < N; i++) p[i] = rand() % 1000; 22 | for (i = 0; i < N; i++) q[i] = i; 23 | for (i = 0; i < N; i++) r[i] = N - i; 24 | srand(time(NULL)); 25 | 26 | QueryPerformanceCounter(&t1); 27 | for (i = 0; i < N; i++) { 28 | min = index; 29 | for (j = index; j < N; j++) if (p[j] < p[min]) min = j; 30 | Swap(&p[min], &p[index]); 31 | index++; 32 | } 33 | QueryPerformanceCounter(&t2); 34 | 35 | elapsed_time = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart; //get elapsed time in millisec 36 | printf("Time by selection sort: %lf ms\n", elapsed_time); 37 | 38 | 39 | QueryPerformanceCounter(&t1); 40 | for (i = 0; i < N; i++) { 41 | min = index; 42 | for (j = index; j < N; j++) if (q[j] < q[min]) min = j; 43 | Swap(&q[min], &q[index]); 44 | index++; 45 | } 46 | QueryPerformanceCounter(&t2); 47 | 48 | elapsed_time = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart; //get elapsed time in millisec 49 | printf("Time by selection sort: %lf ms\n", elapsed_time); 50 | 51 | 52 | QueryPerformanceCounter(&t1); 53 | for (i = 0; i < N; i++) { 54 | min = index; 55 | for (j = index; j < N; j++) if (r[j] < r[min]) min = j; 56 | Swap(&r[min], &r[index]); 57 | index++; 58 | } 59 | QueryPerformanceCounter(&t2); 60 | 61 | elapsed_time = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart; //get elapsed time in millisec 62 | printf("Time by selection sort: %lf ms\n", elapsed_time); 63 | 64 | return 0; 65 | } 66 | 67 | void Swap(int *a, int *b) { 68 | int temp; 69 | temp = *a; 70 | *a = *b; 71 | *b = temp; 72 | } 73 | -------------------------------------------------------------------------------- /Assignments/Week 1/이승훈/이승훈_1주차_보고서.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Assignments/Week 1/이승훈/이승훈_1주차_보고서.pdf -------------------------------------------------------------------------------- /Assignments/Week 1/이우식/BubbleSort_이우식.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Assignments/Week 1/이우식/BubbleSort_이우식.c -------------------------------------------------------------------------------- /Assignments/Week 1/이우식/InsertionSort_이우식.c: -------------------------------------------------------------------------------- 1 | #pragma warning (disable :4996) 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | typedef struct NODE { 8 | struct NODE *next; 9 | struct NODE *prev; 10 | int data; 11 | }NODE; 12 | NODE *getNode() { 13 | NODE *p; 14 | p = (NODE*)malloc(sizeof(NODE)); 15 | p->next = NULL; 16 | p->prev = NULL; 17 | p->data = NULL; 18 | return p; 19 | } 20 | void add(NODE *h, int data) { 21 | NODE *new = h; 22 | NODE *tmp; 23 | if (new == NULL) { 24 | new = getNode(); 25 | new->data = data; 26 | } 27 | else { 28 | tmp = getNode(); 29 | tmp->prev = new; 30 | new->next = tmp; 31 | new = new->next; 32 | new->data = data; 33 | } 34 | } 35 | void insert(NODE *h) { 36 | NODE *set = h; 37 | NODE *p = h; 38 | NODE *tmp = h; 39 | int i, j; 40 | int flag = 0; 41 | while (p->prev != NULL) { 42 | if (p->prev->data <= tmp->data) break; 43 | p = p->prev; 44 | flag++; 45 | } 46 | if (h->next == NULL) { 47 | if (flag != 0) { 48 | tmp->prev->next = NULL; 49 | tmp->next = p;//�̵��� 50 | p->prev->next = tmp; 51 | tmp->prev = p->prev; 52 | p->prev = tmp; 53 | } 54 | 55 | } 56 | else { 57 | if (flag != 0) { 58 | tmp->prev->next = tmp->next; 59 | tmp->next->prev = tmp->prev; 60 | tmp->next = p;//�̵��� 61 | p->prev->next = tmp; 62 | tmp->prev = p->prev; 63 | p->prev = tmp; 64 | } 65 | 66 | insert(set->next); 67 | } 68 | } 69 | void print(NODE *h) { 70 | NODE *show = h; 71 | for (; show != NULL; show = show->next) { 72 | printf(" %d", show->data); 73 | } 74 | } 75 | int main() { 76 | NODE *arr2; 77 | int N; 78 | int i; 79 | NODE *p; 80 | LARGE_INTEGER frequency; 81 | LARGE_INTEGER t1, t2; 82 | double elapsed_time; 83 | QueryPerformanceFrequency(&frequency); 84 | srand(time(NULL)); 85 | scanf("%d", &N); 86 | arr2 = (NODE *)malloc(sizeof(NODE)); 87 | p = arr2; 88 | 89 | for (i = 0; i < N; i++) { 90 | add(p, rand()); 91 | p = p->next; 92 | } 93 | p->prev = NULL; 94 | //insertion_sort(arr1, n); // 95 | //reverse_array(arr1, 0, n - 1); // 96 | 97 | QueryPerformanceCounter(&t1); 98 | insert(arr2->next); 99 | QueryPerformanceCounter(&t2); 100 | 101 | elapsed_time = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart; //get elapsed time in millisec 102 | 103 | printf("Time by insertion sort: %lf ms\n", elapsed_time); 104 | return 0; 105 | } -------------------------------------------------------------------------------- /Assignments/Week 1/이우식/QuickSort_이우식.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Assignments/Week 1/이우식/QuickSort_이우식.c -------------------------------------------------------------------------------- /Assignments/Week 1/이우식/SelectionSort_이우식.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Assignments/Week 1/이우식/SelectionSort_이우식.c -------------------------------------------------------------------------------- /Assignments/Week 1/이우식/이우식_1주차_보고서.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Assignments/Week 1/이우식/이우식_1주차_보고서.pdf -------------------------------------------------------------------------------- /Assignments/Week 1/전선영/BubbleSort_전선영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct Node 4 | { 5 | struct Node *next; 6 | int elem; 7 | }Node; 8 | Node *get_node(int a) 9 | { 10 | Node *node; 11 | 12 | node=(Node*)malloc(sizeof(Node)); 13 | node->next=NULL; 14 | node->elem=a; 15 | 16 | return node; 17 | } 18 | Node *make_list(int n) 19 | { 20 | int i,a; 21 | Node *node=get_node(0),*head=node; 22 | 23 | for(i=0;inext=get_node(a); 27 | node=node->next; 28 | } 29 | node = head->next; 30 | free(head); 31 | return node; 32 | } 33 | Node *bubble_sort(Node *node,int n) 34 | { 35 | int i, j,tmp; 36 | Node *nodei=node,*nodej=node; 37 | 38 | for (i = 0; i < n; i++) 39 | { 40 | for (nodej=nodei,j=i; j < n ; j++) 41 | { 42 | if (nodei->elem > nodej->elem) 43 | { 44 | tmp = nodei->elem; 45 | nodei->elem = nodej->elem; 46 | nodej->elem = tmp; 47 | } 48 | nodej = nodej->next; 49 | } 50 | nodei = nodei->next; 51 | } 52 | return node; 53 | } 54 | void print_node(Node *node) 55 | { 56 | while(node) 57 | { 58 | printf("%d\n",node->elem); 59 | node=node->next; 60 | } 61 | } 62 | void free_node(Node *node) 63 | { 64 | Node *a; 65 | while (node) 66 | { 67 | a = node->next; 68 | free(node); 69 | node = a; 70 | } 71 | } 72 | int main() 73 | { 74 | int n; 75 | Node *node; 76 | 77 | scanf("%d",&n); 78 | 79 | node=make_list(n); 80 | node=bubble_sort(node,n); 81 | 82 | print_node(node); 83 | 84 | free_node(node); 85 | } 86 | -------------------------------------------------------------------------------- /Assignments/Week 1/전선영/InsertionSort_전선영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | LARGE_INTEGER Frequency; 6 | LARGE_INTEGER BeginTime; 7 | LARGE_INTEGER Endtime; 8 | int *make_list(int n) 9 | { 10 | int a, i, *arr; 11 | arr = (int*)malloc(sizeof(int)*n); 12 | 13 | srand(1); 14 | for (i = 0; i < n; i++) 15 | { 16 | arr[i]= rand() % 2000 - 1000; 17 | } 18 | return arr; 19 | } 20 | void swap(int *a, int *b) 21 | { 22 | int tmp; 23 | tmp = *a; 24 | *a = *b; 25 | *b = tmp; 26 | } 27 | void insert_sort(int *arr,int n) 28 | { 29 | int i, j; 30 | 31 | for (i = 0; i < n; i++) 32 | { 33 | j = i; 34 | while (arr[j - 1] > arr[j] && j >= 0) 35 | { 36 | swap(arr + j - 1, arr + j); 37 | j--; 38 | } 39 | } 40 | } 41 | void Print(int *arr,int n) 42 | { 43 | int i; 44 | for (i = 0; i < n; i++) 45 | printf("%d\n", arr[i]); 46 | } 47 | int main() 48 | { 49 | int n, *arr; 50 | QueryPerformanceFrequency(&Frequency); 51 | 52 | scanf("%d", &n); 53 | arr = make_list(n); 54 | 55 | QueryPerformanceCounter(&BeginTime); 56 | 57 | insert_sort(arr,n); 58 | 59 | QueryPerformanceCounter(&Endtime); 60 | 61 | __int64 elapsed = Endtime.QuadPart - BeginTime.QuadPart; 62 | double duringtime = (double)elapsed / (double)Frequency.QuadPart; 63 | 64 | printf("%f\n", duringtime); 65 | 66 | free(arr); 67 | 68 | } -------------------------------------------------------------------------------- /Assignments/Week 1/전선영/MergeSort(TimeCheck)_전선영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | LARGE_INTEGER Frequency; 6 | LARGE_INTEGER BeginTime; 7 | LARGE_INTEGER Endtime; 8 | typedef struct Node 9 | { 10 | struct Node *next; 11 | int elem; 12 | }Node; 13 | Node *get_node(int a) 14 | { 15 | Node *node; 16 | 17 | node = (Node*)malloc(sizeof(Node)); 18 | node->next = NULL; 19 | node->elem = a; 20 | 21 | return node; 22 | } 23 | Node *make_list(int n) 24 | { 25 | int i, a; 26 | Node *node = get_node(0), *head = node; 27 | 28 | srand(1); 29 | for (i = 0; inext = get_node(a); 33 | node = node->next; 34 | } 35 | node = head->next; 36 | free(head); 37 | return node; 38 | } 39 | Node *merge_node(Node *node1, Node*node2) 40 | { 41 | Node *node = get_node(0), *pnode = node; 42 | while (node1 != NULL || node2 != NULL) 43 | { 44 | if (node1 == NULL) 45 | { 46 | node->next = node2; 47 | node2 = node2->next; 48 | } 49 | else if (node2 == NULL) 50 | { 51 | node->next = node1; 52 | node1 = node1->next; 53 | } 54 | else if (node1->elemelem) 55 | { 56 | node->next = node1; 57 | node1 = node1->next; 58 | } 59 | else if(node1->elem>node2->elem) 60 | { 61 | node->next = node2; 62 | node2 = node2->next; 63 | } 64 | else 65 | { 66 | node->next = node1; 67 | node2 = node2->next; 68 | node1 = node1->next; 69 | } 70 | node = node->next; 71 | } 72 | node = pnode->next; 73 | free(pnode); 74 | return node; 75 | } 76 | Node *merge_sort(Node *node, int n) 77 | { 78 | Node *node_r, *node_l = node; 79 | int i; 80 | if (node->next == NULL) 81 | return node; 82 | 83 | for (i = 1; i < n / 2; i++) 84 | node = node->next; 85 | 86 | node_r = node->next; 87 | node->next = NULL; 88 | 89 | if (n % 2 == 0) 90 | return merge_node(merge_sort(node_l, n / 2), merge_sort(node_r, n / 2)); 91 | else 92 | return merge_node(merge_sort(node_l, n / 2), merge_sort(node_r, n / 2 + 1)); 93 | } 94 | void print_node(Node *node) 95 | { 96 | while (node) 97 | { 98 | printf("%d\n", node->elem); 99 | node = node->next; 100 | } 101 | } 102 | void free_node(Node *node) 103 | { 104 | Node *a; 105 | while (node) 106 | { 107 | a = node->next; 108 | free(node); 109 | node = a; 110 | } 111 | } 112 | int main() 113 | { 114 | int n; 115 | Node *node; 116 | QueryPerformanceFrequency(&Frequency); 117 | 118 | scanf("%d", &n); 119 | node = make_list(n); 120 | 121 | QueryPerformanceCounter(&BeginTime); 122 | 123 | node = merge_sort(node, n); 124 | 125 | QueryPerformanceCounter(&Endtime); 126 | 127 | __int64 elapsed = Endtime.QuadPart - BeginTime.QuadPart; 128 | double duringtime = (double)elapsed / (double)Frequency.QuadPart; 129 | 130 | printf("%f", duringtime); 131 | 132 | free_node(node); 133 | } -------------------------------------------------------------------------------- /Assignments/Week 1/전선영/MergeSort_전선영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct Node 4 | { 5 | struct Node *next; 6 | int elem; 7 | }Node; 8 | Node *get_node(int a) 9 | { 10 | Node *node; 11 | 12 | node=(Node*)malloc(sizeof(Node)); 13 | node->next=NULL; 14 | node->elem=a; 15 | 16 | return node; 17 | } 18 | Node *make_list(int n) 19 | { 20 | int i,a; 21 | Node *node=get_node(0),*head=node; 22 | 23 | for(i=0;inext=get_node(a); 27 | node=node->next; 28 | } 29 | node = head->next; 30 | free(head); 31 | return node; 32 | } 33 | Node *merge_node(Node *node1, Node*node2) 34 | { 35 | Node *node = get_node(0), *pnode = node; 36 | while (node1 != NULL || node2 != NULL) 37 | { 38 | if (node1 == NULL) 39 | { 40 | node->next = node2; 41 | node2 = node2->next; 42 | } 43 | else if (node2 == NULL) 44 | { 45 | node->next = node1; 46 | node1 = node1->next; 47 | } 48 | else if (node1->elemelem) 49 | { 50 | node->next = node1; 51 | node1 = node1->next; 52 | } 53 | else if(node1->elem>node2->elem) 54 | { 55 | node->next = node2; 56 | node2 = node2->next; 57 | } 58 | else 59 | { 60 | node->next = node1; 61 | node2 = node2->next; 62 | node1 = node1->next; 63 | } 64 | node = node->next; 65 | } 66 | node = pnode->next; 67 | free(pnode); 68 | return node; 69 | } 70 | Node *merge_sort(Node *node, int n) 71 | { 72 | Node *node_r, *node_l = node; 73 | int i; 74 | if (node->next == NULL) 75 | return node; 76 | 77 | for (i = 1; i < n / 2; i++) 78 | node = node->next; 79 | 80 | node_r = node->next; 81 | node->next = NULL; 82 | 83 | if (n % 2 == 0) 84 | return merge_node(merge_sort(node_l, n / 2), merge_sort(node_r, n / 2)); 85 | else 86 | return merge_node(merge_sort(node_l, n / 2), merge_sort(node_r, n / 2 + 1)); 87 | } 88 | void print_node(Node *node) 89 | { 90 | while(node) 91 | { 92 | printf("%d\n",node->elem); 93 | node=node->next; 94 | } 95 | } 96 | void free_node(Node *node) 97 | { 98 | if (node) 99 | { 100 | free_node(node->next); 101 | free(node); 102 | } 103 | } 104 | int main() 105 | { 106 | int n; 107 | Node *node; 108 | 109 | scanf("%d",&n); 110 | 111 | node=make_list(n); 112 | node=merge_sort(node,n); 113 | 114 | print_node(node); 115 | 116 | free_node(node); 117 | } 118 | -------------------------------------------------------------------------------- /Assignments/Week 1/전선영/QuickSort_전선영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct Node 4 | { 5 | struct Node *next,*end; 6 | int elem; 7 | }Node; 8 | Node *get_node(int a) 9 | { 10 | Node *node; 11 | 12 | node=(Node*)malloc(sizeof(Node)); 13 | node->next=NULL; 14 | node->end=NULL; 15 | node->elem=a; 16 | 17 | return node; 18 | } 19 | Node *make_list(int n) 20 | { 21 | int i,a; 22 | Node *node=get_node(0),*head=node; 23 | 24 | for(i=0;inext=get_node(a); 28 | node=node->next; 29 | } 30 | node = head->next; 31 | free(head); 32 | return node; 33 | } 34 | Node *quick_sort(Node *node) 35 | { 36 | Node *pivot = node, *left = get_node(0), *right = get_node(0); 37 | Node *left_head = left, *right_head = right; 38 | 39 | if (node->next == NULL) 40 | { 41 | node->end = node; 42 | return node; 43 | } 44 | else 45 | { 46 | node = node->next; 47 | while (node) 48 | { 49 | if (node->elem < pivot->elem) 50 | { 51 | left->next = node; 52 | left = left->next; 53 | left_head->next->end = left; 54 | } 55 | else 56 | { 57 | right->next = node; 58 | right = right->next; 59 | right_head->next->end = right; 60 | } 61 | node = node->next; 62 | } 63 | } 64 | 65 | left->next = NULL; 66 | right->next = NULL; 67 | pivot->next = NULL; 68 | 69 | left = left_head->next; 70 | right = right_head->next; 71 | free(left_head); 72 | free(right_head); 73 | 74 | if (left == NULL) 75 | { 76 | node = pivot; 77 | pivot->next = quick_sort(right); 78 | pivot->end = pivot->next->end; 79 | node->end = pivot->end; 80 | } 81 | else if (right == NULL) 82 | { 83 | node = quick_sort(left); 84 | node->end->next = pivot; 85 | node->end = pivot; 86 | } 87 | else 88 | { 89 | node = quick_sort(left); 90 | node->end->next = pivot; 91 | pivot->next = quick_sort(right); 92 | pivot->end = pivot->next->end; 93 | node->end = pivot->end; 94 | } 95 | return node; 96 | } 97 | void print_node(Node *node) 98 | { 99 | while(node) 100 | { 101 | printf("%d\n",node->elem); 102 | node=node->next; 103 | } 104 | } 105 | void free_node(Node *node) 106 | { 107 | if (node) 108 | { 109 | free_node(node->next); 110 | free(node); 111 | } 112 | } 113 | int main() 114 | { 115 | int n; 116 | Node *node; 117 | 118 | scanf("%d",&n); 119 | 120 | node=make_list(n); 121 | node=quick_sort(node,n); 122 | 123 | print_node(node); 124 | 125 | free_node(node); 126 | } 127 | -------------------------------------------------------------------------------- /Assignments/Week 1/전선영/SelectionSort_전선영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct Node 4 | { 5 | struct Node *next; 6 | int elem; 7 | }Node; 8 | Node *get_node(int a) 9 | { 10 | Node *node; 11 | 12 | node=(Node*)malloc(sizeof(Node)); 13 | node->next=NULL; 14 | node->elem=a; 15 | 16 | return node; 17 | } 18 | Node *make_list(int n) 19 | { 20 | int i,a; 21 | Node *node=get_node(0),*head=node; 22 | 23 | for(i=0;inext=get_node(a); 27 | node=node->next; 28 | } 29 | node = head->next; 30 | free(head); 31 | return node; 32 | } 33 | Node *select_min(Node *node) 34 | { 35 | Node *minn=node; 36 | while (node) 37 | { 38 | if (node->elem < minn->elem) 39 | minn = node; 40 | node = node->next; 41 | } 42 | return minn; 43 | } 44 | Node *selection_sort(Node *head) 45 | { 46 | Node *minn, *node=head; 47 | int tmp; 48 | while (head) 49 | { 50 | minn = select_min(head); 51 | if (minn->elem < head->elem) 52 | { 53 | tmp=head->elem; 54 | head->elem = minn->elem; 55 | minn->elem = tmp; 56 | } 57 | head = head->next; 58 | } 59 | return node; 60 | } 61 | void print_node(Node *node) 62 | { 63 | while(node) 64 | { 65 | printf("%d\n",node->elem); 66 | node=node->next; 67 | } 68 | } 69 | void free_node(Node *node) 70 | { 71 | Node *a; 72 | while (node) 73 | { 74 | a = node->next; 75 | free(node); 76 | node = a; 77 | } 78 | } 79 | int main() 80 | { 81 | int n; 82 | Node *node; 83 | 84 | scanf("%d",&n); 85 | 86 | node=make_list(n); 87 | node=selection_sort(node); 88 | 89 | print_node(node); 90 | 91 | free_node(node); 92 | } 93 | -------------------------------------------------------------------------------- /Assignments/Week 1/전선영/전선영_1주차_보고서.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Assignments/Week 1/전선영/전선영_1주차_보고서.pdf -------------------------------------------------------------------------------- /Assignments/Week 1/정지호/BubbleSort_정지호.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void swap(int* a, int* b); 4 | int main() { 5 | 6 | int n, i, j; 7 | int *arr, * par; 8 | 9 | scanf("%d", &n); 10 | 11 | arr = (int*)malloc(sizeof(int) * n); 12 | 13 | for (par = arr; par < arr + n; par++) 14 | scanf("%d", par); 15 | 16 | for (i = 0; i < n; i++) 17 | { 18 | for (j = 0; j < n - i - 1; j++) 19 | { 20 | if (*(arr + j) > *(arr + j + 1)) 21 | swap((arr + j), (arr + j + 1)); 22 | } 23 | } 24 | 25 | for (par = arr; par < arr + n; par++) 26 | printf("%d\n", *par); 27 | 28 | return 0; 29 | 30 | } 31 | void swap(int* a, int* b) 32 | { 33 | int tmp; 34 | tmp = *a; 35 | *a = *b; 36 | *b = tmp; 37 | } 38 | -------------------------------------------------------------------------------- /Assignments/Week 1/정지호/InsertSort_정지호.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int N, i, j, tmp; 6 | int arr[1000], * par; 7 | scanf("%d", &N); 8 | 9 | for (par = arr; par < arr + N; par++) 10 | scanf("%d", par); 11 | 12 | for (i = 1; i < N; i++) 13 | { 14 | tmp = arr[i]; 15 | for (j = i; j > 0; j--) 16 | { 17 | if (tmp > arr[j - 1]) break; 18 | arr[j] = arr[j - 1]; 19 | } 20 | arr[j] = tmp; 21 | } 22 | 23 | for (par = arr; par < arr + N; par++) 24 | printf("%d\n", *par); 25 | 26 | 27 | } -------------------------------------------------------------------------------- /Assignments/Week 1/정지호/MergeSort_정지호.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void Merge_Array(int *arr, int left, int right); 6 | void Divide_Array(int *arr, int left, int right); 7 | int main() 8 | { 9 | int N; 10 | int arr[1000], * par = arr; 11 | scanf("%d", &N); 12 | 13 | for (par = arr; par < arr + N; par++) 14 | scanf("%d", par); 15 | 16 | Divide_Array(arr, 0, N - 1); 17 | 18 | for (par = arr; par < arr + N; par++) 19 | printf("%d\n", *par); 20 | 21 | 22 | } 23 | 24 | void Merge_Array(int* arr, int left, int right) 25 | { 26 | int mid = (left + right) / 2 + 1; 27 | int center = mid, i; 28 | int L = left, R = right; 29 | int size = (right - left + 1); 30 | int* Arr, Num = 0; 31 | Arr = (int*)malloc(sizeof(int) * size); 32 | 33 | while (L < mid && center <= R) 34 | { 35 | if (arr[L] > arr[center]) Arr[Num++] = arr[center++]; 36 | else Arr[Num++] = arr[L++]; 37 | } 38 | while (L < mid) Arr[Num++] = arr[L++]; 39 | while (center <= right) Arr[Num++] = arr[center++]; 40 | for (i = 0; i < size; i++) arr[left++] = Arr[i]; 41 | free(Arr); 42 | } 43 | void Divide_Array(int* arr, int left, int right) 44 | { 45 | int mid = (left + right) / 2; 46 | 47 | if (left < right) 48 | { 49 | Divide_Array(arr, left, mid); 50 | Divide_Array(arr, mid + 1, right); 51 | Merge_Array(arr, left, right); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Assignments/Week 1/정지호/QuickSort_정지호.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void swap(int* a, int* b); 5 | void Divide_Array(int *arr,int left, int right); 6 | int main() 7 | { 8 | int N; 9 | int* arr, * par; 10 | 11 | scanf("%d", &N); 12 | 13 | arr = (int*)malloc(sizeof(int) * N); 14 | 15 | for (par = arr; par < arr + N; par++) 16 | scanf("%d", par); 17 | Divide_Array(arr, 0, N - 1); 18 | for (par = arr; par < arr + N; par++) 19 | printf("%d\n", *par); 20 | 21 | 22 | } 23 | void Divide_Array(int* arr, int left, int right) 24 | { 25 | int Pivot = arr[(left + right) / 2]; 26 | int L = left; 27 | int R = right; 28 | 29 | while (L <= R) 30 | { 31 | while (arr[L] < Pivot) 32 | L++; 33 | while (arr[R] > Pivot) 34 | R--; 35 | if (L <= R) 36 | { 37 | if (L != R) 38 | swap(&arr[L], &arr[R]); 39 | L++, R--; 40 | } 41 | 42 | } 43 | if (left < R) 44 | Divide_Array(arr, left, R); 45 | if (L < right) 46 | Divide_Array(arr, L, right); 47 | 48 | 49 | } 50 | void swap(int* a, int* b) 51 | { 52 | int tmp; 53 | tmp = *a; 54 | *a = *b; 55 | *b = tmp; 56 | } 57 | -------------------------------------------------------------------------------- /Assignments/Week 1/정지호/SelectionSort_정지호.c: -------------------------------------------------------------------------------- 1 | #include 2 | void swap(int* a, int* b); 3 | int main() { 4 | 5 | int n, i, j, max, tmp; 6 | int arr[1000], * par; 7 | scanf("%d", &n); 8 | 9 | for (par = arr; par < arr + n; par++) 10 | scanf("%d", par); 11 | par = arr; 12 | for (i = 0; i < n; i++) 13 | { 14 | max = i; 15 | for (j = i; j < n; j++) 16 | { 17 | if (arr[max] > arr[j]) 18 | max = j; 19 | } 20 | tmp = arr[i]; 21 | arr[i] = arr[max]; 22 | arr[max] = tmp; 23 | } 24 | 25 | for (par = arr; par < arr + n; par++) 26 | printf("%d\n", *par); 27 | 28 | return 0; 29 | 30 | } 31 | void swap(int* a, int* b) 32 | { 33 | int tmp; 34 | tmp = *a; 35 | *a = *b; 36 | *b = tmp; 37 | } -------------------------------------------------------------------------------- /Assignments/Week 1/정지호/정지호_1주차_보고서.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Assignments/Week 1/정지호/정지호_1주차_보고서.pdf -------------------------------------------------------------------------------- /Assignments/Week 1/정지호/정지호_1주차_보고서.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Assignments/Week 1/정지호/정지호_1주차_보고서.xlsx -------------------------------------------------------------------------------- /Assignments/Week 1/허정민/BubbleSort_허정민.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | int N, i, j, *num, tmp; 7 | scanf("%d", &N); 8 | num = (int*)malloc(sizeof(int)*N); 9 | for (i = 0; i < N; i++) { 10 | scanf("%d", &num[i]); 11 | } 12 | for (i = 0; i < N - 1; i++) { 13 | for (j = 0; j < N - i - 1; j++) { 14 | if (num[j] > num[j+1]) { 15 | tmp = num[j]; 16 | num[j] = num[j + 1]; 17 | num[j + 1] = tmp; 18 | } 19 | } 20 | } 21 | for (i = 0; i < N; i++) { 22 | printf("%d ", num[i]); 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Assignments/Week 1/허정민/InsertionSort_허정민.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct Node { 6 | struct Node *next, *prev; 7 | int data; 8 | }Node; 9 | 10 | Node* getNode() { 11 | Node *p = (Node*)malloc(sizeof(Node)); 12 | p->next = NULL; 13 | p->prev = NULL; 14 | return p; 15 | } 16 | 17 | void print(Node *p) { 18 | while (p) { 19 | printf("%d ", p->data); 20 | p = p->next; 21 | } 22 | } 23 | 24 | void freeNode(Node *p) { 25 | if (p->next) freeNode(p->next); 26 | free(p); 27 | } 28 | 29 | void insert_sort(Node *arr) { 30 | Node *p, *q; 31 | for (p = arr->next->next; p; p = p->next) { 32 | for (q = arr->next; q->data <= p->data&&p != q; q = q->next); 33 | if (p == q) continue; 34 | p->prev->next = p->next; 35 | if(p->next) p->next->prev = p->prev; 36 | q->prev->next = p; 37 | p->next = q; 38 | p->prev = q->prev; 39 | q->prev = p; 40 | } 41 | } 42 | 43 | int main() { 44 | int N, i; 45 | Node *list = getNode(), *p = list; 46 | scanf("%d", &N); 47 | for (i = 0; i < N; i++) { 48 | p->next = getNode(); 49 | p->next->prev = p; 50 | scanf("%d", &p->next->data); 51 | p = p->next; 52 | } 53 | insert_sort(list); 54 | print(list->next); 55 | freeNode(list); 56 | return 0; 57 | } -------------------------------------------------------------------------------- /Assignments/Week 1/허정민/MergeSort_허정민.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct Node { 6 | struct Node *next; 7 | int data; 8 | }Node; 9 | 10 | typedef struct List { 11 | Node *head, *last; 12 | int length; 13 | }List; 14 | 15 | Node* getNode() { 16 | Node *p = (Node*)malloc(sizeof(Node)); 17 | p->next = NULL; 18 | return p; 19 | } 20 | 21 | void setList(List *list) { 22 | list->head = getNode(); 23 | list->last = list->head; 24 | list->length = 0; 25 | } 26 | 27 | List merge(List *left, List *right) { 28 | Node *p = left->head->next, *q = right->head->next; 29 | List list; 30 | setList(&list); 31 | list.length = left->length + right->length; 32 | while (p&&q) { 33 | if (p->data > q->data) { 34 | list.last->next = q; 35 | list.last = list.last->next; 36 | q = q->next; 37 | } 38 | else { 39 | list.last->next = p; 40 | list.last = list.last->next; 41 | p = p->next; 42 | } 43 | } 44 | while (p) { 45 | list.last->next = p; 46 | list.last = list.last->next; 47 | p = p->next; 48 | } 49 | while (q) { 50 | list.last->next = q; 51 | list.last = list.last->next; 52 | q = q->next; 53 | } 54 | return list; 55 | } 56 | List merge_sort(List *list) { 57 | int i; 58 | List left, right; 59 | Node *p = list->head->next; 60 | if (list->length == 1) return *list; 61 | setList(&left); 62 | setList(&right); 63 | left.head->next = p; 64 | for (i = 0; i < list->length / 2 - 1; i++) { 65 | p = p->next; 66 | } 67 | left.last = p; 68 | right.head->next = p->next; 69 | left.last->next = NULL; 70 | right.last = list->last; 71 | left.length = list->length / 2; 72 | right.length = list->length - left.length; 73 | left = merge_sort(&left); 74 | right = merge_sort(&right); 75 | return merge(&left, &right); 76 | } 77 | 78 | void append(List *list) { 79 | Node *new_node = getNode(); 80 | scanf("%d",&new_node->data); 81 | list->last->next = new_node; 82 | list->last = new_node; 83 | list->length++; 84 | } 85 | 86 | void print(Node *p) { 87 | while (p) { 88 | printf("%d ", p->data); 89 | p = p->next; 90 | } 91 | } 92 | 93 | void freeNode(Node *p) { 94 | if(p->next) freeNode(p->next); 95 | free(p); 96 | } 97 | 98 | int main() { 99 | int N, i; 100 | List list; 101 | setList(&list); 102 | scanf("%d", &N); 103 | for (i = 0; i < N; i++) append(&list); 104 | list = merge_sort(&list); 105 | print(list.head->next); 106 | freeNode(list.head->next); 107 | return 0; 108 | } -------------------------------------------------------------------------------- /Assignments/Week 1/허정민/QuickSort_허정민.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct Node { 6 | struct Node *next; 7 | int data; 8 | }Node; 9 | 10 | typedef struct List { 11 | Node *head, *last; 12 | int length; 13 | }List; 14 | 15 | Node* getNode() { 16 | Node *p = (Node*)malloc(sizeof(Node)); 17 | p->next = NULL; 18 | return p; 19 | } 20 | 21 | void setList(List *list) { 22 | list->head = getNode(); 23 | list->last = list->head; 24 | list->length = 0; 25 | } 26 | 27 | 28 | 29 | void append(List *list) { 30 | Node *new_node = getNode(); 31 | scanf("%d", &new_node->data); 32 | list->last->next = new_node; 33 | list->last = new_node; 34 | list->length++; 35 | } 36 | 37 | void print(Node *p) { 38 | while (p) { 39 | printf("%d ", p->data); 40 | p = p->next; 41 | } 42 | } 43 | 44 | List quick_sort(List *list) { 45 | int i; 46 | List left, right; 47 | Node *pivot = list->head->next, *p; 48 | if (list->length <= 1) return *list; 49 | setList(&left); 50 | setList(&right); 51 | for (i = 0; i < list->length / 2; i++) { 52 | pivot = pivot->next; 53 | } 54 | for (p = list->head->next; p;) { 55 | if (p == pivot) { 56 | p = p->next; 57 | continue; 58 | } 59 | if (p->data < pivot->data) { 60 | left.last->next = p; 61 | p = p->next; 62 | left.last = left.last->next; 63 | left.last->next = NULL; 64 | left.length++; 65 | } 66 | else { 67 | right.last->next = p; 68 | p = p->next; 69 | right.last = right.last->next; 70 | right.last->next = NULL; 71 | right.length++; 72 | } 73 | } 74 | 75 | left = quick_sort(&left); 76 | right = quick_sort(&right); 77 | pivot->next = NULL; 78 | 79 | left.last->next = pivot; 80 | left.last = left.last->next; 81 | if (right.length != 0) { 82 | left.last->next = right.head->next; 83 | left.last = right.last; 84 | } 85 | left.length += right.length + 1; 86 | return left; 87 | } 88 | 89 | void freeNode(Node *p) { 90 | if (p->next) freeNode(p->next); 91 | free(p); 92 | } 93 | 94 | int main() { 95 | int N, i; 96 | List list; 97 | setList(&list); 98 | scanf("%d", &N); 99 | for (i = 0; i < N; i++) append(&list); 100 | list=quick_sort(&list); 101 | print(list.head->next); 102 | freeNode(list.head->next); 103 | return 0; 104 | } -------------------------------------------------------------------------------- /Assignments/Week 1/허정민/SelectionSort_허정민.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | int N, i, j, *num, tmp, min; 7 | scanf("%d", &N); 8 | num = (int*)malloc(sizeof(int)*N); 9 | for (i = 0; i < N; i++) scanf("%d", &num[i]); 10 | 11 | for (i = 0; i < N - 1; i++) { 12 | min = i + 1; 13 | for (j = i + 2; j < N; j++) { 14 | if (num[j] < num[min]) { 15 | min = j; 16 | } 17 | } 18 | if (num[i] > num[min]) { 19 | tmp = num[i]; 20 | num[i] = num[min]; 21 | num[min] = tmp; 22 | } 23 | } 24 | 25 | for (i = 0; i < N; i++) { 26 | printf("%d ", num[i]); 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Assignments/Week 1/허정민/허정민_1주차_보고서.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Assignments/Week 1/허정민/허정민_1주차_보고서.pdf -------------------------------------------------------------------------------- /Assignments/Week 2/김현영/BST_김현영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct Node { 5 | int elem; 6 | struct Node* left, * right, * parent; 7 | }Node; 8 | 9 | Node* createnode(int data) 10 | { 11 | Node* node = (Node*)malloc(sizeof(Node)); 12 | node->elem = data; 13 | node->parent = NULL; 14 | node->left = NULL; 15 | node->right = NULL; 16 | return node; 17 | } 18 | 19 | void PreOrder(Node* root) 20 | { 21 | if (root == NULL) return; 22 | printf("%d ", root->elem); 23 | PreOrder(root->left); 24 | PreOrder(root->right); 25 | } 26 | 27 | 28 | Node* Find(Node* tree, int data) 29 | { 30 | if (tree->elem > data) 31 | { 32 | if (tree->left == NULL) 33 | return tree; 34 | else Find(tree->left, data); 35 | } 36 | else if (tree->elem < data) 37 | { 38 | if (tree->right == NULL) return tree; 39 | else Find(tree->right, data); 40 | } 41 | else return tree; 42 | } 43 | 44 | 45 | void Insert(Node** tree, int data) 46 | { 47 | Node* tmp = *tree; 48 | if (*tree == NULL) 49 | * tree = createnode(data); 50 | else 51 | { 52 | tmp = Find(*tree, data); 53 | if (tmp->elem > data) 54 | { 55 | tmp->left = createnode(data); 56 | tmp->left->parent = tmp; 57 | } 58 | else 59 | { 60 | tmp->right = createnode(data); 61 | tmp->right->parent = tmp; 62 | } 63 | } 64 | } 65 | 66 | Node* SuccessorNode(Node* node) 67 | { 68 | if (node->left == NULL) 69 | return node; 70 | else 71 | SuccessorNode(node->left); 72 | } 73 | 74 | 75 | void Delete(Node** tree, int data) 76 | { 77 | int tmpnum; 78 | Node* tmp = *tree, * child = NULL, * parent = NULL, * successor = NULL; 79 | 80 | tmp = Find(*tree, data); 81 | 82 | if (tmp->left == NULL && tmp->right == NULL) // child 0 83 | { 84 | if (tmp->parent) 85 | { 86 | parent = tmp->parent; 87 | if (parent->left == tmp) 88 | parent->left = NULL; 89 | else 90 | parent->right = NULL; 91 | } 92 | else *tree = NULL; 93 | return; 94 | } 95 | 96 | else if ((tmp->left == NULL) || (tmp->right == NULL)) //child 1 97 | { 98 | if (tmp->left == NULL) 99 | child = tmp->right; 100 | else 101 | child = tmp->left; 102 | if (tmp->parent) 103 | { 104 | parent = tmp->parent; 105 | if (parent->left == tmp) 106 | parent->left = child; 107 | else 108 | parent->right = child; 109 | child->parent = parent; 110 | } 111 | else 112 | *tree = child; 113 | } 114 | else //child 2 115 | { 116 | successor = SuccessorNode(tmp->right); 117 | tmpnum = tmp->elem; 118 | tmp->elem = successor->elem; 119 | successor->elem = tmpnum; 120 | Delete(&successor, successor->elem); 121 | } 122 | } 123 | 124 | 125 | 126 | int main() { 127 | Node* root = NULL; 128 | int i, n, data; 129 | 130 | scanf("%d", &n); 131 | 132 | for (i = 0; i < n; i++) 133 | { 134 | scanf("%d", &data); 135 | Insert(&root, data); 136 | } 137 | PreOrder(root); 138 | printf("\ndelete start : "); 139 | 140 | scanf("%d", &n); 141 | for (i = 0; i < n; i++) 142 | { 143 | scanf("%d", &data); 144 | Delete(&root, data); 145 | } 146 | PreOrder(root); 147 | } -------------------------------------------------------------------------------- /Assignments/Week 2/김현영/Baekjoon_11286_김현영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void Swap(int* a, int* b) 6 | { 7 | int tmp; 8 | tmp = *a; 9 | *a = *b; 10 | *b = tmp; 11 | } 12 | void UpHeap(int* arr, int n) 13 | { 14 | if (n <= 1) return; 15 | 16 | if (abs(arr[n]) < abs(arr[n / 2])) 17 | Swap(&arr[n], &arr[n / 2]); 18 | 19 | else if (abs(arr[n]) == abs(arr[n / 2])) 20 | { 21 | if (arr[n] < arr[n / 2]) 22 | Swap(&arr[n], &arr[n / 2]); 23 | } 24 | UpHeap(arr, n / 2); 25 | } 26 | 27 | void Delete(int* arr, int* n) 28 | { 29 | if (*n < 1) 30 | { 31 | printf("0\n"); 32 | return; 33 | } 34 | else 35 | { 36 | printf("%d\n", arr[1]); 37 | Swap(&arr[1], &arr[*n]); 38 | arr[*n] = NULL; 39 | (*n)--; 40 | } 41 | } 42 | 43 | void DownHeap(int* arr, int self, int n) 44 | { 45 | 46 | int left = self * 2, right = left + 1; 47 | 48 | if (left > n) return; 49 | 50 | if (right <= n) //right o 51 | { 52 | if (abs(arr[self]) > abs(arr[left]) || abs(arr[self]) > abs(arr[right])) 53 | { 54 | if (abs(arr[left]) < abs(arr[right])) 55 | { 56 | Swap(&arr[self], &arr[left]); 57 | DownHeap(arr, left, n); 58 | } 59 | else if (abs(arr[left]) == abs(arr[right])) 60 | { 61 | if (arr[left] < arr[right]) 62 | { 63 | Swap(&arr[self], &arr[left]); 64 | DownHeap(arr, left, n); 65 | } 66 | else 67 | { 68 | Swap(&arr[self], &arr[right]); 69 | DownHeap(arr, right, n); 70 | } 71 | } 72 | else 73 | { 74 | Swap(&arr[self], &arr[right]); 75 | DownHeap(arr, right, n); 76 | } 77 | } 78 | else if (abs(arr[self]) == abs(arr[left]) || abs(arr[self]) == abs(arr[right])) 79 | { 80 | if (arr[self] > arr[left]) 81 | { 82 | Swap(&arr[self], &arr[left]); 83 | DownHeap(arr, left, n); 84 | } 85 | else if (arr[self] > arr[right]) 86 | { 87 | Swap(&arr[self], &arr[right]); 88 | DownHeap(arr, right, n); 89 | } 90 | else 91 | return; 92 | } 93 | } 94 | else //only left 95 | { 96 | if (abs(arr[self]) > abs(arr[left])) 97 | Swap(&arr[self], &arr[left]); 98 | else if (abs(arr[self]) == abs(arr[left])) 99 | { 100 | if (arr[self] > arr[left]) 101 | Swap(&arr[self], &arr[left]); 102 | } 103 | } 104 | } 105 | 106 | 107 | int main() 108 | { 109 | int i, n, size = 0, data; 110 | int* arr; 111 | 112 | scanf("%d", &n); 113 | arr = (int*)malloc(sizeof(int) * n); 114 | 115 | for (i = 1; i <= n; i++) 116 | { 117 | scanf("%d", &data); 118 | if (data != 0) 119 | { 120 | size++; 121 | arr[size] = data; 122 | UpHeap(arr, size); 123 | } 124 | else 125 | { 126 | Delete(arr, &size); 127 | DownHeap(arr,1,size); 128 | } 129 | } 130 | 131 | return 0; 132 | } -------------------------------------------------------------------------------- /Assignments/Week 2/김현영/Baekjoon_5639_김현영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct Node { 5 | int elem; 6 | struct Node* left, * right, * parent; 7 | }Node; 8 | 9 | Node* createnode(int data) 10 | { 11 | Node* node = (Node*)malloc(sizeof(Node)); 12 | node->elem = data; 13 | node->parent = NULL; 14 | node->left = NULL; 15 | node->right = NULL; 16 | return node; 17 | } 18 | 19 | void PostOrder(Node* root) 20 | { 21 | if (root == NULL) return; 22 | PostOrder(root->left); 23 | PostOrder(root->right); 24 | printf("%d ", root->elem); 25 | } 26 | 27 | Node* Find(Node* tree, int data) 28 | { 29 | if (tree->elem > data) 30 | { 31 | if (tree->left == NULL) 32 | return tree; 33 | else Find(tree->left, data); 34 | } 35 | else if (tree->elem < data) 36 | { 37 | if (tree->right == NULL) return tree; 38 | else Find(tree->right, data); 39 | } 40 | else return tree; 41 | } 42 | 43 | void Insert(Node** tree, int data) 44 | { 45 | Node* tmp = *tree; 46 | if (*tree == NULL) 47 | * tree = createnode(data); 48 | else 49 | { 50 | tmp = Find(*tree, data); 51 | if (tmp->elem > data) 52 | { 53 | tmp->left = createnode(data); 54 | tmp->left->parent = tmp; 55 | } 56 | else 57 | { 58 | tmp->right = createnode(data); 59 | tmp->right->parent = tmp; 60 | } 61 | } 62 | } 63 | 64 | 65 | int main() 66 | { 67 | int data, n; 68 | Node* root = NULL; 69 | 70 | while (scanf("%d", &data) != EOF) 71 | Insert(&root, data); 72 | 73 | PostOrder(root); 74 | 75 | } -------------------------------------------------------------------------------- /Assignments/Week 2/김현영/HeapSort_김현영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void Swap(int* a, int* b) 5 | { 6 | int tmp; 7 | tmp = *a; 8 | *a = *b; 9 | *b = tmp; 10 | } 11 | 12 | void BuildHeap(int* arr, int self, int n) 13 | { 14 | int left = self * 2, right = left + 1; 15 | if (left > n) return; 16 | 17 | if (right <= n) //right o 18 | { 19 | if (arr[self] < arr[left] || arr[self] < arr[right]) 20 | { 21 | if (arr[left] > arr[right]) 22 | { 23 | Swap(&arr[self], &arr[left]); 24 | BuildHeap(arr, left, n); 25 | } 26 | else 27 | { 28 | Swap(&arr[self], &arr[right]); 29 | BuildHeap(arr, right, n); 30 | } 31 | } 32 | } 33 | else //only left 34 | if (arr[self] < arr[left]) Swap(&arr[self], &arr[left]); 35 | } 36 | 37 | 38 | int main() 39 | { 40 | int i, n; 41 | int* arr; 42 | scanf("%d", &n); 43 | arr = (int*)malloc(sizeof(int) * (n + 1)); 44 | 45 | for (i = 1; i <= n; i++) 46 | scanf("%d", &arr[i]); 47 | 48 | for (i = n / 2; i > 0; i--) 49 | BuildHeap(arr, i, n); 50 | 51 | i = n; 52 | while (1) 53 | { 54 | Swap(&arr[1], &arr[i]); 55 | i--; 56 | BuildHeap(arr, 1, i); 57 | if (i <= 1) break; 58 | } 59 | 60 | for (i = 1; i <= n; i++) 61 | printf("%d ", arr[i]); 62 | 63 | 64 | } -------------------------------------------------------------------------------- /Assignments/Week 2/박상민/bst 삭제,insert,print.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by front on 2019-07-18. 3 | // 4 | 5 | #include 6 | typedef struct Node { 7 | struct Node *left; 8 | struct Node *right; 9 | struct Node *parent; 10 | int data; 11 | }Node; 12 | Node *get_node(Node*node, int data); 13 | void insert_node(Node **root, int data); 14 | void print_node(Node *root); 15 | void *delete_node(Node **node, int data); 16 | int main() { 17 | Node *root = NULL; 18 | int n, i, data, a; 19 | char ch = 0; 20 | /* 21 | while (scanf("%d", &data) != EOF){ 22 | insert_node(&root, data); 23 | } 24 | */ 25 | scanf("%d",&n); 26 | for (i = 0; i < n; i++) { 27 | scanf("%d", &data); 28 | insert_node(&root, data); 29 | } 30 | getchar(); 31 | while (ch != 'f') { 32 | scanf("%c", &ch); 33 | if (ch == 'I') { 34 | insert_node(&root, data); 35 | } 36 | else if (ch == 'd') { 37 | scanf("%d", &a); 38 | getchar(); 39 | delete_node(&root, a); 40 | } 41 | else if (ch == 'p') { 42 | print_node(root); 43 | printf("\n"); 44 | } 45 | } 46 | } 47 | Node *successor_node(Node **node, int data) { 48 | Node *output = NULL; 49 | if ((*node)->data == data) //첫 번째 입력일 때 50 | successor_node(&(*node)->right, data); 51 | else { 52 | if ((*node)->left != NULL) 53 | successor_node(&(*node)->left, data); 54 | else if ((*node)->left == NULL) { 55 | output = *node; 56 | *node = NULL; 57 | return output; 58 | } 59 | } 60 | } 61 | void *delete_node(Node **node, int data) { 62 | Node *tmp = NULL, *ttmp = NULL,*tmpr=NULL,*tmpl=NULL; 63 | if ((*node)->data == data) { 64 | if ((*node)->left == NULL && (*node)->right == NULL) { 65 | (*node) = NULL; 66 | } 67 | else if ((*node)->left == NULL && (*node)->right != NULL) { 68 | tmp = (*node)->right; 69 | ttmp = (*node)->parent; 70 | (*node) = NULL; 71 | ttmp->left = tmp; 72 | } 73 | else if ((*node)->left != NULL && (*node)->right == NULL) { 74 | tmp = (*node)->left; 75 | ttmp = (*node)->parent; 76 | (*node) = NULL; 77 | ttmp->right = tmp; 78 | } 79 | else if ((*node)->left != NULL && (*node)->right != NULL) { 80 | tmp = successor_node(&(*node), data); 81 | tmpr = (*node)->right; 82 | tmpl = (*node)->left; 83 | (*node) = tmp; 84 | (*node)->right = tmpr; 85 | (*node)->left = tmpl; 86 | } 87 | } 88 | else if ((*node)->data < data) { 89 | delete_node(&(*node)->right, data); 90 | } 91 | else if ((*node)->data > data) { 92 | delete_node(&(*node)->left, data); 93 | } 94 | } 95 | Node *get_node(Node*node, int data) { 96 | Node *newnode = (Node*)malloc(sizeof(Node)); 97 | newnode->data = data; 98 | newnode->left = NULL; 99 | newnode->right = NULL; 100 | newnode->parent = NULL; 101 | return newnode; 102 | } 103 | void insert_node(Node **node, int data) { 104 | if ((*node) == NULL) {//첫 번째일때 105 | (*node) = get_node(&(*node), data); 106 | } 107 | if ((*node)->data == data) 108 | return; 109 | else if ((*node)->data < data) { 110 | if ((*node)->right != NULL) 111 | insert_node(&(*node)->right, data); 112 | else { 113 | (*node)->right = get_node(*node, data); 114 | (*node)->right->parent = (*node); 115 | } 116 | } 117 | else if ((*node)->data > data) { 118 | if ((*node)->left != NULL) 119 | insert_node(&(*node)->left, data); 120 | else { 121 | (*node)->left = get_node(*node, data); 122 | (*node)->left->parent = (*node); 123 | } 124 | } 125 | } 126 | void print_node(Node *root) { 127 | if (root != NULL) { 128 | printf("%d\n", root->data); 129 | print_node(root->left); 130 | print_node(root->right); 131 | } 132 | } -------------------------------------------------------------------------------- /Assignments/Week 2/박상민/heap_sort.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by front on 2019-07-21. 3 | // 4 | 5 | #include 6 | #include 7 | void up_heap(int *arr, int i,int N); 8 | void swap(int *arr, int a, int b); 9 | int main() { 10 | int *arr, n, i,N=1; 11 | scanf("%d", &n); 12 | arr = (int*)malloc(sizeof(int)*(n+1)); 13 | for (i = 1; i < n+1; i++) { 14 | scanf("%d", &arr[i]); 15 | up_heap(arr, i,N++); 16 | } 17 | for (i = 1; i < n + 1; i++) { 18 | printf(" %d", arr[i]); 19 | } 20 | } 21 | void up_heap(int *arr, int i,int N) { 22 | if (i == 1) 23 | return; 24 | if (i == 2) { 25 | if (i == N) { 26 | if (arr[2] > arr[1]) 27 | swap(arr,1, 2); 28 | } 29 | else { 30 | if (arr[2] > arr[1]) 31 | swap(arr, 1, 2); 32 | if (arr[3] > arr[1]) 33 | swap(arr, 1, 3); 34 | up_heap(arr, i / 2, N); 35 | } 36 | } 37 | else if (i == 3) { 38 | if (arr[3] > arr[1]) 39 | swap(arr, 1, 3); 40 | if (arr[2] > arr[1]) 41 | swap(arr, 1, 2); 42 | } 43 | else if(i>3){ 44 | if (arr[i / 2] < arr[i]) 45 | swap(arr, i / 2, i); 46 | if (i % 2 == 0) { 47 | if (arr[i / 2] < arr[i / 2 * 2 + 1]&&i/2*2+1 != N+1) 48 | swap(arr, i / 2, i / 2 * 2 + 1); 49 | } 50 | if (i % 2 == 1) { 51 | if (arr[i / 2] < arr[i / 2 * 2]&&i/2*2 != N-1) 52 | swap(arr, i / 2, i / 2 * 2); 53 | } 54 | up_heap(arr, i / 2, N); 55 | } 56 | } 57 | void swap(int *arr, int a, int b) { 58 | int tmp = 0; 59 | tmp = arr[a]; 60 | arr[a] = arr[b]; 61 | arr[b] = tmp; 62 | } -------------------------------------------------------------------------------- /Assignments/Week 2/박상민/상미인.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by front on 2019-07-22. 3 | // 4 | 5 | #include 6 | typedef struct Node { 7 | struct Node *left; 8 | struct Node *right; 9 | struct Node *parent; 10 | int data; 11 | }Node; 12 | Node *get_node(Node*node, int data); 13 | void insert_node(Node **root, int data); 14 | void print_node(Node *root); 15 | void *delete_node(Node **node, int data); 16 | int main() { 17 | Node *root = NULL; 18 | int n, i, data, a; 19 | char ch = 0; 20 | /* 21 | while (scanf("%d", &data) != EOF){ 22 | insert_node(&root, data); 23 | } 24 | */ 25 | 26 | scanf("%d",&n); 27 | for (i = 0; i < n; i++) { 28 | scanf("%d", &data); 29 | insert_node(&root, data); 30 | } 31 | getchar(); 32 | while (ch != 'f') { 33 | scanf("%c", &ch); 34 | if (ch == 'I') { 35 | insert_node(&root, data); 36 | } 37 | else if (ch == 'd') { 38 | scanf("%d", &a); 39 | getchar(); 40 | delete_node(&root, a); 41 | } 42 | else if (ch == 'p') { 43 | print_node(root); 44 | printf("\n"); 45 | } 46 | } 47 | } 48 | Node *successor_node(Node **node, int data) { 49 | Node *output = NULL; 50 | if ((*node)->data == data) //첫 번째 입력일 때 51 | successor_node(&(*node)->right, data); 52 | else { 53 | if ((*node)->left != NULL) 54 | successor_node(&(*node)->left, data); 55 | else if ((*node)->left == NULL) { 56 | output = *node; 57 | *node = NULL; 58 | return output; 59 | } 60 | } 61 | } 62 | void *delete_node(Node **node, int data) { 63 | Node *tmp = NULL, *ttmp = NULL,*tmpr=NULL,*tmpl=NULL; 64 | if ((*node)->data == data) { 65 | if ((*node)->left == NULL && (*node)->right == NULL) { 66 | (*node) = NULL; 67 | } 68 | else if ((*node)->left == NULL && (*node)->right != NULL) { 69 | tmp = (*node)->right; 70 | ttmp = (*node)->parent; 71 | (*node) = NULL; 72 | ttmp->left = tmp; 73 | } 74 | else if ((*node)->left != NULL && (*node)->right == NULL) { 75 | tmp = (*node)->left; 76 | ttmp = (*node)->parent; 77 | (*node) = NULL; 78 | ttmp->right = tmp; 79 | } 80 | else if ((*node)->left != NULL && (*node)->right != NULL) { 81 | tmp = successor_node(&(*node), data); 82 | tmpr = (*node)->right; 83 | tmpl = (*node)->left; 84 | (*node) = tmp; 85 | (*node)->right = tmpr; 86 | (*node)->left = tmpl; 87 | } 88 | } 89 | else if ((*node)->data < data) { 90 | delete_node(&(*node)->right, data); 91 | } 92 | else if ((*node)->data > data) { 93 | delete_node(&(*node)->left, data); 94 | } 95 | } 96 | Node *get_node(Node*node, int data) { 97 | Node *newnode = (Node*)malloc(sizeof(Node)); 98 | newnode->data = data; 99 | newnode->left = NULL; 100 | newnode->right = NULL; 101 | newnode->parent = NULL; 102 | return newnode; 103 | } 104 | void insert_node(Node **node, int data) { 105 | if ((*node) == NULL) {//첫 번째일때 106 | (*node) = get_node(&(*node), data); 107 | } 108 | if ((*node)->data == data) 109 | return; 110 | else if ((*node)->data < data) { 111 | if ((*node)->right != NULL) 112 | insert_node(&(*node)->right, data); 113 | else { 114 | (*node)->right = get_node(*node, data); 115 | (*node)->right->parent = (*node); 116 | } 117 | } 118 | else if ((*node)->data > data) { 119 | if ((*node)->left != NULL) 120 | insert_node(&(*node)->left, data); 121 | else { 122 | (*node)->left = get_node(*node, data); 123 | (*node)->left->parent = (*node); 124 | } 125 | } 126 | } 127 | void print_node(Node *root) { 128 | if (root != NULL) { 129 | printf("%d\n", root->data); 130 | print_node(root->left); 131 | print_node(root->right); 132 | } 133 | } -------------------------------------------------------------------------------- /Assignments/Week 2/박상민/절댓값힙.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by front on 2019-07-21. 3 | // 4 | 5 | // 6 | // Created by front on 2019-07-21. 7 | // 8 | 9 | #include 10 | #include 11 | void up_heap(int *arr, int i); 12 | void swap(int *arr, int a, int b); 13 | void heap_down(int *arr, int i, int n); 14 | int main() { 15 | int *arr, n, i, input = 0, N = 1; 16 | scanf("%d", &n); 17 | arr = (int*)malloc(sizeof(int)*(n + 1)); 18 | 19 | for (i = 1; i < n + 1; i++) { 20 | scanf("%d", &input); 21 | if (input != 0) { 22 | arr[N++] = input; 23 | up_heap(arr, N - 1); 24 | } 25 | else if (input == 0) {//arr배열이 arr[10]까지 있다면 n은 11 26 | if (N < 2) 27 | printf("0\n"); 28 | else { 29 | heap_down(arr, 1, N - 1); 30 | N--; 31 | } 32 | } 33 | } 34 | } 35 | void heap_down(int *arr, int i, int n) {//arr배열이 arr[10]까지 있으면 n은 10 36 | if (n == 1) { 37 | printf("%d\n", arr[1]); 38 | return; 39 | } 40 | if (i > n - 1) //base 41 | return; 42 | if (i == 1) { 43 | if (n == 2) { 44 | printf("%d\n", arr[1]); 45 | arr[1] = arr[2]; 46 | return; 47 | } 48 | else if (n == 3) { 49 | printf("%d\n", arr[1]); 50 | arr[1] = arr[n]; 51 | if (abs(arr[1]) > abs(arr[2])) 52 | swap(arr, 1, 2); 53 | else if (abs(arr[1]) == abs(arr[2])) { 54 | if (arr[1] > arr[2]) 55 | swap(arr, 1, 2); 56 | } 57 | return; 58 | } 59 | else { 60 | printf("%d\n", arr[1]);//뽑아 냈음 61 | arr[1] = arr[n]; 62 | if (abs(arr[1]) > abs(arr[2])) { 63 | swap(arr, 1, 2); 64 | heap_down(arr, 2, n); 65 | } 66 | else if (abs(arr[1]) == abs(arr[2])) { 67 | if (arr[1] > arr[2]) 68 | swap(arr, 1, 2); 69 | heap_down(arr, 2, n); 70 | } 71 | 72 | if (abs(arr[1]) > abs(arr[3])) { 73 | swap(arr, 1, 3); 74 | heap_down(arr, 3, n); 75 | } 76 | else if (abs(arr[1]) == abs(arr[3])) { 77 | if (arr[1] > arr[3]) 78 | swap(arr, 1, 3); 79 | heap_down(arr, 3, n); 80 | } 81 | } 82 | } 83 | else { 84 | if (abs(arr[i]) > abs(arr[i * 2])) {//왼쪽 85 | swap(arr, i, i * 2); 86 | heap_down(arr, i * 2, n); 87 | } 88 | else if (abs(arr[i]) == abs(arr[i * 2])) { 89 | if (arr[i] > arr[i * 2]) 90 | swap(arr, i, i * 2); 91 | heap_down(arr, i * 2, n); 92 | } 93 | 94 | if (abs(arr[i]) > abs(arr[i * 2 + 1])) {//오른쪽 95 | swap(arr, i, i * 2 + 1); 96 | heap_down(arr, i * 2 + 1, n); 97 | } 98 | else if (abs(arr[i]) == abs(arr[i * 2 + 1])) { 99 | if (arr[i] > arr[i * 2 + 1]) 100 | swap(arr, i, i * 2 + 1); 101 | heap_down(arr, i * 2 + 1, n); 102 | } 103 | } 104 | } 105 | void up_heap(int *arr, int i) { 106 | if (i == 1) //재귀 함수 끝내는 조건 107 | return; 108 | else if (i == 2) { 109 | if (abs(arr[1]) > abs(arr[2])) 110 | swap(arr, 1, 2); 111 | else if (abs(arr[1]) == abs(arr[2])) { 112 | if (arr[1] > arr[2]) 113 | swap(arr, 1, 2); 114 | } 115 | 116 | if (abs(arr[1]) > abs(arr[3])) 117 | swap(arr, 1, 3); 118 | else if (abs(arr[1] == abs(arr[3]))) { 119 | if (arr[1] > arr[3]) 120 | swap(arr, 1, 3); 121 | } 122 | up_heap(arr, 1); 123 | } 124 | else if (i == 3) { 125 | if (abs(arr[1]) > abs(arr[3])) 126 | swap(arr, 1, 3); 127 | else if (abs(arr[1] == abs(arr[3]))) { 128 | if (arr[1] > arr[3]) 129 | swap(arr, 1, 3); 130 | } 131 | 132 | if (abs(arr[1]) > abs(arr[2])) 133 | swap(arr, 1, 2); 134 | else if (abs(arr[1] == abs(arr[2]))) { 135 | if (arr[1] > arr[2]) 136 | swap(arr, 1, 2); 137 | } 138 | up_heap(arr, 1); 139 | } 140 | else { 141 | if (abs(arr[i]) < abs(arr[i / 2])) { // i/2=부모 노드 142 | swap(arr, i, i / 2); 143 | up_heap(arr, i / 2); 144 | } 145 | else if (abs(arr[i]) == abs(arr[i / 2])) { 146 | if (arr[i] < arr[i / 2]) 147 | swap(arr, i, i / 2); 148 | up_heap(arr, i / 2); 149 | } 150 | 151 | if (i % 2 == 1) { 152 | if (abs(arr[i / 2] < abs(arr[i / 2 * 2]))) { 153 | swap(arr, i / 2 * 2, i / 2); 154 | up_heap(arr, i / 2); 155 | } 156 | else if (abs(arr[i / 2]) == abs(arr[i / 2 * 2])) { 157 | if (arr[i / 2] > arr[i / 2 * 2]) 158 | swap(arr, i / 2, i / 2 * 2); 159 | up_heap(arr, i / 2); 160 | } 161 | } 162 | else if (i % 2 == 0) { 163 | if (abs(arr[i / 2] < abs(arr[i / 2 * 2+1]))) { 164 | swap(arr, i / 2 * 2, i / 2); 165 | up_heap(arr, i / 2); 166 | } 167 | else if (abs(arr[i / 2]) == abs(arr[i / 2 * 2 + 1])) { 168 | if (arr[i / 2] > arr[i / 2 * 2 + 1]) 169 | swap(arr, i / 2, i / 2 * 2 + 1); 170 | up_heap(arr, i / 2); 171 | } 172 | } 173 | } 174 | } 175 | void swap(int *arr, int a, int b) { 176 | int tmp = 0; 177 | tmp = arr[a]; 178 | arr[a] = arr[b]; 179 | arr[b] = tmp; 180 | } -------------------------------------------------------------------------------- /Assignments/Week 2/이승훈/BST.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Assignments/Week 2/이승훈/BST.c -------------------------------------------------------------------------------- /Assignments/Week 2/이승훈/Heap.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void Swap(int *a, int *b); 6 | int Absolute(int a, int b); 7 | void UpHeap(int *p, int index); 8 | void DownHeap(int *p, int a, int b); 9 | 10 | int main() { 11 | int *p, N, i, j, data, index = 1; 12 | 13 | scanf("%d", &N); 14 | p = (int *)malloc(sizeof(int) * (N + 1)); 15 | for (i = 1; i <= N; i++) { 16 | scanf("%d", &data); 17 | if (data == 0) { 18 | if (index == 1) { 19 | printf("0\n"); 20 | continue; 21 | } 22 | printf("%d\n", p[1]); 23 | p[1] = p[--index]; 24 | DownHeap(p, 1, index); 25 | } 26 | else { 27 | p[index] = data; 28 | UpHeap(p, index++); 29 | } 30 | } 31 | 32 | return 0; 33 | } 34 | 35 | 36 | void UpHeap(int *p, int index) { 37 | if (index == 1 || Absolute(p[index], p[index / 2]) == p[index / 2]) return; 38 | else { 39 | Swap(&p[index], &p[index / 2]); 40 | UpHeap(p, index / 2); 41 | } 42 | } 43 | 44 | void DownHeap(int *p, int a, int b) { 45 | if (a * 2 > b) return; 46 | else if (a * 2 < b) { 47 | if (Absolute(p[a * 2], p[a * 2 + 1]) == p[a * 2]) { 48 | if (Absolute(p[a], p[a * 2]) == p[a * 2]) { 49 | Swap(&p[a], &p[a * 2]); 50 | DownHeap(p, a * 2, b); 51 | } 52 | else return; 53 | } 54 | else { 55 | if (Absolute(p[a], p[a * 2 + 1]) == p[a * 2 + 1]) { 56 | Swap(&p[a], &p[a * 2 + 1]); 57 | DownHeap(p, a * 2 + 1, b); 58 | } 59 | else return; 60 | } 61 | } 62 | else { 63 | if (Absolute(p[a], p[a * 2]) == p[a * 2]) { 64 | Swap(&p[a], &p[a * 2]); 65 | DownHeap(p, a * 2, b); 66 | } 67 | else return; 68 | } 69 | } 70 | 71 | int Absolute(int a, int b) { 72 | int x = a, y = b; 73 | if (a <= 0) x = a * (-1); 74 | if (b <= 0) y = b * (-1); 75 | if (x < y) return a; 76 | else if (x > y) return b; 77 | else if (a < b) return a; 78 | else if (a > b) return b; 79 | } 80 | 81 | void Swap(int *a, int *b) { 82 | int tmp; 83 | tmp = *a; 84 | *a = *b; 85 | *b = tmp; 86 | } -------------------------------------------------------------------------------- /Assignments/Week 2/이우식/ABS.c: -------------------------------------------------------------------------------- 1 | #pragma warning (disable : 4996) 2 | #include 3 | #include 4 | #include 5 | int reset(int a) { 6 | if (a >= 0)return a; 7 | else return -a; 8 | } 9 | int vs(int a, int b) { 10 | if (reset(a) < reset(b)) return a; 11 | else if (reset(a) > reset(b)) return b; 12 | else { 13 | if (a < b) return a; 14 | else return b; 15 | } 16 | } 17 | void UP(int *heap, int index) { 18 | int tmp = 0; 19 | if (index == 1 || vs(heap[index / 2], heap[index]) == heap[index / 2]) return; 20 | else { 21 | tmp = heap[index]; 22 | heap[index] = heap[index / 2]; 23 | heap[index / 2] = tmp; 24 | UP(heap, index / 2); 25 | } 26 | } 27 | void DOWN(int *heap, int index, int N) { 28 | int tmp = 0; 29 | if (index * 2 > N) return; 30 | else if (index * 2 < N) { 31 | if (vs(heap[index * 2], heap[index * 2 + 1]) == heap[index * 2]) { 32 | if (vs(heap[index], heap[index * 2]) == heap[index * 2]) { 33 | tmp = heap[index]; 34 | heap[index] = heap[index * 2]; 35 | heap[index * 2] = tmp; 36 | DOWN(heap, index * 2, N); 37 | } 38 | else return; 39 | } 40 | else { 41 | if (vs(heap[index], heap[index * 2 + 1]) == heap[index * 2 + 1]) { 42 | tmp = heap[index]; 43 | heap[index] = heap[index * 2 + 1]; 44 | heap[index * 2 + 1] = tmp; 45 | DOWN(heap, index * 2 + 1, N); 46 | } 47 | else return; 48 | 49 | } 50 | } 51 | else { 52 | if (vs(heap[index], heap[index * 2]) == heap[index * 2]) { 53 | tmp = heap[index]; 54 | heap[index] = heap[index * 2]; 55 | heap[index * 2] = tmp; 56 | DOWN(heap, index * 2, N); 57 | } 58 | else return; 59 | } 60 | } 61 | void push(int *heap, int data, int N) { 62 | heap[N] = data; 63 | UP(heap, N); 64 | } 65 | int pop(int *heap, int N) { 66 | int tmp = heap[1]; 67 | heap[1] = heap[N]; 68 | heap[N] = NULL; 69 | DOWN(heap, 1, N - 1); 70 | 71 | return tmp; 72 | } 73 | int main() { 74 | int *heap; 75 | int N; 76 | int i; 77 | int data; 78 | int stack = 1; 79 | scanf("%d", &N); 80 | heap = (int*)malloc(sizeof(int)*(N + 1)); 81 | for (i = 1; i <= N; i++) { 82 | scanf("%d", &data); 83 | if (data != 0) { 84 | push(heap, data, stack++); 85 | } 86 | else { 87 | if (stack == 1) printf("0\n"); 88 | else printf("%d\n", pop(heap, stack--)); 89 | } 90 | } 91 | return 0; 92 | } -------------------------------------------------------------------------------- /Assignments/Week 2/이우식/Bst.c: -------------------------------------------------------------------------------- 1 | #pragma warning (disable :4996) 2 | #include 3 | #include 4 | #include 5 | typedef struct NODE { 6 | struct NODE *parent; 7 | struct NODE *left; 8 | struct NODE *right; 9 | int data; 10 | }NODE; 11 | NODE *getNode() { 12 | NODE *p; 13 | p = (NODE*)malloc(sizeof(NODE)); 14 | p->data = NULL; 15 | p->left = NULL; 16 | p->right = NULL; 17 | p->parent = NULL; 18 | return p; 19 | } 20 | void Bprint(NODE *p) { 21 | if (p->left != NULL) Bprint(p->left); 22 | if (p->right != NULL) Bprint(p->right); 23 | printf("%d\n", p->data); 24 | } 25 | void insert(NODE *h, int data) { 26 | NODE *p = h; 27 | NODE *tmp; 28 | if (h->data == data) return; 29 | if (data < p->data) { 30 | if (p->left != NULL) { 31 | insert(p->left, data); 32 | } 33 | else 34 | { 35 | tmp = getNode(); 36 | tmp->parent = p; 37 | tmp->data = data; 38 | p->left = tmp; 39 | } 40 | } 41 | else if (data > p->data) { 42 | if (p->right != NULL) { 43 | insert(p->right, data); 44 | } 45 | else 46 | { 47 | tmp = getNode(); 48 | tmp->parent = p; 49 | tmp->data = data; 50 | p->right = tmp; 51 | } 52 | } 53 | } 54 | void FR(NODE *p) { 55 | if (p->left != NULL) FR(p->left); 56 | if (p->right != NULL) FR(p->right); 57 | free(p); 58 | } 59 | int main() { 60 | NODE *h; 61 | NODE *tmp; 62 | int i=0; 63 | int data; 64 | h = getNode(); 65 | while ((scanf("%d", &data) != EOF)) { 66 | if (i == 0) { 67 | h->data = data; 68 | i++; 69 | continue; 70 | } 71 | insert(h, data); 72 | } 73 | Bprint(h); 74 | FR(h); 75 | return 0; 76 | } -------------------------------------------------------------------------------- /Assignments/Week 2/이우식/Hsort.c: -------------------------------------------------------------------------------- 1 | #pragma warning (disable : 4996) 2 | #include 3 | #include 4 | #include 5 | void UP(int *heap, int index) { 6 | int tmp = 0; 7 | if (index == 1 || heap[index / 2] < heap[index]) return; 8 | else { 9 | tmp = heap[index]; 10 | heap[index] = heap[index / 2]; 11 | heap[index / 2] = tmp; 12 | UP(heap, index / 2); 13 | } 14 | } 15 | void DOWN(int *heap, int index, int N) { 16 | int tmp = 0; 17 | if (index * 2 > N) return; 18 | else if (index * 2 < N) { 19 | if (heap[index * 2] < heap[index * 2 + 1]) { 20 | if (heap[index] > heap[index * 2]) { 21 | tmp = heap[index]; 22 | heap[index] = heap[index * 2]; 23 | heap[index * 2] = tmp; 24 | DOWN(heap, index * 2, N); 25 | } 26 | else return; 27 | } 28 | else { 29 | if (heap[index] > heap[index * 2 + 1]) { 30 | tmp = heap[index]; 31 | heap[index] = heap[index * 2 + 1]; 32 | heap[index * 2 + 1] = tmp; 33 | DOWN(heap, index * 2 + 1, N); 34 | } 35 | else return; 36 | } 37 | } 38 | else { 39 | if (heap[index] > heap[index * 2]) { 40 | tmp = heap[index]; 41 | heap[index] = heap[index * 2]; 42 | heap[index * 2] = tmp; 43 | DOWN(heap, index * 2, N); 44 | } 45 | else return; 46 | } 47 | } 48 | void push(int *heap, int data, int N) { 49 | heap[N] = data; 50 | UP(heap, N); 51 | } 52 | int pop(int *heap, int N) { 53 | int tmp = heap[1]; 54 | heap[1] = heap[N]; 55 | heap[N] = NULL; 56 | if (N != 1) DOWN(heap, 1, N - 1); 57 | 58 | return tmp; 59 | } 60 | int main() { 61 | int *heap; 62 | int N; 63 | int i; 64 | int data; 65 | scanf("%d", &N); 66 | heap = (int*)malloc(sizeof(int)*(N + 1)); 67 | for (i = 1; i <= N; i++) { 68 | scanf("%d", &data); 69 | push(heap, data, i); 70 | } 71 | for (i = 0; i < N; i++) { 72 | printf("%d\n", pop(heap, N - i)); 73 | } 74 | return 0; 75 | } -------------------------------------------------------------------------------- /Assignments/Week 2/전선영/11286번_전선영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | void swap(int *a, int *b) 6 | { 7 | int tmp; 8 | tmp = *a; 9 | *a = *b; 10 | *b = tmp; 11 | } 12 | void upheap(int *arr, int cnt) 13 | { 14 | int i = cnt; 15 | if (cnt == 1) 16 | return; 17 | if (abs(arr[i]) < abs(arr[i / 2])) 18 | { 19 | swap(arr + i, arr + (i / 2)); 20 | upheap(arr, cnt / 2); 21 | } 22 | else if (abs(arr[i]) == abs(arr[i / 2])) 23 | { 24 | if (arr[i] < arr[i / 2]) 25 | { 26 | swap(arr + i, arr + (i / 2)); 27 | upheap(arr, cnt / 2); 28 | } 29 | } 30 | } 31 | void downheap(int *arr, int i, int cnt) 32 | { 33 | if (i > cnt) 34 | return; 35 | if (i * 2 + 1 <= cnt) 36 | { 37 | if (abs(arr[i]) > abs(arr[i * 2]) || abs(arr[i]) > abs(arr[i * 2 + 1])) { 38 | if (abs(arr[i * 2]) < abs(arr[i * 2 + 1])) { 39 | swap(arr + i, arr + (i * 2)); 40 | downheap(arr, i * 2, cnt); 41 | } 42 | else if (abs(arr[i * 2]) == abs(arr[i * 2 + 1])) { 43 | if (arr[i * 2] < arr[i * 2 + 1]) { 44 | swap(arr + i, arr + (i * 2)); 45 | downheap(arr, i * 2, cnt); 46 | } 47 | else { 48 | swap(arr + i, arr + (i * 2) + 1); 49 | downheap(arr, i * 2 + 1, cnt); 50 | } 51 | } 52 | else { 53 | swap(arr + i, arr + (i * 2) + 1); 54 | downheap(arr, i * 2 + 1, cnt); 55 | } 56 | } 57 | else if (abs(arr[i]) == abs(arr[i * 2]) || abs(arr[i]) == abs(arr[i * 2 + 1])) 58 | { 59 | if (arr[i * 2] < arr[i * 2 + 1]) { 60 | if (arr[i] > arr[i * 2]) 61 | { 62 | swap(arr + i, arr + (i * 2)); 63 | downheap(arr, i * 2, cnt); 64 | } 65 | } 66 | else { 67 | if (arr[i] > arr[i * 2]) 68 | { 69 | swap(arr + i, arr + (i * 2) + 1); 70 | downheap(arr, i * 2 + 1, cnt); 71 | } 72 | } 73 | } 74 | } 75 | if (i * 2 <= cnt) { 76 | if (abs(arr[i]) > abs(arr[i * 2])) { 77 | swap(arr + i, arr + i * 2); 78 | downheap(arr, i * 2, cnt); 79 | } 80 | else if (abs(arr[i]) == abs(arr[i * 2])) { 81 | if (arr[i] > arr[i * 2]) { 82 | swap(arr + i, arr + i * 2); 83 | downheap(arr, i * 2, cnt); 84 | } 85 | } 86 | } 87 | } 88 | int main() 89 | { 90 | int n, i, a, *arr, cnt = 0; 91 | 92 | scanf("%d", &n); 93 | arr = (int *)malloc(sizeof(int)*n); 94 | arr[0] = 0; 95 | 96 | for (i = 0; i < n; i++) 97 | { 98 | scanf("%d", &a); 99 | if (a != 0) 100 | { 101 | cnt++; 102 | arr[cnt] = a; 103 | upheap(arr, cnt); 104 | } 105 | else 106 | { 107 | if (cnt == 0) 108 | printf("0\n"); 109 | else 110 | { 111 | printf("%d\n", arr[1]); 112 | arr[1] = arr[cnt]; 113 | arr[cnt] = 0; 114 | --cnt; 115 | downheap(arr, 1, cnt); 116 | } 117 | } 118 | } 119 | } -------------------------------------------------------------------------------- /Assignments/Week 2/전선영/5693번_전선영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | LARGE_INTEGER Frequency; 6 | LARGE_INTEGER BeginTime; 7 | LARGE_INTEGER Endtime; 8 | typedef struct Node 9 | { 10 | struct Node *left, *right; 11 | int elem; 12 | }Node; 13 | Node *get_node(int a) 14 | { 15 | Node *node; 16 | 17 | node = (Node*)malloc(sizeof(Node)); 18 | node->left = NULL; 19 | node->right = NULL; 20 | 21 | node->elem = a; 22 | 23 | return node; 24 | } 25 | void insert_node(Node *root, int a) 26 | { 27 | if (a == root->elem) 28 | return; 29 | else if (a < root->elem) 30 | { 31 | if (root->left == NULL) 32 | root->left = get_node(a); 33 | insert_node(root->left, a); 34 | } 35 | else if (a > root->elem) 36 | { 37 | if (root->right == NULL) 38 | root->right = get_node(a); 39 | insert_node(root->right, a); 40 | } 41 | } 42 | Node *make_tree() 43 | { 44 | int a, first = 0; 45 | Node *root=NULL,*node; 46 | 47 | while (scanf("%d", &a) != EOF) 48 | { 49 | if (first == 0) 50 | { 51 | root = get_node(a); 52 | node = root; 53 | ++first; 54 | } 55 | else 56 | insert_node(root, a); 57 | } 58 | 59 | return root; 60 | } 61 | void print_node(Node *node) 62 | { 63 | if(node == NULL) 64 | return ; 65 | print_node(node->left); 66 | printf("%d\n", node->elem); 67 | print_node(node->right); 68 | } 69 | int main() 70 | { 71 | int n; 72 | Node *node; 73 | 74 | node = make_tree(); 75 | 76 | print_node(node); 77 | 78 | } -------------------------------------------------------------------------------- /Assignments/Week 2/전선영/HeapSort_전선영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | void swap(int *a, int *b) 6 | { 7 | int tmp; 8 | tmp = *a; 9 | *a = *b; 10 | *b = tmp; 11 | } 12 | void downheap(int *arr, int i, int cnt) 13 | { 14 | if (i == 0) 15 | return; 16 | if (i * 2 + 1 <= cnt) 17 | { 18 | if (arr[i] > arr[i * 2] || arr[i] > arr[i * 2 + 1]) 19 | { 20 | if (arr[i * 2] < arr[i * 2 + 1]) 21 | swap(arr + i, arr + (i * 2)); 22 | else 23 | swap(arr + i, arr + (i * 2) + 1); 24 | } 25 | } 26 | else if (i * 2 <= cnt) { 27 | if (arr[i] > arr[i * 2]) { 28 | swap(arr + i, arr + i * 2); 29 | } 30 | } 31 | downheap(arr, --i, cnt); 32 | } 33 | int main() 34 | { 35 | int n, i, a, *arr; 36 | 37 | scanf("%d", &n); 38 | arr = (int *)malloc(sizeof(int)*(n + 1)); 39 | arr[0] = 0; 40 | 41 | for (i = 0; i < n; i++) 42 | { 43 | scanf("%d", &a); 44 | arr[i + 1] = a; 45 | } 46 | while (n > 0) { 47 | downheap(arr, n / 2, n); 48 | printf("%d\n", arr[1]); 49 | arr[1] = arr[n]; 50 | arr[n] = 0; 51 | n--; 52 | } 53 | } 54 | 55 | -------------------------------------------------------------------------------- /Assignments/Week 2/정지호/BST_정지호.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct node { 6 | struct node* left, * right; 7 | int id; 8 | }Node; 9 | void insert_tree(Node** Root, int num); 10 | void Remove_tree(Node* DT, Node** Root, Node** Parent); 11 | Node* Remove_node(Node* node); 12 | void Preorder_traverse(Node* Root); 13 | void Inorder_traverse(Node* Root); 14 | Node* find_tree(Node* Root, int num); 15 | Node* find_Parent(Node* Root, Node** Parent, int num); 16 | Node* getnode(int data); 17 | int main() 18 | { 19 | Node* Root = NULL,* PR = NULL, * Parent = NULL; 20 | int i; 21 | int N, num; 22 | char menu; 23 | 24 | scanf("%d", &N); 25 | for (i = 0; i < N; i++) 26 | { 27 | getchar(); 28 | scanf("%c %d", &menu, &num); 29 | 30 | switch (menu) 31 | { 32 | case'I': 33 | insert_tree(&Root, num); 34 | break; 35 | case'P': 36 | switch (num) 37 | { 38 | case 1: 39 | Preorder_traverse(Root); 40 | if (Root == NULL)printf("NULL"); 41 | printf("\n"); 42 | break; 43 | case 2: 44 | Inorder_traverse(Root); 45 | printf("\n"); 46 | break; 47 | } 48 | break; 49 | case'D': 50 | PR = find_Parent(Root, &Parent, num); 51 | Remove_tree(PR, &Root, &Parent); 52 | Parent = NULL; 53 | break; 54 | } 55 | } 56 | 57 | 58 | } 59 | void insert_tree(Node * *Root, int num) 60 | { 61 | if (*Root != NULL) 62 | { 63 | if (num < (*Root)->id) 64 | insert_tree(&(*Root)->left, num); 65 | else if (num > (*Root)->id) 66 | insert_tree(&(*Root)->right, num); 67 | } 68 | else 69 | *Root = getnode(num); 70 | } 71 | void Remove_tree(Node * DT, Node * *Root, Node * *Parent) 72 | { 73 | 74 | if (DT == NULL) 75 | printf("No Data\n"); 76 | else if (DT == *Root) * Root = Remove_node(DT); 77 | else if (DT == (*Parent)->left) 78 | (*Parent)->left = Remove_node(DT); 79 | else if (DT == (*Parent)->right) 80 | (*Parent)->right = Remove_node(DT); 81 | return; 82 | } 83 | Node * Remove_node(Node * node) 84 | { 85 | Node* Parent = NULL, * R = NULL; 86 | if (node->left == NULL && node->right == NULL) return NULL; 87 | else if (node->left == NULL && node->right != NULL) 88 | return node->right; 89 | else if (node->left != NULL && node->right == NULL) 90 | return node->left; 91 | else 92 | { 93 | R = node->right; 94 | while (R->left != NULL) 95 | { 96 | Parent = R; 97 | R = R->left; 98 | } 99 | node->id = R->id; 100 | if (R == node->right) node->right = R->right; 101 | else Parent->left = R->right; 102 | return node; 103 | } 104 | } 105 | void Preorder_traverse(Node * Root) 106 | { 107 | if (Root != NULL) 108 | { 109 | printf("%d ", Root->id); 110 | Preorder_traverse(Root->left); 111 | Preorder_traverse(Root->right); 112 | } 113 | } 114 | void Inorder_traverse(Node * Root) 115 | { 116 | if (Root != NULL) 117 | { 118 | Inorder_traverse(Root->left); 119 | printf("%d ", Root->id); 120 | Inorder_traverse(Root->right); 121 | 122 | } 123 | } 124 | Node* find_tree(Node * Root, int num) 125 | { 126 | Node* result = NULL; 127 | 128 | if (Root == NULL) return NULL; 129 | if (Root->id == num) return Root; 130 | if (Root->left != NULL) 131 | result = find_tree(Root->left, num); 132 | if (result == NULL) 133 | result = find_tree(Root->right, num); 134 | return result; 135 | } 136 | Node * find_Parent(Node * Root, Node * *Parent, int num) 137 | { 138 | Node* result = NULL; 139 | 140 | if (Root == NULL) return NULL; 141 | if (Root->id == num) return Root; 142 | if (Root->left != NULL) 143 | { 144 | *Parent = Root; 145 | result = find_Parent(Root->left, Parent, num); 146 | } 147 | if (result == NULL) 148 | { 149 | *Parent = Root; 150 | result = find_Parent(Root->right, Parent, num); 151 | } 152 | return result; 153 | } 154 | Node* getnode(int data) 155 | { 156 | Node* Newnode; 157 | Newnode = (Node*)malloc(sizeof(Node)); 158 | Newnode->id = data; 159 | Newnode->left = NULL; 160 | Newnode->right = NULL; 161 | return Newnode; 162 | } -------------------------------------------------------------------------------- /Assignments/Week 2/정지호/Beakjoon_5936_정지호.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct node { 6 | struct node* left, * right; 7 | int id; 8 | }Node; 9 | void Postorder_traverse(Node* Root); 10 | Node* getnode(int data); 11 | Node* PreOrder_GetNode(Node ** Root, int data); 12 | int main() 13 | { 14 | Node* Root = NULL; 15 | int num; 16 | 17 | 18 | while (scanf("%d", &num) != EOF) 19 | PreOrder_GetNode(&Root,num); 20 | Postorder_traverse(Root); 21 | } 22 | void Postorder_traverse(Node* Root) 23 | { 24 | if (Root != NULL) 25 | { 26 | Postorder_traverse(Root->left); 27 | Postorder_traverse(Root->right); 28 | printf("%d\n", Root->id); 29 | } 30 | } 31 | Node* getnode(int data) 32 | { 33 | Node* Newnode; 34 | Newnode = (Node*)malloc(sizeof(Node)); 35 | Newnode->id = data; 36 | Newnode->left = NULL; 37 | Newnode->right = NULL; 38 | return Newnode; 39 | } 40 | Node* PreOrder_GetNode(Node** Root, int data) 41 | { 42 | if (*Root == NULL) 43 | * Root = getnode(data); 44 | else if ((*Root)->id == data) 45 | return NULL; 46 | else if ((*Root)->id > data) 47 | PreOrder_GetNode(&(*Root)->left, data); 48 | else if ((*Root)->id < data) 49 | PreOrder_GetNode(&(*Root)->right, data); 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Assignments/Week 2/정지호/HeapSort_정지호.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Assignments/Week 2/정지호/HeapSort_정지호.c -------------------------------------------------------------------------------- /Assignments/Week 2/정지호/beakjoon_11286_정지호.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct Heap 5 | { 6 | int* arr, * Arr; 7 | int Index; 8 | int Min; 9 | }Heap; 10 | void UpHeap(Heap* H, int data); 11 | void DownHeap(Heap* H); 12 | void Swap2(Heap* H, int L, int R); 13 | void Realloc(Heap* H); 14 | int main() 15 | { 16 | int N, i, j; 17 | int data, Min; 18 | Heap H; 19 | 20 | scanf("%d", &N); 21 | H.arr = (int*)malloc(sizeof(int)); 22 | H.Arr = (int*)malloc(sizeof(int)); 23 | H.Index = 0; 24 | H.Min = 1; 25 | for (i = 0; i < N; i++) 26 | { 27 | scanf("%d", &data); 28 | if (data == 0) 29 | { 30 | if (H.Index == 0) 31 | printf("0\n"); 32 | else 33 | { 34 | H.Min = 1; 35 | printf("%d\n", H.arr[H.Min]); 36 | Swap2(&H, 1, H.Index); 37 | H.Index--; 38 | Realloc(&H); 39 | DownHeap(&H); 40 | } 41 | } 42 | else 43 | { 44 | H.Index++; 45 | Realloc(&H); 46 | H.arr[H.Index] = data; 47 | H.Arr[H.Index] = abs(data); 48 | UpHeap(&H, H.Index); 49 | } 50 | } 51 | free(H.arr); 52 | free(H.Arr); 53 | } 54 | void UpHeap(Heap* H, int Lastindex) 55 | { 56 | int index = Lastindex; 57 | if (index / 2 <= 0 || H->Arr[index / 2] < H->Arr[index]) 58 | return; 59 | else if (H->Arr[index / 2] > H->Arr[index]) 60 | Swap2(H, index, index / 2); 61 | else if (H->Arr[index / 2] == H->Arr[index]) 62 | { 63 | if (H->arr[index / 2] > H->arr[index]) 64 | Swap2(H, index, index / 2); 65 | else 66 | return; 67 | } 68 | index /= 2; 69 | UpHeap(H, index); 70 | } 71 | void DownHeap(Heap * H) 72 | { 73 | int Left, Right; 74 | int Compare; 75 | Left = H->Min * 2; 76 | Right = H->Min * 2 + 1; 77 | if (H->Index < Left) 78 | return; 79 | if (H->Index != Left) 80 | { 81 | if (H->Arr[Left] == H->Arr[Right]) 82 | Compare = H->arr[Left] < H->arr[Right] ? Left : Right; 83 | else 84 | Compare = H->Arr[Left] < H->Arr[Right] ? Left : Right; 85 | } 86 | if (H->Index == Left) 87 | { 88 | if (H->Arr[Left] < H->Arr[H->Min]) 89 | Swap2(H, H->Min, Left); 90 | else if (H->Arr[Left] == H->Arr[H->Min]) 91 | { 92 | if (H->arr[Left] < H->arr[H->Min]) 93 | Swap2(H, H->Min, Left); 94 | } 95 | 96 | } 97 | else if (H->Arr[Compare] < H->Arr[H->Min]) 98 | { 99 | Swap2(H, Compare, H->Min); 100 | DownHeap(H); 101 | } 102 | else if (H->Arr[Compare] == H->Arr[H->Min]) 103 | { 104 | if (H->arr[Compare] < H->arr[H->Min]) 105 | { 106 | Swap2(H, Compare, H->Min); 107 | DownHeap(H); 108 | } 109 | } 110 | } 111 | void Swap2(Heap * H, int L, int R) 112 | { 113 | int tmp, Tmp; 114 | tmp = H->arr[L]; 115 | H->arr[L] = H->arr[R]; 116 | H->arr[R] = tmp; 117 | Tmp = H->Arr[L]; 118 | H->Arr[L] = H->Arr[R]; 119 | H->Arr[R] = Tmp; 120 | H->Min = L; 121 | } 122 | void Realloc(Heap * H) 123 | { 124 | H->arr = (int*)realloc(H->arr, sizeof(int) * (H->Index + 1)); 125 | H->Arr = (int*)realloc(H->Arr, sizeof(int) * (H->Index + 1)); 126 | } -------------------------------------------------------------------------------- /Assignments/Week 2/허정민/AbsoluteHeap_허정민.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int *a, int *b) { 5 | int tmp; 6 | tmp = *a; 7 | *a = *b; 8 | *b = tmp; 9 | } 10 | 11 | int compare(int a, int b) { 12 | if (abs(a) < abs(b)) return a; 13 | else if (abs(a) > abs(b)) return b; 14 | else if (a < b) return a; 15 | else return b; 16 | } 17 | 18 | void UpHeap(int *arr, int idx) { 19 | while (idx / 2) { 20 | if (compare(arr[idx],arr[idx/2])==arr[idx]) swap(arr + idx, arr + idx / 2); 21 | else break; 22 | idx /= 2; 23 | } 24 | } 25 | 26 | void DownHeap(int *arr, int len, int index) { 27 | if (index * 2 > len) return; 28 | else if (index * 2 == len) { 29 | if (compare(arr[index * 2], arr[index]) == arr[index * 2]) { 30 | swap(arr + index * 2, arr + index); 31 | DownHeap(arr, len, index * 2); 32 | } 33 | } 34 | else { 35 | if (compare(arr[index * 2], arr[index * 2 + 1]) == arr[index * 2]) { 36 | if (compare(arr[index * 2], arr[index]) == arr[index * 2]) { 37 | swap(arr + index * 2, arr + index); 38 | DownHeap(arr, len, index * 2); 39 | } 40 | } 41 | else { 42 | if (compare(arr[index * 2 + 1], arr[index]) == arr[index * 2 + 1]) { 43 | swap(arr + index * 2 + 1, arr + index); 44 | DownHeap(arr, len, index * 2 + 1); 45 | } 46 | } 47 | } 48 | } 49 | 50 | int main() { 51 | int N, *arr, idx = 0, i,input; 52 | scanf("%d", &N); 53 | arr = (int*)calloc(N + 1, sizeof(int)); 54 | for (i = 1; i <= N; i++) { 55 | scanf("%d", &input); 56 | if (input == 0) { 57 | if (!idx) printf("0\n"); 58 | else { 59 | printf("%d\n", arr[1]); 60 | arr[1] = arr[idx--]; 61 | DownHeap(arr, idx, 1); 62 | } 63 | } 64 | else { 65 | arr[++idx] = input; 66 | UpHeap(arr, idx); 67 | } 68 | } 69 | free(arr); 70 | return 0; 71 | } -------------------------------------------------------------------------------- /Assignments/Week 2/허정민/BST_허정민.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct Node { 4 | struct Node *left, *right, *parent; 5 | int data; 6 | }Node; 7 | 8 | Node* getNode(int d) { 9 | Node *p = (Node*)malloc(sizeof(Node)); 10 | p->left = NULL; 11 | p->right = NULL; 12 | p->parent = NULL; 13 | p->data = d; 14 | return p; 15 | } 16 | 17 | Node* searchNode(int data, Node *leaf) { 18 | Node *p; 19 | if (leaf->data == data) return leaf; 20 | if (leaf->left) { 21 | p = searchNode(data, leaf->left); 22 | if (!p) return p; 23 | } 24 | if (leaf->right) { 25 | p = searchNode(data, leaf->right); 26 | if (!p) return p; 27 | } 28 | return NULL; 29 | } 30 | 31 | void insert(Node *root,int d) { 32 | Node *new_node, *p = root; 33 | new_node = getNode(d); 34 | while (p) { 35 | if (p->data == d) return; 36 | if (p->data > d) { 37 | if (p->left == NULL) { 38 | p->left = new_node; 39 | new_node->parent = p; 40 | break; 41 | } 42 | else p = p->left; 43 | } 44 | else { 45 | if (p->right == NULL) { 46 | p->right = new_node; 47 | new_node->parent = p; 48 | break; 49 | } 50 | else p = p->right; 51 | } 52 | } 53 | } 54 | 55 | void printTree(Node *p) { 56 | if (p == NULL) return; 57 | printTree(p->left); 58 | printTree(p->right); 59 | printf("%d ", p->data); 60 | } 61 | 62 | void freeNode(Node *p) { 63 | if (p == NULL) return; 64 | freeNode(p->left); 65 | freeNode(p->right); 66 | free(p); 67 | } 68 | 69 | int main() { 70 | int N, tmp; 71 | Node *root; 72 | //scanf("%d", &N); 73 | scanf("%d", &tmp); 74 | root = getNode(tmp); 75 | //N--; 76 | while (scanf("%d",&tmp)!=EOF) { 77 | //scanf("%d", &tmp); 78 | insert(root, tmp); 79 | } 80 | printTree(root); 81 | freeNode(root); 82 | } -------------------------------------------------------------------------------- /Assignments/Week 2/허정민/HeapSort_허정민.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int *a, int *b) { 5 | int tmp; 6 | tmp = *a; 7 | *a = *b; 8 | *b = tmp; 9 | } 10 | 11 | void UpHeap(int *arr, int idx) { 12 | while (idx / 2) { 13 | if (arr[idx] < arr[idx / 2]) swap(arr + idx, arr + idx / 2); 14 | else break; 15 | idx /= 2; 16 | } 17 | } 18 | 19 | void DownHeap(int *arr, int len, int index) { 20 | if (index * 2 > len) return; 21 | else if (index * 2 == len) { 22 | if (arr[index] > arr[index * 2]) { 23 | swap(arr + index, arr + index * 2); 24 | DownHeap(arr, len, index * 2); 25 | } 26 | } 27 | else if (arr[index]>arr[index*2]||arr[index]>arr[index*2+1]) { 28 | if (arr[index * 2] < arr[index * 2 + 1]) { 29 | swap(arr + index * 2, arr + index); 30 | DownHeap(arr, len, index * 2); 31 | } 32 | else { 33 | swap(arr + index * 2 + 1, arr + index); 34 | DownHeap(arr, len, index * 2 + 1); 35 | } 36 | } 37 | } 38 | 39 | int main() { 40 | int N, *arr, idx = 0, i; 41 | scanf("%d", &N); 42 | arr = (int*)calloc(N + 1, sizeof(int)); 43 | for (i = 1; i <= N; i++) { 44 | scanf("%d", &arr[i]); 45 | } 46 | for (i = N/2; i > 0; i--) { 47 | DownHeap(arr, N, i); 48 | } 49 | idx = N; 50 | for (i = 1; i <= N; i++) { 51 | printf("%d\n", arr[1]); 52 | arr[1] = arr[idx--]; 53 | DownHeap(arr, idx, 1); 54 | } 55 | free(arr); 56 | return 0; 57 | } -------------------------------------------------------------------------------- /Assignments/Week 3/전선영/AVL_Tree_전선영.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | typedef struct Node 7 | { 8 | struct Node *right, *left; 9 | int b_factor, height, elem; 10 | }Node; 11 | int max_num(int a, int b) 12 | { 13 | if (a > b) 14 | return a; 15 | else 16 | return b; 17 | } 18 | Node *get_node(int a) 19 | { 20 | Node *node = (Node*)malloc(sizeof(Node)); 21 | 22 | node->right = NULL; 23 | node->left = NULL; 24 | 25 | node->elem = a; 26 | node->height = 1; 27 | node->b_factor = 0; 28 | 29 | return node; 30 | } 31 | void find_height_bfactor(Node *root) 32 | { 33 | if (root->left != NULL && root->right != NULL) 34 | { 35 | root->height = max_num(root->left->height, root->right->height) + 1; 36 | root->b_factor = root->left->height - root->right->height; 37 | } 38 | else if (root->left == NULL && root->right != NULL) 39 | { 40 | root->height = root->right->height + 1; 41 | root->b_factor = -root->right->height; 42 | } 43 | else if (root->left != NULL && root->right == NULL) 44 | { 45 | root->height = root->left->height + 1; 46 | root->b_factor = root->left->height; 47 | } 48 | else 49 | { 50 | root->height = 1; 51 | root->b_factor = 0; 52 | } 53 | } 54 | Node *rotate_right(Node *node) 55 | { 56 | Node *tmp, *balanced_root; 57 | 58 | tmp = node->left->right; 59 | node->left->right = node; 60 | balanced_root = node->left; 61 | node->left = tmp; 62 | 63 | find_height_bfactor(node); 64 | find_height_bfactor(balanced_root); 65 | 66 | return balanced_root; 67 | } 68 | Node *rotate_left(Node *node) 69 | { 70 | Node *tmp, *balanced_root; 71 | 72 | tmp = node->right->left; 73 | node->right->left = node; 74 | balanced_root = node->right; 75 | node->right = tmp; 76 | 77 | find_height_bfactor(node); 78 | find_height_bfactor(balanced_root); 79 | 80 | return balanced_root; 81 | } 82 | Node *balance_node(Node *root, Node *mother) 83 | { 84 | if (abs(root->b_factor) >= 2) 85 | { 86 | if (root->b_factor > 1) 87 | { 88 | if (root->left->b_factor < 0) 89 | root->left = rotate_left(root->left); 90 | root = rotate_right(root); 91 | } 92 | else if (root->b_factor < -1) 93 | { 94 | if (root->right->b_factor > 0) 95 | root->right = rotate_right(root->right); 96 | root = rotate_left(root); 97 | } 98 | } 99 | if (mother != NULL) 100 | find_height_bfactor(mother); 101 | return root; 102 | } 103 | void insert_node(Node *root, int a) 104 | { 105 | if (a == root->elem) 106 | return; 107 | else 108 | { 109 | if (a < root->elem) 110 | { 111 | if (root->left == NULL) 112 | root->left = get_node(a); 113 | else 114 | insert_node(root->left, a); 115 | } 116 | else if (a > root->elem) 117 | { 118 | if (root->right == NULL) 119 | root->right = get_node(a); 120 | else 121 | insert_node(root->right, a); 122 | } 123 | find_height_bfactor(root); 124 | if (root->left != NULL) 125 | root->left = balance_node(root->left, root); 126 | if (root->right != NULL) 127 | root->right = balance_node(root->right, root); 128 | } 129 | } 130 | Node *search_node(Node *node, int a) 131 | { 132 | if (node == NULL) 133 | return NULL; 134 | else if (node->elem == a) 135 | return node; 136 | else if (a < node->elem) 137 | search_node(node->left, a); 138 | else if (a > node->elem) 139 | search_node(node->right, a); 140 | } 141 | Node *deleting(Node *node) 142 | { 143 | Node *tmp=NULL, *p_tmp; 144 | if (node->left == NULL && node->right == NULL) 145 | { 146 | free(node); 147 | return NULL; 148 | } 149 | else 150 | { 151 | if (node->left != NULL && node->right != NULL) 152 | { 153 | tmp = node->right; 154 | p_tmp = node; 155 | if (tmp->left != NULL) 156 | { 157 | while (tmp->left != NULL) 158 | { 159 | p_tmp = tmp; 160 | tmp = tmp->left; 161 | } 162 | p_tmp->left = tmp->right; 163 | tmp->right = node->right; 164 | } 165 | 166 | tmp->left = node->left; 167 | free(node); 168 | node = NULL; 169 | 170 | return tmp; 171 | } 172 | else if (node->left != NULL || node->right != NULL) 173 | { 174 | if (node->left == NULL) 175 | { 176 | tmp = node->right; 177 | free(node); 178 | node = NULL; 179 | return tmp; 180 | } 181 | else if (node->right == NULL) 182 | { 183 | tmp = node->left; 184 | free(node); 185 | node = NULL; 186 | return tmp; 187 | } 188 | } 189 | } 190 | } 191 | void delete_node(Node *node, int a) 192 | { 193 | if (node->left->elem == a) 194 | node->left = deleting(node->left); 195 | else if (node->right->elem == a) 196 | node->right = deleting(node->right); 197 | else 198 | { 199 | if (a < node->elem) 200 | delete_node(node->left, a); 201 | else 202 | delete_node(node->right, a); 203 | } 204 | } 205 | void rebalance(Node *root) 206 | { 207 | if (root == NULL) 208 | return; 209 | else 210 | { 211 | rebalance(root->left); 212 | rebalance(root->right); 213 | find_height_bfactor(root); 214 | if (root->left != NULL) 215 | root->left = balance_node(root->left, root); 216 | if (root->right != NULL) 217 | root->right = balance_node(root->right, root); 218 | } 219 | } 220 | void print_node(Node *node) 221 | { 222 | if (node == NULL) 223 | return; 224 | printf(" %d", node->elem); 225 | print_node(node->left); 226 | print_node(node->right); 227 | } 228 | int main() 229 | { 230 | int first = 0, a; 231 | char ch; 232 | Node *node = NULL, *tmp; 233 | 234 | while (1) 235 | { 236 | scanf("%c", &ch); 237 | 238 | if (ch == 'q') 239 | break; 240 | else if (ch == 'i') 241 | { 242 | scanf("%d", &a); 243 | if (first == 0) 244 | { 245 | node = get_node(a); 246 | ++first; 247 | } 248 | else 249 | { 250 | insert_node(node, a); 251 | find_height_bfactor(node); 252 | node = balance_node(node, NULL); 253 | } 254 | } 255 | else if (ch == 'p') 256 | { 257 | print_node(node); 258 | printf("\n"); 259 | } 260 | else if (ch == 's') 261 | { 262 | scanf("%d", &a); 263 | tmp = search_node(node, a); 264 | if (tmp == NULL) 265 | printf("X\n"); 266 | else 267 | printf("%d\n", tmp->elem); 268 | } 269 | else if (ch == 'd') 270 | { 271 | scanf("%d", &a); 272 | tmp = search_node(node, a); 273 | if (tmp == NULL) 274 | printf("X\n"); 275 | else 276 | { 277 | if (a == node->elem) 278 | node = deleting(node); 279 | else 280 | delete_node(node, a); 281 | if (node != NULL) 282 | { 283 | rebalance(node); 284 | node = balance_node(node, NULL); 285 | } 286 | else 287 | first = 0; 288 | } 289 | } 290 | getchar(); 291 | } 292 | } 293 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | project(algorithm_class C) 3 | 4 | set(CMAKE_C_STANDARD 99) 5 | 6 | add_executable(algorithm_class main.c) -------------------------------------------------------------------------------- /Lecture Notes/알고리즘 0강.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Lecture Notes/알고리즘 0강.pdf -------------------------------------------------------------------------------- /Lecture Notes/알고리즘 1강.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Lecture Notes/알고리즘 1강.pdf -------------------------------------------------------------------------------- /Lecture Notes/알고리즘 2강.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Lecture Notes/알고리즘 2강.pdf -------------------------------------------------------------------------------- /Lecture Notes/알고리즘 3강.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/therealjamesjung/AlgorithmClass/969f44f96c9b9c95c4ae1a2d1d3a94d63060a543/Lecture Notes/알고리즘 3강.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 방학 알고리즘 강의 Github Repository 2 | 3 | 4 | ## 개요 5 | 6 | - 2019/7/3 부터 진행하는 알고리즘 수업 강의자료, 샘플 코드, 과제등이 올라오는 저장소 입니다. 7 | 8 | - 각자 과제를 이 저장소에 Pull Request 를 보내서 과제를 제출할 수 있도록 합니다. 9 | 10 | - 모든 강의자료의 저작권은 제작자 (James Jung)에게 있습니다. 11 | 12 | - 강의자료의 상업적인 이용, 복제 및 수정이 불가능합니다. 13 | 14 | - 커리큘럼은 강의가 진행함에 따라 변할 수 있습니다 15 | 16 | ## 커리큘럼 17 | 18 | 0. [OT](https://github.com/jamesj0918/AlgorithmClass/blob/master/Lecture%20Notes/%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98%200%EA%B0%95.pdf) 19 | 1. [정렬 알고리즘](https://github.com/jamesj0918/AlgorithmClass/blob/master/Lecture%20Notes/%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98%201%EA%B0%95.pdf) 20 | 2. [Heap, Heap Sort, Binary Search Tree](https://github.com/jamesj0918/AlgorithmClass/blob/master/Lecture%20Notes/%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98%202%EA%B0%95.pdf) 21 | 3. [AVL Tree, Hash Table](https://github.com/jamesj0918/AlgorithmClass/blob/master/Lecture%20Notes/%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98%203%EA%B0%95.pdf) 22 | 4. 중간 Quiz 23 | 5. 그래프 이론, 순회 24 | 6. 방향 그래프, Spanning Tree 25 | 7. Kruskal 알고리즘 26 | 8. Dijkstra 알고리즘 27 | 28 | ## 과제 29 | 30 | 1. [정렬 알고리즘 수행시간 측정/분석](https://www.acmicpc.net/problem/2750) 31 | 2. [Absolute Heap](https://www.acmicpc.net/problem/11286), [Binary Search Tree](https://www.acmicpc.net/problem/5639) 32 | 3. AVL Tree 구현 33 | 34 | ## 벌금 제도 35 | 36 | 1. 지각 벌금 : 15:01 부터 분당 500원, 최대 5천원. 37 | 2. 과제 제출기한 지남 벌금: 5천원. -------------------------------------------------------------------------------- /Sample Codes/Week 1/BubbleSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int *a, int *b) 5 | { 6 | int tmp; 7 | tmp = *a; 8 | *a = *b; 9 | *b = tmp; 10 | } 11 | void bubble_sort(int *arr, int n) 12 | { 13 | int *p, *q; 14 | int i = 0; 15 | 16 | for (p = arr; p < arr + n; p++, i++) 17 | { 18 | for (q = arr; q < arr + n - 1 - i; q++) 19 | { 20 | if (*q > *(q + 1)) swap(q, (q + 1)); 21 | } 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | int *arr; 28 | int n, i; 29 | 30 | scanf("%d", &n); 31 | 32 | arr = (int *)malloc(sizeof(int)*n); 33 | 34 | for (i = 0; i < n; i++) scanf("%d", &arr[i]); 35 | bubble_sort(arr, n); 36 | 37 | for (i = 0; i < n; i++) printf(" %d", arr[i]); 38 | 39 | free(arr); 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Sample Codes/Week 1/InsertionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void insertion_sort(int *arr, int n) 5 | { 6 | int tmp; 7 | int *p, *q; 8 | 9 | for (p = arr + 1; p < arr + n; p++) 10 | { 11 | tmp = *p; 12 | for (q = p; q > arr; q--) 13 | { 14 | if (*(q - 1) < tmp) break; 15 | *q = *(q - 1); 16 | } 17 | *q = tmp; 18 | } 19 | } 20 | 21 | int main() 22 | { 23 | int *arr; 24 | int n, i; 25 | 26 | scanf("%d", &n); 27 | 28 | arr = (int *)malloc(sizeof(int)*n); 29 | 30 | for (i = 0; i < n; i++) scanf("%d", &arr[i]); 31 | insertion_sort(arr, n); 32 | 33 | for (i = 0; i < n; i++) printf(" %d", arr[i]); 34 | 35 | free(arr); 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Sample Codes/Week 1/MergeSort(Array).c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void merge(int *arr, int left, int right) 5 | { 6 | int *tmp; 7 | int n = (right - left + 1); 8 | int mid = (left + right) / 2 + 1; 9 | int i = 0, l = left, m = mid; 10 | 11 | tmp = (int *)malloc(sizeof(int) * n); 12 | 13 | while (l < mid && m <= right) 14 | { 15 | if (arr[l] > arr[m]) tmp[i++] = arr[m++]; 16 | else tmp[i++] = arr[l++]; 17 | } 18 | 19 | while (l < mid) tmp[i++] = arr[l++]; 20 | 21 | while (m <= right) tmp[i++] = arr[m++]; 22 | 23 | for (i = 0; i < n; i++) arr[left++] = tmp[i]; 24 | free(tmp); 25 | } 26 | 27 | void merge_sort(int *arr, int left, int right) 28 | { 29 | int mid = (left + right) / 2; 30 | 31 | if (right - left < 1) return; 32 | 33 | merge_sort(arr, left, mid); 34 | merge_sort(arr, mid + 1, right); 35 | 36 | merge(arr, left, right); 37 | } 38 | 39 | int main() 40 | { 41 | int *arr; 42 | int n, i; 43 | 44 | scanf("%d", &n); 45 | 46 | arr = (int *)malloc(sizeof(int)*n); 47 | 48 | for (i = 0; i < n; i++) scanf("%d", &arr[i]); 49 | merge_sort(arr, 0, n - 1); 50 | for (i = 0; i < n; i++) printf(" %d", arr[i]); 51 | 52 | free(arr); 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Sample Codes/Week 1/MergeSort(LinkedList).c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct Node 5 | { 6 | int data; 7 | struct Node *next; 8 | }Node; 9 | 10 | Node *create_node(int data) 11 | { 12 | Node *new_node = (Node *)malloc(sizeof(Node)); 13 | 14 | new_node->data = data; 15 | new_node->next = NULL; 16 | 17 | return new_node; 18 | } 19 | 20 | void divide(Node *node, int n) 21 | { 22 | int i; 23 | for (i = 0; i < n - 1; i++) node = node->next; 24 | 25 | node->next = NULL; 26 | } 27 | void merge(Node *left, Node *right) 28 | { 29 | Node *tmp = (Node *)malloc(sizeof(Node)); 30 | 31 | while (left != NULL && right != NULL) 32 | { 33 | if (left->data < right->data) 34 | { 35 | tmp->next = left; 36 | left = left->next; 37 | } 38 | else 39 | { 40 | tmp->next = right; 41 | right = right->next; 42 | } 43 | 44 | tmp = tmp->next; 45 | } 46 | 47 | while (left != NULL) 48 | { 49 | tmp->next = left; 50 | left = left->next; 51 | tmp = tmp->next; 52 | } 53 | 54 | while (right != NULL) 55 | { 56 | tmp->next = right; 57 | right = right->next; 58 | tmp = tmp->next; 59 | } 60 | } 61 | 62 | Node *merge_sort(Node *node, int cnt) 63 | { 64 | int i; 65 | Node *left, *right; 66 | if (node->next == NULL) return node; 67 | 68 | left = node; 69 | right = node; 70 | 71 | for (i = 0; i < cnt / 2; i++) right = right->next; 72 | 73 | divide(node, cnt / 2); 74 | 75 | left = merge_sort(left, cnt / 2); 76 | 77 | if (cnt % 2 == 1) right = merge_sort(right, cnt / 2 + 1); 78 | else right = merge_sort(right, cnt / 2); 79 | 80 | merge(left, right); 81 | 82 | if (left->data <= right->data) return left; 83 | else return right; 84 | } 85 | 86 | int main() 87 | { 88 | int n, i, tmp; 89 | Node *node; 90 | 91 | scanf("%d", &n); 92 | 93 | node = (Node *)malloc(sizeof(Node) * n); 94 | 95 | for (i = 0; i < n; i++) 96 | { 97 | scanf("%d", &tmp); 98 | node[i] = *create_node(tmp); 99 | } 100 | for (i = 0; i < n - 1; i++) (node + i)->next = (node + i + 1); 101 | 102 | node = merge_sort(node, n); 103 | 104 | while (node != NULL) 105 | { 106 | printf(" %d", node->data); 107 | node = node->next; 108 | } 109 | 110 | free(node); 111 | 112 | return 0; 113 | } 114 | -------------------------------------------------------------------------------- /Sample Codes/Week 1/Power.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int power(int A, int B) 4 | { 5 | int tmp; 6 | 7 | if (B == 0) return 1; 8 | 9 | tmp = power(A, B / 2); 10 | 11 | if (B % 2 == 0) return tmp * tmp; 12 | else return A * tmp * tmp; 13 | } 14 | 15 | int main() 16 | { 17 | int A, B; 18 | int answer = 1; 19 | 20 | scanf("%d", &A); 21 | scanf("%d", &B); 22 | 23 | 24 | answer = power(A, B); 25 | printf("%d", answer); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Sample Codes/Week 1/QuickSort(Array).c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int *a, int *b) 5 | { 6 | int tmp; 7 | tmp = *a; 8 | *a = *b; 9 | *b = tmp; 10 | } 11 | 12 | void quick_sort(int *arr, int left, int right) 13 | { 14 | int pivot = arr[(left + right) / 2]; 15 | int l = left, r = right; 16 | 17 | if (right - left < 1) return; 18 | 19 | while (l < r) 20 | { 21 | while (arr[l] < pivot) l++; 22 | while (arr[r] > pivot) r--; 23 | 24 | if (l <= r) swap(&arr[l++], &arr[r--]); 25 | } 26 | 27 | quick_sort(arr, left, r); 28 | quick_sort(arr, l, right); 29 | } 30 | 31 | int main() 32 | { 33 | int *arr; 34 | int n, i; 35 | 36 | scanf("%d", &n); 37 | 38 | arr = (int *)malloc(sizeof(int) * n); 39 | 40 | for (i = 0; i < n; i++) scanf("%d", &arr[i]); 41 | quick_sort(arr, 0, n - 1); 42 | for (i = 0; i < n; i++) printf(" %d", arr[i]); 43 | 44 | free(arr); 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Sample Codes/Week 1/SelectionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void selection_sort(int *arr, int n) 5 | { 6 | int *p, *q; 7 | int max, tmp, *p_max = arr; 8 | int flag; 9 | 10 | for (p = arr + n - 1; p >= arr; p--) 11 | { 12 | max = *p; 13 | flag = 0; 14 | for (q = p; q >= arr; q--) 15 | { 16 | if (*q > max) 17 | { 18 | max = *q; 19 | p_max = q; 20 | flag = 1; 21 | } 22 | } 23 | if (flag == 1) 24 | { 25 | tmp = *p; 26 | *p = *p_max; 27 | *p_max = tmp; 28 | } 29 | } 30 | 31 | } 32 | 33 | int main() 34 | { 35 | int *arr; 36 | int n, i; 37 | 38 | scanf("%d", &n); 39 | 40 | arr = (int *)malloc(sizeof(int)*n); 41 | 42 | for (i = 0; i < n; i++) scanf("%d", &arr[i]); 43 | 44 | selection_sort(arr, n); 45 | 46 | for (i = 0; i < n; i++) printf(" %d", arr[i]); 47 | 48 | free(arr); 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Sample Codes/Week 2/AbsoluteHeap.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int *a, int *b) 5 | { 6 | int tmp; 7 | tmp = *a; 8 | *a = *b; 9 | *b = tmp; 10 | } 11 | 12 | int compare_abs(int n1, int n2) 13 | { 14 | if (abs(n1) < abs(n2)) return n1; 15 | else if (abs(n1) > abs(n2)) return n2; 16 | else return n1 < n2 ? n1 : n2; 17 | } 18 | 19 | void UpHeap(int *heap, int index) 20 | { 21 | if (index == 1 || compare_abs(heap[index], heap[index / 2]) == heap[index / 2]) return; // Current node is bigger than parent 22 | else 23 | { 24 | swap(&heap[index], &heap[index / 2]); 25 | UpHeap(heap, index / 2); 26 | } 27 | } 28 | 29 | void DownHeap(int *heap, int current_index, int last_index) 30 | { 31 | if (current_index * 2 > last_index) return; // Leaf node 32 | 33 | else if (current_index * 2 < last_index) // Has both child nodes 34 | { 35 | if (compare_abs(heap[current_index * 2], heap[current_index * 2 + 1]) == heap[current_index * 2]) // Left child is smaller 36 | { 37 | if (compare_abs(heap[current_index], heap[current_index * 2]) == heap[current_index * 2]) 38 | { 39 | swap(&heap[current_index], &heap[current_index * 2]); 40 | DownHeap(heap, current_index * 2, last_index); 41 | } 42 | else return; 43 | } 44 | 45 | else // Right child is smaller 46 | { 47 | if (compare_abs(heap[current_index], heap[current_index * 2 + 1]) == heap[current_index * 2 + 1]) 48 | { 49 | swap(&heap[current_index], &heap[current_index * 2 + 1]); 50 | DownHeap(heap, current_index * 2 + 1, last_index); 51 | } 52 | else return; 53 | } 54 | } 55 | 56 | else // Has only left child 57 | { 58 | if (compare_abs(heap[current_index], heap[current_index * 2]) == heap[current_index * 2]) 59 | { 60 | swap(&heap[current_index], &heap[current_index * 2]); 61 | DownHeap(heap, current_index * 2, last_index); 62 | } 63 | else return; 64 | } 65 | } 66 | 67 | int main(){ 68 | int *heap; 69 | int n, i, num; 70 | int index = 1; 71 | 72 | scanf("%d", &n); 73 | 74 | heap = (int *)malloc(sizeof(int) * (n + 1)); 75 | 76 | for (i = 1; i <= n; i++) 77 | { 78 | scanf("%d", &num); 79 | 80 | if (num == 0) 81 | { 82 | if (index == 1) 83 | { 84 | printf("0\n"); 85 | continue; 86 | } 87 | printf("%d\n", heap[1]); 88 | heap[1] = heap[--index]; 89 | DownHeap(heap, 1, index); 90 | } 91 | else 92 | { 93 | heap[index] = num; 94 | UpHeap(heap, index++); 95 | } 96 | } 97 | 98 | return 0; 99 | } -------------------------------------------------------------------------------- /Sample Codes/Week 2/BST.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct Node 5 | { 6 | int data; 7 | struct Node *left_child, *right_child, *parent; 8 | 9 | }Node; 10 | 11 | Node *create_node(int data) 12 | { 13 | Node *new_node = (Node *)malloc(sizeof(Node)); 14 | 15 | new_node->data = data; 16 | new_node->left_child = NULL; 17 | new_node->right_child = NULL; 18 | new_node->parent = NULL; 19 | 20 | return new_node; 21 | } 22 | 23 | Node *find_node(Node *root, int data) 24 | { 25 | if (root == NULL) return NULL; 26 | 27 | else if (root->data == data) return root; 28 | else if (root->data > data) return find_node(root->left_child, data); 29 | else return find_node(root->right_child, data); 30 | } 31 | 32 | void insert(Node *root, Node *node) 33 | { 34 | if (root->left_child == NULL && root->right_child == NULL) // Leaf node 35 | { 36 | if (root->data > node->data) root->left_child = node; 37 | else root->right_child = node; 38 | 39 | node->parent = root; 40 | } 41 | else if (root->left_child == NULL) // Only has right_child 42 | { 43 | if (root->data > node->data) 44 | { 45 | root->left_child = node; 46 | node->parent = root; 47 | } 48 | else insert(root->right_child, node); 49 | } 50 | else if (root ->right_child == NULL) // Only has left_child 51 | { 52 | if (root->data > node->data) insert(root->left_child, node); 53 | else 54 | { 55 | root->right_child = node; 56 | node->parent = root; 57 | } 58 | } 59 | else // Has both child nodes 60 | { 61 | if (root->data > node->data) insert(root->left_child, node); 62 | else insert(root->right_child, node); 63 | } 64 | } 65 | 66 | Node *delete(Node *root, Node *node) 67 | { 68 | Node *successor_node; 69 | if (node == NULL) 70 | { 71 | printf("Node with data was not found."); 72 | return root; 73 | } 74 | 75 | if (node->left_child == NULL && node ->right_child == NULL) // Node is leaf node 76 | { 77 | if (node->parent == NULL) // Attempt to delete the root node 78 | { 79 | free(root); 80 | root = NULL; 81 | return root; 82 | } 83 | else if (node->parent->left_child != NULL && node->parent->left_child->data == node->data) // Node is left child of parent 84 | { 85 | node->parent->left_child = NULL; 86 | } 87 | else if (node->parent->right_child != NULL && node->parent->right_child->data == node->data) // Node is right child of parent 88 | { 89 | node->parent->right_child = NULL; 90 | } 91 | free(node); 92 | node = NULL; 93 | return root; 94 | } 95 | 96 | else if (node->right_child == NULL) // Node only has left child node 97 | { 98 | node->left_child->parent = node->parent; 99 | if (node->parent == NULL) // Attempt to delete the root node 100 | { 101 | free(root); 102 | root = root->left_child; 103 | return root; 104 | } 105 | if (node->parent->left_child != NULL && node->parent->left_child->data == node->data) // Node is left child of parent 106 | { 107 | node->parent->left_child = node->left_child; 108 | } 109 | else if (node->parent->right_child != NULL && node->parent->right_child->data == node->data) // Node is right child of parent 110 | { 111 | node->parent->right_child = node->left_child; 112 | } 113 | free(node); 114 | node = NULL; 115 | return root; 116 | } 117 | 118 | else if (node->left_child == NULL) // Root only has right child node 119 | { 120 | node->right_child->parent = node->parent; 121 | if (node->parent == NULL) // Attempt to delete the root node 122 | { 123 | free(root); 124 | root = root->right_child; 125 | return root; 126 | } 127 | if (node->parent->left_child != NULL && node->parent->left_child->data == node->data) // Root is left child of parent 128 | { 129 | node->parent->left_child = node->right_child; 130 | } 131 | else if (node->parent->right_child != NULL && node->parent->right_child->data == node->data) // Root is right child of parent 132 | { 133 | node->parent->right_child = node->right_child; 134 | } 135 | free(node); 136 | node = NULL; 137 | return root; 138 | } 139 | 140 | else // Root has both child nodes 141 | { 142 | successor_node = node->right_child; 143 | while(successor_node->left_child != NULL) successor_node = successor_node->left_child; // Find successor node 144 | 145 | node->data = successor_node->data; // Swap root with successor node 146 | 147 | if (successor_node == node->right_child) // Successor node is root's right child 148 | { 149 | node->right_child = successor_node->right_child; 150 | if (successor_node->right_child != NULL)successor_node->right_child->parent = root; 151 | } 152 | else successor_node->parent->left_child = NULL; 153 | 154 | free(successor_node); 155 | successor_node = NULL; 156 | return root; 157 | } 158 | } 159 | 160 | void print_preorder(Node *root) 161 | { 162 | if (root == NULL) return; 163 | 164 | printf("%d ", root->data); 165 | print_preorder(root->left_child); 166 | print_preorder(root->right_child); 167 | 168 | } 169 | 170 | int main() 171 | { 172 | int n; 173 | char c; 174 | Node *root = NULL; 175 | 176 | while (1) 177 | { 178 | scanf("%c", &c); 179 | 180 | if (c == 'q') break; 181 | 182 | else if (c == 'i') 183 | { 184 | scanf("%d", &n); 185 | if (root == NULL) root = create_node(n); 186 | else insert(root, create_node(n)); 187 | getchar(); 188 | } 189 | else if (c == 'p') 190 | { 191 | print_preorder(root); 192 | printf("\n"); 193 | } 194 | else if (c == 'd') 195 | { 196 | scanf("%d", &n); 197 | root = delete(root, find_node(root, n)); 198 | getchar(); 199 | } 200 | } 201 | 202 | return 0; 203 | } -------------------------------------------------------------------------------- /Sample Codes/Week 2/BST_Baekjoon.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct Node 5 | { 6 | int data; 7 | struct Node *left_child, *right_child; 8 | }Node; 9 | 10 | Node *create_node(int data) 11 | { 12 | Node *new_node = (Node *)malloc(sizeof(Node)); 13 | 14 | new_node->data = data; 15 | new_node->left_child = NULL; 16 | new_node->right_child = NULL; 17 | 18 | return new_node; 19 | } 20 | 21 | Node *find_node(Node *root, int data) 22 | { 23 | if (root == NULL) return NULL; 24 | 25 | else if (root->data == data) return root; 26 | else if (root->data > data) return find_node(root->left_child, data); 27 | else return find_node(root->right_child, data); 28 | } 29 | 30 | void insert(Node *root, Node *node) 31 | { 32 | if (root->left_child == NULL && root->right_child == NULL) // Leaf node 33 | { 34 | if (root->data > node->data) root->left_child = node; 35 | else root->right_child = node; 36 | } 37 | else if (root->left_child == NULL) // Only has right_child 38 | { 39 | if (root->data > node->data) root->left_child = node; 40 | else insert(root->right_child, node); 41 | } 42 | else if (root ->right_child == NULL) // Only has left_child 43 | { 44 | if (root->data > node->data) insert(root->left_child, node); 45 | else root->right_child = node; 46 | } 47 | else // Has both child nodes 48 | { 49 | if (root->data > node->data) insert(root->left_child, node); 50 | else insert(root->right_child, node); 51 | } 52 | } 53 | 54 | void print_postorder(Node *root) 55 | { 56 | if (root == NULL) return; 57 | 58 | print_postorder(root->left_child); 59 | print_postorder(root->right_child); 60 | printf("%d\n", root->data); 61 | } 62 | 63 | int main() 64 | { 65 | int n; 66 | Node *root = NULL; 67 | 68 | 69 | while (scanf("%d", &n) != EOF) 70 | { 71 | if (root == NULL) root = create_node(n); 72 | else insert(root, create_node(n)); 73 | } 74 | print_postorder(root); 75 | 76 | return 0; 77 | } -------------------------------------------------------------------------------- /Sample Codes/Week 2/HeapSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int *a, int *b) 5 | { 6 | int tmp; 7 | tmp = *a; 8 | *a = *b; 9 | *b = tmp; 10 | } 11 | 12 | 13 | void DownHeap(int *heap, int current_index, int last_index) 14 | { 15 | if (current_index * 2 > last_index) return; // Leaf node 16 | 17 | else if (current_index * 2 < last_index) // Has both child nodes 18 | { 19 | if (heap[current_index * 2] < heap[current_index * 2 + 1]) // Left child is smaller 20 | { 21 | if (heap[current_index] > heap[current_index * 2]) 22 | { 23 | swap(&heap[current_index], &heap[current_index * 2]); 24 | DownHeap(heap, current_index * 2, last_index); 25 | } 26 | else return; 27 | } 28 | else // Right child is smaller 29 | { 30 | if (heap[current_index] > heap[current_index * 2 + 1]) 31 | { 32 | swap(&heap[current_index], &heap[current_index * 2 + 1]); 33 | DownHeap(heap, current_index * 2 + 1, last_index); 34 | } 35 | else return; 36 | } 37 | } 38 | 39 | else // Has only left child 40 | { 41 | if (heap[current_index] > heap[current_index * 2]) 42 | { 43 | swap(&heap[current_index], &heap[current_index * 2]); 44 | DownHeap(heap, current_index * 2, last_index); 45 | } 46 | else return; 47 | } 48 | } 49 | 50 | int main(){ 51 | int *heap; 52 | int n, i, index; 53 | 54 | scanf("%d", &n); 55 | 56 | heap = (int *)malloc(sizeof(int) * (n + 1)); 57 | 58 | for (i = 1; i <= n; i++) scanf("%d", &heap[i]); 59 | 60 | for (i = n / 2; i >= 1; i--) DownHeap(heap, i, n); 61 | 62 | index = n; 63 | for (i = 1; i <= n; i++) 64 | { 65 | printf("%d\n", heap[1]); 66 | heap[1] = heap[index--]; 67 | DownHeap(heap, 1, index); 68 | } 69 | 70 | return 0; 71 | } -------------------------------------------------------------------------------- /Sample Codes/Week 2/MaxHeap.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int *a, int *b) 5 | { 6 | int tmp; 7 | tmp = *a; 8 | *a = *b; 9 | *b = tmp; 10 | } 11 | 12 | void UpHeap(int *heap, int index) 13 | { 14 | if (index == 1 || (heap[index / 2] > heap[index])) return; 15 | else 16 | { 17 | swap(&heap[index], &heap[index / 2]); 18 | UpHeap(heap, index / 2); 19 | } 20 | } 21 | 22 | void DownHeap(int *heap, int current_index, int last_index) 23 | { 24 | if (current_index * 2 > last_index) return; // Leaf node 25 | 26 | else if (current_index * 2 < last_index) // Has both child nodes 27 | { 28 | if (heap[current_index * 2] > heap[current_index * 2 + 1]) // Left child is bigger 29 | { 30 | if (heap[current_index] < heap[current_index * 2]) 31 | { 32 | swap(&heap[current_index], &heap[current_index * 2]); 33 | DownHeap(heap, current_index * 2, last_index); 34 | } 35 | else return; 36 | } 37 | else // Right child is bigger 38 | { 39 | if (heap[current_index] < heap[current_index * 2 + 1]) 40 | { 41 | swap(&heap[current_index], &heap[current_index * 2 + 1]); 42 | DownHeap(heap, current_index * 2 + 1, last_index); 43 | } 44 | else return; 45 | } 46 | } 47 | 48 | else // Has only left child 49 | { 50 | if (heap[current_index] < heap[current_index * 2]) 51 | { 52 | swap(&heap[current_index], &heap[current_index * 2]); 53 | DownHeap(heap, current_index * 2, last_index); 54 | } 55 | else return; 56 | } 57 | } 58 | 59 | int main(){ 60 | int *heap; 61 | int n, i, num; 62 | int index = 1; 63 | 64 | scanf("%d", &n); 65 | 66 | heap = (int *)malloc(sizeof(int) * (n + 1)); 67 | 68 | for (i = 1; i <= n; i++) 69 | { 70 | scanf("%d", &num); 71 | 72 | if (num == 0) 73 | { 74 | if (index == 1) 75 | { 76 | printf("0\n"); 77 | continue; 78 | } 79 | printf("%d\n", heap[1]); 80 | heap[1] = heap[--index]; 81 | DownHeap(heap, 1, index); 82 | } 83 | else 84 | { 85 | heap[index] = num; 86 | UpHeap(heap, index++); 87 | } 88 | } 89 | 90 | return 0; 91 | } -------------------------------------------------------------------------------- /Sample Codes/Week 2/MinHeap.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int *a, int *b) 5 | { 6 | int tmp; 7 | tmp = *a; 8 | *a = *b; 9 | *b = tmp; 10 | } 11 | 12 | void UpHeap(int *heap, int index) 13 | { 14 | if (index == 1 || (heap[index / 2] < heap[index])) return; 15 | else 16 | { 17 | swap(&heap[index], &heap[index / 2]); 18 | UpHeap(heap, index / 2); 19 | } 20 | } 21 | 22 | void DownHeap(int *heap, int current_index, int last_index) 23 | { 24 | if (current_index * 2 > last_index) return; // Leaf node 25 | 26 | else if (current_index * 2 < last_index) // Has both child nodes 27 | { 28 | if (heap[current_index * 2] < heap[current_index * 2 + 1]) // Left child is smaller 29 | { 30 | if (heap[current_index] > heap[current_index * 2]) 31 | { 32 | swap(&heap[current_index], &heap[current_index * 2]); 33 | DownHeap(heap, current_index * 2, last_index); 34 | } 35 | else return; 36 | } 37 | else // Right child is smaller 38 | { 39 | if (heap[current_index] > heap[current_index * 2 + 1]) 40 | { 41 | swap(&heap[current_index], &heap[current_index * 2 + 1]); 42 | DownHeap(heap, current_index * 2 + 1, last_index); 43 | } 44 | else return; 45 | } 46 | } 47 | 48 | else // Has only left child 49 | { 50 | if (heap[current_index] > heap[current_index * 2]) 51 | { 52 | swap(&heap[current_index], &heap[current_index * 2]); 53 | DownHeap(heap, current_index * 2, last_index); 54 | } 55 | else return; 56 | } 57 | } 58 | 59 | int main(){ 60 | int *heap; 61 | int n, i, num; 62 | int index = 1; 63 | 64 | scanf("%d", &n); 65 | 66 | heap = (int *)malloc(sizeof(int) * (n + 1)); 67 | 68 | for (i = 1; i <= n; i++) 69 | { 70 | scanf("%d", &num); 71 | 72 | if (num == 0) 73 | { 74 | if (index == 1) 75 | { 76 | printf("0\n"); 77 | continue; 78 | } 79 | printf("%d\n", heap[1]); 80 | heap[1] = heap[--index]; 81 | DownHeap(heap, 1, index); 82 | } 83 | else 84 | { 85 | heap[index] = num; 86 | UpHeap(heap, index++); 87 | } 88 | } 89 | 90 | return 0; 91 | } -------------------------------------------------------------------------------- /Sample Codes/Week 3/AVLTree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct Node 5 | { 6 | int data, height, balance_factor; 7 | struct Node *left_child, *right_child, *parent; 8 | 9 | }Node; 10 | 11 | Node *create_node(int data) 12 | { 13 | Node *new_node = (Node *)malloc(sizeof(Node)); 14 | 15 | new_node->data = data; 16 | new_node->height = 1; 17 | new_node->balance_factor = 0; 18 | new_node->left_child = NULL; 19 | new_node->right_child = NULL; 20 | new_node->parent = NULL; 21 | 22 | return new_node; 23 | } 24 | 25 | Node *find_node(Node *root, int data) 26 | { 27 | if (root == NULL) return NULL; 28 | 29 | else if (root->data == data) return root; 30 | else if (root->data > data) return find_node(root->left_child, data); 31 | else return find_node(root->right_child, data); 32 | } 33 | 34 | int compare_max(int a, int b) 35 | { 36 | return (a > b) ? a : b; 37 | } 38 | 39 | int height(Node *node) 40 | { 41 | if (node == NULL) return 0; 42 | else return node->height; 43 | } 44 | 45 | void update_height(Node *node) 46 | { 47 | if (node == NULL) return; 48 | 49 | node->height = compare_max(height(node->left_child), height(node->right_child)) + 1; 50 | update_height(node->parent); 51 | } 52 | 53 | void update_balance_factor(Node *node) 54 | { 55 | if (node == NULL) return; 56 | 57 | node->balance_factor = height(node->left_child) - height(node->right_child); 58 | update_balance_factor(node->parent); 59 | } 60 | 61 | void *rotate_right(Node *target_node) 62 | { 63 | Node *new_root = target_node->left_child; 64 | 65 | 66 | if (target_node->parent != NULL) 67 | { 68 | if (target_node->parent->left_child != NULL && target_node->parent->left_child->data == target_node->data) 69 | { 70 | target_node->parent->left_child = new_root; 71 | } 72 | else if (target_node->parent->right_child != NULL && target_node->parent->right_child->data == target_node->data) 73 | { 74 | target_node->parent->right_child = new_root; 75 | } 76 | } 77 | new_root->parent = target_node->parent; 78 | 79 | target_node->left_child = new_root->right_child; 80 | if (new_root->right_child != NULL) new_root->right_child->parent = target_node; 81 | 82 | new_root->right_child = target_node; 83 | target_node->parent = new_root; 84 | 85 | update_height(target_node); 86 | update_balance_factor(target_node); 87 | } 88 | 89 | void *rotate_left(Node *target_node) 90 | { 91 | Node *new_root = target_node->right_child; 92 | 93 | 94 | if (target_node->parent != NULL) 95 | { 96 | if (target_node->parent->left_child != NULL && target_node->parent->left_child->data == target_node->data) 97 | { 98 | target_node->parent->left_child = new_root; 99 | } 100 | else if (target_node->parent->right_child != NULL && target_node->parent->right_child->data == target_node->data) 101 | { 102 | target_node->parent->right_child = new_root; 103 | } 104 | } 105 | new_root->parent = target_node->parent; 106 | 107 | target_node->right_child = new_root->left_child; 108 | if (new_root->left_child != NULL) new_root->left_child->parent = target_node; 109 | 110 | new_root->left_child = target_node; 111 | target_node->parent = new_root; 112 | 113 | update_height(target_node); 114 | update_balance_factor(target_node); 115 | } 116 | 117 | void insert(Node *root, Node *node) 118 | { 119 | if (root->left_child == NULL && root->right_child == NULL) // Leaf node 120 | { 121 | if (root->data > node->data) root->left_child = node; 122 | else root->right_child = node; 123 | 124 | node->parent = root; 125 | } 126 | else if (root->left_child == NULL) // Only has right_child 127 | { 128 | if (root->data > node->data) 129 | { 130 | root->left_child = node; 131 | node->parent = root; 132 | } 133 | else insert(root->right_child, node); 134 | } 135 | else if (root ->right_child == NULL) // Only has left_child 136 | { 137 | if (root->data > node->data) insert(root->left_child, node); 138 | else 139 | { 140 | root->right_child = node; 141 | node->parent = root; 142 | } 143 | } 144 | else // Has both child nodes 145 | { 146 | if (root->data > node->data) insert(root->left_child, node); 147 | else insert(root->right_child, node); 148 | } 149 | } 150 | 151 | Node *rebalance(Node *node) 152 | { 153 | if (node == NULL) return node; 154 | 155 | update_height(node); 156 | update_balance_factor(node); 157 | 158 | while (1) 159 | { 160 | if (abs(node->balance_factor) >= 2) 161 | { 162 | if (node->balance_factor > 1) // Left subtree's height is greater than right subtree 163 | { 164 | if (node->left_child->balance_factor < 0) rotate_left(node->left_child); 165 | rotate_right(node); 166 | } 167 | else if (node->balance_factor < -1) // Right subtree's height is greater than left subtree 168 | { 169 | if (node->right_child->balance_factor > 0) rotate_right(node->right_child); 170 | rotate_left(node); 171 | } 172 | } 173 | 174 | if (node->parent == NULL) break; 175 | node = node->parent; 176 | } 177 | return node; 178 | } 179 | 180 | Node *delete(Node *root, Node *node) 181 | { 182 | Node *successor_node; 183 | if (node == NULL) 184 | { 185 | printf("X\n"); 186 | return root; 187 | } 188 | 189 | if (node->left_child == NULL && node ->right_child == NULL) // Node is leaf node 190 | { 191 | if (node->parent == NULL) // Attempt to delete the root node 192 | { 193 | free(root); 194 | root = NULL; 195 | return root; 196 | } 197 | else if (node->parent->left_child != NULL && node->parent->left_child->data == node->data) // Node is left child of parent 198 | { 199 | node->parent->left_child = NULL; 200 | } 201 | else if (node->parent->right_child != NULL && node->parent->right_child->data == node->data) // Node is right child of parent 202 | { 203 | node->parent->right_child = NULL; 204 | } 205 | free(node); 206 | node = NULL; 207 | return root; 208 | } 209 | 210 | else if (node->right_child == NULL) // Node only has left child node 211 | { 212 | node->left_child->parent = node->parent; 213 | if (node->parent == NULL) // Attempt to delete the root node 214 | { 215 | free(root); 216 | root = root->left_child; 217 | return root; 218 | } 219 | if (node->parent->left_child != NULL && node->parent->left_child->data == node->data) // Node is left child of parent 220 | { 221 | node->parent->left_child = node->left_child; 222 | } 223 | else if (node->parent->right_child != NULL && node->parent->right_child->data == node->data) // Node is right child of parent 224 | { 225 | node->parent->right_child = node->left_child; 226 | } 227 | free(node); 228 | node = NULL; 229 | return root; 230 | } 231 | 232 | else if (node->left_child == NULL) // Root only has right child node 233 | { 234 | node->right_child->parent = node->parent; 235 | if (node->parent == NULL) // Attempt to delete the root node 236 | { 237 | free(root); 238 | root = root->right_child; 239 | return root; 240 | } 241 | if (node->parent->left_child != NULL && node->parent->left_child->data == node->data) // Root is left child of parent 242 | { 243 | node->parent->left_child = node->right_child; 244 | } 245 | else if (node->parent->right_child != NULL && node->parent->right_child->data == node->data) // Root is right child of parent 246 | { 247 | node->parent->right_child = node->right_child; 248 | } 249 | free(node); 250 | node = NULL; 251 | return root; 252 | } 253 | 254 | else // Root has both child nodes 255 | { 256 | successor_node = node->right_child; 257 | while(successor_node->left_child != NULL) successor_node = successor_node->left_child; // Find successor node 258 | 259 | node->data = successor_node->data; // Swap root with successor node 260 | 261 | if (successor_node == node->right_child) // Successor node is root's right child 262 | { 263 | node->right_child = successor_node->right_child; 264 | if (successor_node->right_child != NULL)successor_node->right_child->parent = root; 265 | } 266 | else successor_node->parent->left_child = NULL; 267 | 268 | free(successor_node); 269 | successor_node = NULL; 270 | return root; 271 | } 272 | } 273 | 274 | Node *find_delete_position(Node *node, int data) 275 | { 276 | if (node->left_child == NULL && node->right_child == NULL) return node; 277 | 278 | else if (node->data > data) 279 | { 280 | if (node->left_child == NULL) return node; 281 | else return find_delete_position(node->left_child, data); 282 | } 283 | else 284 | { 285 | if (node->right_child == NULL) return node; 286 | return find_delete_position(node->right_child, data); 287 | } 288 | } 289 | 290 | void print_preorder(Node *root) 291 | { 292 | if (root == NULL) return; 293 | 294 | printf(" %d", root->data); 295 | print_preorder(root->left_child); 296 | print_preorder(root->right_child); 297 | } 298 | 299 | Node *insert_into_AVL(Node *root, Node *node) 300 | { 301 | insert(root, node); 302 | return rebalance(node); 303 | } 304 | 305 | Node *delete_from_AVL(Node *root, int n) 306 | { 307 | Node *delete_node = find_node(root, n); 308 | if (delete_node == NULL) return root; 309 | root = delete(root, delete_node); 310 | if (root == NULL) return root; 311 | return rebalance(find_delete_position(root, n)); 312 | } 313 | 314 | int main() 315 | { 316 | int n; 317 | char c; 318 | Node *root = NULL, *tmp = NULL; 319 | 320 | while (1) 321 | { 322 | scanf("%c", &c); 323 | 324 | if (c == 'q') break; 325 | 326 | else if (c == 'i') 327 | { 328 | scanf("%d", &n); 329 | if (root == NULL) root = create_node(n); 330 | else root = insert_into_AVL(root, create_node(n)); 331 | getchar(); 332 | } 333 | else if (c == 's') 334 | { 335 | scanf("%d", &n); 336 | tmp = find_node(root, n); 337 | if (tmp != NULL) printf("%d\n", tmp->data); 338 | else printf("X\n"); 339 | } 340 | else if (c == 'p') 341 | { 342 | print_preorder(root); 343 | printf("\n"); 344 | } 345 | else if (c == 'd') 346 | { 347 | scanf("%d", &n); 348 | root = delete_from_AVL(root, n); 349 | getchar(); 350 | } 351 | } 352 | 353 | return 0; 354 | } -------------------------------------------------------------------------------- /Sample Codes/Week 4/DuplicateKeysMergeSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void merge(int arr[8][2], int left, int right) 5 | { 6 | int tmp[8][2]; 7 | int n = (right - left + 1); 8 | int mid = (left + right) / 2 + 1; 9 | int i = 0, l = left, m = mid; 10 | 11 | while (l < mid && m <= right) 12 | { 13 | if (arr[l][0] > arr[m][0]) 14 | { 15 | tmp[i][0] = arr[m][0]; 16 | tmp[i++][1] = arr[m++][1]; 17 | } 18 | else 19 | { 20 | tmp[i][0] = arr[l][0]; 21 | tmp[i++][1] = arr[l++][1]; 22 | } 23 | } 24 | 25 | while (l < mid) 26 | { 27 | tmp[i][0] = arr[l][0]; 28 | tmp[i++][1] = arr[l++][1]; 29 | } 30 | 31 | while (m <= right) 32 | { 33 | tmp[i][0]= arr[m][0]; 34 | tmp[i++][1] = arr[m++][1]; 35 | } 36 | 37 | for (i = 0; i < n; i++) 38 | { 39 | arr[left][0] = tmp[i][0]; 40 | arr[left++][1] = tmp[i][1]; 41 | } 42 | } 43 | 44 | void merge_sort(int arr[8][2], int left, int right) 45 | { 46 | int mid = (left + right) / 2; 47 | if (right - left < 1) return; 48 | 49 | merge_sort(arr, left, mid); 50 | merge_sort(arr, mid + 1, right); 51 | 52 | merge(arr, left, right); 53 | } 54 | 55 | int main() 56 | { 57 | int arr[8][2] = {{2, 1}, {2, 2}, {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8}}; 58 | int i; 59 | 60 | merge_sort(arr, 0, 7); 61 | for (i = 0; i < 8; i++) printf(" %d %d\n", arr[i][0], arr[i][1]); 62 | 63 | return 0; 64 | } -------------------------------------------------------------------------------- /Sample Codes/Week 4/DuplicateKeysQuickSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int *a, int *b) 5 | { 6 | int tmp; 7 | tmp = *a; 8 | *a = *b; 9 | *b = tmp; 10 | } 11 | 12 | void quick_sort(int arr[8][2], int left, int right) 13 | { 14 | int pivot = arr[(left + right) / 2][0]; 15 | int l = left, r = right; 16 | 17 | if (right - left < 1) return; 18 | 19 | while (l < r) 20 | { 21 | while (arr[l][0] < pivot) l++; 22 | while (arr[r][0] > pivot) r--; 23 | 24 | if (l <= r) 25 | { 26 | swap(&arr[l][0], &arr[r][0]); 27 | swap(&arr[l++][1], &arr[r--][1]); 28 | } 29 | } 30 | 31 | quick_sort(arr, left, r); 32 | quick_sort(arr, l, right); 33 | } 34 | 35 | int main() 36 | { 37 | int arr[8][2] = {{2, 1}, {2, 2}, {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8}}; 38 | int i; 39 | 40 | printf("Before Quick Sort:\n"); 41 | for (i = 0; i < 8; i++) printf(" %d %d\n", arr[i][0], arr[i][1]); 42 | printf("\n"); 43 | quick_sort(arr, 0, 7); 44 | printf("After Quick Sort:\n"); 45 | for (i = 0; i < 8; i++) printf(" %d %d\n", arr[i][0], arr[i][1]); 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Sample Codes/Week 4/PaperFold.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int result[2] = {0}; 5 | 6 | int check(int **grid, int x, int y, int length) 7 | { 8 | int i, j; 9 | int flag = grid[x][y]; 10 | 11 | for (i = x; i < x + length; i++) for (j = y; j < y + length; j++) if (flag != grid[i][j]) return -1; 12 | 13 | return flag; 14 | } 15 | 16 | void divide(int **grid, int x, int y, int length, int depth) 17 | { 18 | int flag; 19 | if (length == 0) return; 20 | flag = check(grid, x, y, length); 21 | 22 | if (flag == -1) 23 | { 24 | divide(grid, x, y, length/2, depth); 25 | divide(grid, x + length/2, y, length/2, depth); 26 | divide(grid, x, y + length/2, length/2, depth); 27 | divide(grid, x + length/2, y + length/2, length/2, depth); 28 | } 29 | else if (flag == 0) result[0]++; 30 | else if (flag == 1) result[1]++; 31 | } 32 | 33 | int main() 34 | { 35 | int **grid; 36 | int n, i, j; 37 | 38 | scanf("%d", &n); 39 | 40 | grid = (int **)malloc(sizeof(int *) * n); 41 | for (i = 0; i < n; i++) grid[i] = (int *)malloc(sizeof(int) * n); 42 | 43 | for (i = 0; i < n; i++) for (j = 0; j < n; j++) scanf("%d",&grid[i][j]); 44 | 45 | divide(grid, 0, 0, n, 0); 46 | 47 | printf("%d\n%d", result[0], result[1]); 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | printf("Hello, World!"); 4 | return 0; 5 | } --------------------------------------------------------------------------------