μ΄νƒœν™
홍'story
μ΄νƒœν™
전체 방문자
였늘
μ–΄μ œ
  • λΆ„λ₯˜ 전체보기 (171)
    • TW (39)
    • AI (47)
      • μžμ—°μ–΄ 처리 (10)
      • Kaggle (2)
      • Machine Learning (26)
      • Computer Vision (0)
      • Deep Learning (0)
      • ROS2 (7)
    • Computer Science (29)
      • Data Structure (0)
      • Algorithm (18)
      • Computer Architecture (5)
      • SOLID (0)
      • System Programing (6)
    • LOLPAGO (10)
      • ν”„λ‘ νŠΈμ—”λ“œ (10)
      • λ°±μ—”λ“œ (0)
    • BAEKJOON (2)
    • React (5)
    • μ–Έμ–΄ (8)
      • C++ (8)
    • GIT (0)
    • MOGAKCO (19)
    • λ―Έκ΅­ μ—¬ν–‰κΈ° (3)
    • etc. (7)
      • Blog (2)
      • μ½œλΌν†€ (2)

λΈ”λ‘œκ·Έ 메뉴

  • ν™ˆ
  • νƒœκ·Έ
  • λ°©λͺ…둝

곡지사항

인기 κΈ€

νƒœκ·Έ

  • LOLPAGO
  • pytorch
  • NLP
  • κ²½μ‚¬ν•˜κ°•λ²•
  • Ai
  • C++
  • kaggle
  • λ°±μ€€
  • computerscience
  • baekjoon
  • react
  • algorithm
  • ML
  • κΈ°κ³„ν•™μŠ΅
  • ROS2
  • λ¨Έμ‹ λŸ¬λ‹
  • computer architecture
  • tw
  • λ”₯λŸ¬λ‹
  • μ•Œκ³ λ¦¬μ¦˜

졜근 λŒ“κΈ€

졜근 κΈ€

ν‹°μŠ€ν† λ¦¬

hELLO Β· Designed By μ •μƒμš°.
μ΄νƒœν™

홍'story

[Algorithm] Sort(Selection, Insertion, Bubble)
Computer Science/Algorithm

[Algorithm] Sort(Selection, Insertion, Bubble)

2024. 3. 8. 15:20

πŸš— 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

 

[μ•Œκ³ λ¦¬μ¦˜] νž™ μ •λ ¬(heap sort)μ΄λž€ - Heee's Development Blog

Step by step goes a long way.

gmlwjd9405.github.io

 

 

🚌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

 

[μ•Œκ³ λ¦¬μ¦˜] 합병 μ •λ ¬(merge sort)μ΄λž€ - Heee's Development Blog

Step by step goes a long way.

gmlwjd9405.github.io

 

'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
    'Computer Science/Algorithm' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
    • [Algorithm] Dynamic Programming(5) - ν”Œλ‘œμ΄λ“œ 와샬 (Floyd-Warshall)
    • [Algorithm] Dynamic Programming(4) - λ²¨λ§Œν¬λ“œ(Bellman-Ford)
    • [Algorithm] Dynamic Programming(3) - Edit Distance(νŽΈμ§‘κ±°λ¦¬)
    • [Algorithm] Dynamic Programming(2) - LIS(Longest Increasing Subsequence)
    μ΄νƒœν™
    μ΄νƒœν™
    κ³΅λΆ€ν•˜μž νƒœν™μ•„

    ν‹°μŠ€ν† λ¦¬νˆ΄λ°”