π Selection Sort
μ ν μ λ ¬μ λ°μ΄ν° μ€ κ°μ₯ μμ κ°μ λ°μ΄ν°λ₯Ό μ ννμ¬ μμΌλ‘ 보λ΄λ μ λ ¬μ΄λ€.
Selection Sort is a sort that selects the data with the smallest value and sends it forward.
Time complexity
$ = N + (N-1) + (n-2) + ... + 2 + 1 $
$ = \frac{N(N+1)}{2}\ $
$ = N * N $
$ = O(N^2) $
Best : $N(O^2)$
Average : $N(O^2)$
Worst : $N(O^2)$
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(arr[min_idx], arr[i]);
}
}
π Insertion Sort
μ½μ μ λ ¬μ λ°μ΄ν°λ₯Ό μμλλ‘ λ½μμ μ μ ν μμΉλ₯Ό μ°Ύμ μ½μ ν¨μΌλ‘μ¨ μμ±νλ μ λ ¬μ΄λ€.
Insertion sort is a sort that is completed by extracting data in order and inserting it at an appropriate location.
μ½κ² μ€λͺ νμλ©΄ μλ£ λ°°μ΄μ λͺ¨λ μμλ₯Ό μμμλΆν° μ°¨λ‘λλ‘ μ΄λ―Έ μ λ ¬λ λ°°μ΄ λΆλΆκ³Ό λΉκ΅νμ¬ μμ μ μμΉλ₯Ό μ°Ύμ μ½μ νμ¬ μ λ ¬μ μμ±νλ€.
Simply, sorting is completed by comparing all elements of the data array with the already sorted part of the array, finding their positions, and inserting them.
Time complexity
κ° μμμ λν΄ μμ μ μ΄μ μ λͺ¨λ μμμ λΉκ΅ν΄μΌ νλ€.
Each element must be compared to all elements before it.
$ = 1 + 2 + ... (N-1) $
$ = \frac{(N-1)N}{2}\ $
$ = N * N $
$ = O(N^2) $
Best(Already Sorted) : $O(N)$
Average : $O(N^2)$
Worst : $O(N^2)$
#include <iostream>
using namespace std;
void InsertionSort(int arr[], int n){
for(int i=1; i<n; i++){
int target = arr[i];
int j = i-1;
while(j >= 0 && target < arr[j]){
arr[j+1] = arr[j];
j -= 1;
}
arr[j+1] = target;
}
}
π Bubble Sort
λ²λΈ μ λ ¬μ μΈμ ν μμλ₯Ό λ°λ³΅μ μΌλ‘ λΉκ΅νκ³ νμν κ²½μ° μ€μνμ¬ μμ±νλ μ λ ¬μ΄λ€.
Buble sort is a sort that is completed by repeatedly compairing adjacent elements and swapping them if necessary.
Time complexity
κ° μμμ λν΄ μ΄μ μ λͺ¨λ μμμ λΉκ΅ν΄μΌ νλ€.
For each element, it must be compared to all previous elements.
Best : $O(N^2)$
Average : $O(N^2)$
Worst : $O(N^2)$
#include <iostream>
using namespace std;
void BubbleSort(int arr[], int n){
for(int i = 0; i<n; i++){
for(int j = 0; j<n-i-1; j++){
if(arr[j] > arr[j+1]){
swap(arr[j], arr[j+1]);
}
}
}
}
πΊ QuickSort
ν΅ μ λ ¬μ λΆν μ 볡λ²κ³Ό μ¬κ·λ₯Ό μ¬μ©ν΄ μ λ ¬νλ μκ³ λ¦¬μ¦μ΄λ€.
Quick sort is a sorting algorithm that uses divde and conquer and recursion.
ν΅ μ λ ¬μ νΌλ΄μ΄λΌλ κ°λ μ μ¬μ©νλ€.
Quick sort uses the concept of pivot.
νΌλ΄μ΄λ μ½κ²λ§ν΄μ μ λ ¬ λ κΈ°μ€ μμλ₯Ό λ»νλ€.
Simply, pivot means the reference element to be sorted.
νΌλ΄μ μ΄λ»κ² μ ννλμ λ°λΌ ν΅ μ λ ¬μ μ±λ₯μ΄ λ¬λΌμ§λ€.
The performance of quick sort varies depending on how you choose the pivot.
νμ§λ§ μ΅μ μ νΌλ΄μ μ ννλκ²μ μ΄λ ΅κΈ° λλ¬Έμ μμλ‘ μ νμ νκ²λλ©° λ³΄ν΅ λ°°μ΄μ μ²μ, μ€μ, λ§μ§λ§ κ°μ μ ννλ€.
However, because it is difficult to select the optimal pivot, it is chosen arbitarily, usually selecting the first, midle, and last values of the array.
Best : $O(NlogN)$
Average : $O(NlogN)$
Worst : $O(N^2)$
μλλ λ°°μ΄μ 첫 μμλ₯Ό νΌλ΄μΌλ‘ μ ννμ¬ ν΅ μνΈλ₯Ό μ§ννλ μμλ€.
Below is an example of performing quicksort by selecting the first element of the array as the pivot
PIVOT : λ°μ€
LOW : λΉ¨κ°μ
HIGH : νλμ
π HeapSort
ν μ λ ¬μ μ΄ν΄νκΈ° μ μλ£κ΅¬μ‘° νμ λν΄ μμμΌ νλ€.
Before understanding heap sort, you must know about the data structure heap.
νμ μμ μ΄μ§ νΈλ¦¬μ μΌμ’ μΌλ‘ μ°μ μμ νλ₯Ό μνμ¬ λ§λ€μ΄μ§ μλ£κ΅¬μ‘°λ€.
A heap is type of complete binary tree and is a data structure created for a priority queue.
νλ λ¨Όμ λ€μ΄μ€λ λ°μ΄ν°κ° λ¨Όμ λκ°λ FIFO νμμ μλ£κ΅¬μ‘°μΈλ°, μ°μ μμ νλ λ¨Όμ λ€μ΄μ€λ λ°μ΄ν°κ° μλλΌ μ°μ μμκ° λμ λ°μ΄ν°κ° λ¨Όμ λκ°λ ννμ μλ£κ΅¬μ‘°λ€.
A queue is a FIFO type data structure in which the data that comes in first goes out first, and a priority queue is a data structure in which the data with the highest priority goes out first, not the data thata comes in first.
κ·Έλμ λΆλͺ¨λ Έλμ μλΈνΈλ¦¬κ°μ λμ κ΄κ³κ° μ±λ¦½λκ² λλ€.
Therefore, the size relationship between the parent node and the subtree is established.
νμ μ’ λ₯λ "μ΅λ ν"κ³Ό "μ΅μ ν"μΌλ‘ λλμ΄μ§λ€.
Heap types are divided into "max heap" and "min heap".
- μ΅λ ν(max heap)
λΆλͺ¨ λ Έλμ ν€ κ°μ΄ μμ λ Έλμ ν€ κ°λ³΄λ€ ν¬κ±°λ κ°μ μμ μ΄μ§ νΈλ¦¬
A complete binary tree where the key values of the parent node are greater than or equal to the key values of the child nodes.
- μ΅μ ν(min heap)
λΆλͺ¨ λ Έλμ ν€ κ°μ΄ μμ λ Έλμ ν€ κ°λ³΄λ€ μκ±°λ κ°μ μμ μ΄μ§ νΈλ¦¬
A complet binary tree where the key values of the parent node are smaller than or equal to the key values of the child nodes.
μ¦, ν μ λ ¬ μ λ΄λ¦Όμ°¨μ μ λ ¬μ μν΄μλ μ΅λ νμ ꡬμ±νκ³ , μ€λ¦μ°¨μ μ λ ¬μ μν΄μλ μ΅μ νμ ꡬμ±νλ©΄ λλ€.
In other words, when sorting a heap, a max heap can be configured for descending sorting, and a min heap cna be configured for ascending sorting.
ν νΈλ¦¬μ μ 체 λμ΄κ° κ±°μ $\log_2n$μ΄λ―λ‘ νλμ μμλ₯Ό νμ μ½μ νκ±°λ μμ ν λ νμ μ¬μ λΉ νλ μκ°μ΄ $\log_2n$λ§νΌ μμλλ€.
Since the total height of the heap tre is aprroximately $\log_2n$, it takes $\log_2n$ time to reorganize the heap when inserting or deleting one element from the heap.
μμμ κ°μκ° nκ°μ΄λ―λ‘ μ 체μ μΌλ‘ $O(n\log_2n)$μ μκ°μ΄ κ±Έλ¦°λ€.
Since there are n elements, it takes $O(n\log_2n)$ overall.
μ΅λ νμ μ½μ
1. νμ μλ‘μ΄ μμκ° λ€μ΄μ€λ©΄, μΌλ¨ μλ‘μ΄ λ Έλλ₯Ό νμ λ§μ§λ§ λ Έλμ μ΄μ΄μ μ½μ νλ€.
2. μλ‘μ΄ λ Έλλ₯Ό λΆλͺ¨ λ Έλλ€κ³Ό κ΅νν΄μ νμ μ±μ§μ λ§μ‘±μν¨λ€.
ex) μ΅λ νμ μλ‘μ΄ μμ 8μ μ½μ
μ΅λ νμ μμ
μ΅λ νμμ μ΅λκ°μ λ£¨νΈ λ Έλμ΄λ―λ‘ λ£¨νΈ λ Έλκ° μμ λλ€.
μμ λ λ£¨νΈ λ Έλμλ νμ λ§μ§λ§ λ Έλλ₯Ό κ°μ Έμ¨λ€.
νμ μ¬κ΅¬μ±νλ€.
ex) λ£¨νΈ λ Έλ 9λ₯Ό μμ .
ν μ λ ¬μ μ€νν λ 3 2 5 7 9 10 1 6 4 8 λΌλ λ°°μ΄μ μ λ ¬νλ€κ³ ν΄λ³΄μ.
μ λ ¬μ΄ μλ£λ μ΄ν ν΄λΉ λ°°μ΄μ κ°μ₯ λ§μ§λ§ λ°°μ΄μ κ°κ³Ό 0λ²μ§Έ μΈλ±μ€μ κ°μ λ³κ²½ν λ€ ν μ λ ¬μ μ§νν λ°°μ΄μ ν¬κΈ°λ₯Ό 1 μ€μ¬μ€λ€.
κ·Έλ¦¬κ³ λ¬΄νλ°λ³΅
μ΄λ₯Ό μ½λλ‘ λνλ΄λ©΄ μλμ κ°λ€.
#include <iostream>
#include <vector>
using namespace std;
void heapify(int arr[], int len, int root){
int largest = root;
int left = 2*root + 1;
int right = 2*root + 2;
if(left < len && arr[left] > arr[largest]){
largest = left;
}
if(right < len && arr[right] > arr[largest]){
largest = right;
}
if(largest != root){
swap(arr[root], arr[largest]);
heapify(arr, len, largest);
}
}
void heapSort(int arr[], int n){
for(int i=n/2-1; i>=0; i--){
heapify(arr, n, i);
}
for(int i=n-1; i>0; i--){
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}
ν μ λ ¬μμ μλμ λ§ν¬λ₯Ό μ°Έκ³ νμμ΅λλ€.
https://gmlwjd9405.github.io/2018/05/10/algorithm-heap-sort.html
πMerge Sort
λ³ν© μ λ ¬ λν λνμ μΈ λΆν μ 볡 λ°©λ²μ μ¬μ©ν μκ³ λ¦¬μ¦μ΄λ€.
Merge sort is also a representative algorithm that uses the divde and conquer method.
λ³ν© μ λ ¬μ μ νν λ°μΌλ‘ λλκ³ λμ€μ μ λ ¬νλ λ°©λ²μΈλ°, ν° λ¬Έμ λ₯Ό λ κ°μ μμ λ¬Έμ λ‘ λΆν ν λ€μ κ°μ κ³μ°νκ³ λμ€μ ν©μΉλ λ°©λ²μ μ±ννλ€.
Merge sort is a method of dividing the problem in half and sorting it later, it splits a large problem into two smaller problems, calculates them seperately, and then combines them later.
7 6 5 8 3 5 9 1 μ ν©λ³μ λ ¬μ μ΄μ©νμ¬ μ λ ¬μ μ§νν΄λ³΄κ² λ€.
μμ κ°μ΄ iμ j λ₯Ό λΉκ΅νμ¬ kμ λ£μ λ€ κ°κ°μ ν μΉΈμ© μ΄λμμΌμ£Όλ©΄μ μ λ ¬νλ κ³Όμ μ λ°λ³΅νλ€.
μ΄λ₯Ό μ½λλ‘ κ΅¬ννλ©΄ μλμ κ°λ€.
#include <iostream>
using namespace std;
int sorted[100000];
void merge(int arr[], int left, int right){
int middle = (left + right) / 2;
int i = left, j = middle + 1, k = left;
while(i <= middle && j <= right){
if(arr[i] <= arr[j]){
sorted[k++] = arr[i++];
}else{
sorted[k++] = arr[j++];
}
}
// λ¨Όμ λκΉμ§ λλ¬νλ κ²½μ° λλ¨Έμ§λ λ€ λ£μ΄μ€μΌ ν¨
if(i > middle){
for(int l = j; l <= right; l++){
sorted[k++] = arr[l];
}
} else {
for(int l = i; l <= middle; l++){
sorted[k++] = arr[l];
}
}
// μ λ ¬λ λ°°μ΄μ μ½μ
for(int l = left; l <= right; l++){
arr[l] = sorted[l];
}
}
void mergeSort(int arr[], int left, int right){
if(left < right){
int middle = (left + right) / 2;
mergeSort(arr, left, middle);
mergeSort(arr, middle+1, right);
merge(arr, left, right);
}
}
ν©λ³μ λ ¬μ μλμ λΈλ‘κ·Έλ₯Ό μ°Έκ³ νμμ΅λλ€.
https://gmlwjd9405.github.io/2018/05/08/algorithm-merge-sort.html
'Computer Science > Algorithm' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Algorithm] Dynamic Programming(5) - νλ‘μ΄λ μμ¬ (Floyd-Warshall) (0) | 2022.11.20 |
---|---|
[Algorithm] Dynamic Programming(4) - 벨λ§ν¬λ(Bellman-Ford) (0) | 2022.11.19 |
[Algorithm] Dynamic Programming(3) - Edit Distance(νΈμ§κ±°λ¦¬) (0) | 2022.11.19 |
[Algorithm] Dynamic Programming(2) - LIS(Longest Increasing Subsequence) (0) | 2022.11.17 |
[Algorithm] Dynamic Programming(1) - Warm Up (0) | 2022.11.15 |