Skip to content

Latest commit

ย 

History

History

02. Sorting

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Sorting

  • Soting Algorithm Properties
    • Complexity
    • In-place
    • Stable
    • Parallel

Contents

  1. Bubble Sort : $O(n^{2})$
  2. Insertion Sort : $O(n^{2})$
  3. Selection Sort : $O(n^{2})$
  4. Merge Sort : $O(n \log n)$
  5. Quick Sort : ํ‰๊ท : $O(n \log n)$, ์ตœ์•…: $O(n^2)$
  6. Shell Sort : $O(n^{2})$ ~ $O(n \log n)$ ~ $O(n \log ^{2} n)$
  7. Heap Sort : $O(n \log n)$
  8. Counting Sort : $O(n)$
  9. Radix Sort : $O(cn)$
  10. Parallel Sorting Algorithm
  11. Bitonic Sort : $O( \log ^{2} n)$ parallel time
  12. Odd-even transposition Sort : $O(n)$ parallel time
  13. Odd-even merge Sort : $O( \log ^{2} n)$ parallel time

๋ฐฑ๋ฌธ์ด ๋ถˆ์—ฌ์ผ๊ฒฌ.

๋„์›€์ด ๋˜๋Š” ์‚ฌ์ดํŠธ : visualgo.net

Algorithm Time Stable In-place Notes
Bubble Sort $O(n^{2})$ Yes Yes - Slow (good for small inputs)
- Easy to implement
Insertion Sort $O(n^{2})$ Yes Yes - Slow (good for small inputs)
- Easy to implement
Selection Sort $O(n^{2})$ No Yes - Slow (good for small inputs)
- Easy to implement
- Un-stable algorithm
Merge Sort $O(n \log n)$ Yes No - Fast (good for huge inputs)
- Sequential data access
- Not in-place algorithm
Quick Sort $O(n \log n)$ No or Yes Yes or No - Fastest (good for large inputs)
- In-place, randomized

Bubble Sort

Bubble Sort๋Š” "Exchange Sort" ๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค.

void exchangesort (int n, keytype S[]) {
    index i, j;
    for (i = 1; i <= n-1; i++)
        for (j = i+1; j <= n; j++)
            if (S[j] < S[i])
                exchange S[i] and S[j];
}
  • ๋‚ด๋ฆผ์ฐจ์ˆœ์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋ฐ”๊พธ๋ ค๋ฉด 5๋ฒˆ ์ค„์˜ if (S[j] < S[i])์˜ ๋ถ€๋“ฑํ˜ธ ๋ฐฉํ–ฅ์„ ๋ฐ˜๋Œ€๋กœ ๋ฐ”๊พธ๋ฉด ๋œ๋‹ค.
  • 5๋ฒˆ ์ค„์˜ if (S[j] < S[i])์—์„œ S[j]์™€ S[i]๋Š” $n^{2}$๋ฒˆ ๋น„๊ต๋œ๋‹ค.
  • 6๋ฒˆ ์ค„์˜ exchange ์—ฐ์‚ฐ์€ ์•„๋ž˜์˜ 3๋‹จ๊ณ„ ์—ฐ์‚ฐ์„ ๊ฑฐ์นœ๋‹ค.
temp = a;
a = b;
b = temp;

Time Complexity

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-04-15 แ…ฉแ„’แ…ฎ 9 31 41

๊ทธ๋Ÿฌ๋ฏ€๋กœ Time Complexity๋Š” $O(n^{2})$ ์ด๋‹ค.

Insertion Sort

void insertionsort(int n, keytype S[])
{
    index i, j;
    keytype x;

    for (i = 2; i < n; i++) {
        x = S[i];
        j = i - 1;
        while (j > 0 && S[j] > x) {
            S[j+1] = S[j];
            j--;
        }
        S[j+1] = x;
    }
}
  • 6๋ฒˆ ์ค„์˜ for (i = 2; i < n; i++)์—์„œ i๊ฐ€ 2๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ด์œ ๋Š” i๊ฐ€ 1์ผ ๋•Œ๊ฐ€ ์ดˆ๊ธฐ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
    • ํ•˜๋‚˜๋งŒ ๋น„๊ตํ–ˆ์„ ๋•Œ๋Š” ๊ทธ ํ•˜๋‚˜๊ฐ€ ๋‹น์—ฐํžˆ ์ •๋ ฌ๋œ ์ƒํƒœ์ž„
  • 9๋ฒˆ ์ค„์˜ while (j > 0 && S[j] > x)์—์„œ S[j]๊ณผ x๋Š” $n^{2}$ ๋ฒˆ ๋น„๊ต๋œ๋‹ค.
  • ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, 10๋ฒˆ ์ค„ (S[j+1] = S[j];)์€ $n^{2}$ ๋ฒˆ ํ• ๋‹น๋œ๋‹ค.

Time Complexity

Worst Case

์ฃผ์–ด์ง„ i์— ๋Œ€ํ•ด i-1๋ฒˆ์˜ ๋น„๊ต๊ฐ€ ์ด๋ฃจ์–ด์ง„๋‹ค.

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-04-15 แ…ฉแ„’แ…ฎ 9 56 13

๊ทธ๋Ÿฌ๋ฏ€๋กœ Time Complexity๋Š” $O(n^{2})$ ์ด๋‹ค.

Average Case

์ฃผ์–ด์ง„ i์— ๋Œ€ํ•ด, x๊ฐ€ ์‚ฝ์ž…๋  ์ˆ˜ ์žˆ๋Š” ์žฅ์†Œ๋Š” i๊ณณ์ด ์žˆ๋‹ค.

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-04-15 แ…ฉแ„’แ…ฎ 9 59 10

๋”ฐ๋ผ์„œ ํ‰๊ท  ๋น„๊ต ํšŸ์ˆ˜๋Š” $\Theta(n^2)$ ์ด๋‹ค.

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-04-15 แ…ฉแ„’แ…ฎ 10 01 07

Selection Sort

void selectionsort(int n, keytype S[])
{
    index i, j, smallest;

    for (i = 0; i < n-1;  i++) {
        smallest = i;
        for (j = i+1; j <= n; j++)
            if (S[j] < S[smallest])
                smallest = j;
        exchange S[i] and S[smallest];
    }
}
  • 7๋ฒˆ ์ค„์˜ for (j = i+1; j <= n; j++)์—์„œ j์™€ n์€ $n^{2}$ ๋ฒˆ ๋น„๊ต๋œ๋‹ค.
  • 10๋ฒˆ ์ค„์˜ exchange S[i] and S[smallest];์—์„œ๋Š” ํ• ๋‹น์ด $3n$๋ฒˆ ์ด๋ฃจ์–ด์ง„๋‹ค.

Worst Case

$O(n^{2})$

n^2 ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋น„๊ต

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋น„๊ตํšŸ์ˆ˜ ์ง€์ •ํšŸ์ˆ˜ ์ถ”๊ฐ€์ €์žฅ์†Œ ์‚ฌ์šฉ๋Ÿ‰
Bubble Sort $T(n) = n^2/2$ $W(n) = 3n^2/2$
$A(n) = 3n^2/4$
์ œ์ž๋ฆฌ์ •๋ ฌ
Insertion Sort $W(n) = n^2/2$
$A(n) = n^2/4$
$W(n) = n^2/2$
$A(n) = n^2/4$
์ œ์ž๋ฆฌ์ •๋ ฌ
Selection Sort $T(n) = n^2/2$ $T(n) = 3n$ ์ œ์ž๋ฆฌ์ •๋ ฌ
  • ์‚ฝ์ž…์ •๋ ฌ์€ ๋ฒ„๋ธ”์ •๋ ฌ ๋ณด๋‹ค๋Š” ํ•ญ์ƒ ์ตœ์†Œํ•œ ๋น ๋ฅด๊ฒŒ ์ˆ˜ํ–‰๋œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์„ ํƒ์ •๋ ฌ์ด ๋ฒ„๋ธ”์ •๋ ฌ ๋ณด๋‹ค ๋น ๋ฅธ๊ฐ€?
    • ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ์„ ํƒ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋น ๋ฅด๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ๊ทธ๋Ÿฌ๋‚˜ ์ž…๋ ฅ์ด ์ด๋ฏธ ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ, ์„ ํƒ์ •๋ ฌ์€ ์ง€์ •์ด ์ด๋ฃจ์–ด์ง€์ง€๋งŒ ๋ฒ„๋ธ”์ •๋ ฌ์€ ์ง€์ •์ด ์ด๋ฃจ์–ด์ง€์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ฒ„๋ธ”์ •๋ ฌ์ด ๋น ๋ฅด๋‹ค.
  • ์„ ํƒ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์‚ฝ์ž…์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณด๋‹ค ๋น ๋ฅธ๊ฐ€?
    • $n$์˜ ํฌ๊ธฐ๊ฐ€ ํฌ๊ณ ,ํ‚ค์˜ ํฌ๊ธฐ๊ฐ€ ํฐ ์ž๋ฃŒ๊ตฌ์กฐ ์ผ ๋•Œ๋Š” ์ง€์ •ํ•˜๋Š” ์‹œ๊ฐ„์ด ๋งŽ์ด ๊ฑธ๋ฆฌ๋ฏ€๋กœ ์„ ํƒ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋” ๋น ๋ฅด๋‹ค.

Merge Sort

ezgif com-gif-maker

void mergesort (int n, keytype S[]) {
    const int h = n / 2, m = n - h;
    keytype U[1..h], V[1..m];

    if (n > 1) {
        copy S[1] through S[h] to U[1] through U[h];
        copy S[h+1] through S[n] to V[1] through V[m];
        mergesort(h,U);
        mergesort(m,V);
        merge(h,m,U,V,S);
    }
}
void merge (int h, int m, const keytype U[], const keytype V[], const keytype S[])
{
    index i, j, k;
    i = 1; j = 1; k = 1;
    while (i <= h && j <= m) {
        if (U[i] < V[j]) {
            S[k] = U[i];
            i++;
        } else {
            S[k] = V[j];
            j++;
        }
        k++;
    }
    if (i > h)
        copy V[j] through V[m] to S[k] through S[h+m];
    else
        copy U[i] through U[h] to S[k] through S[h+m];
}
  • Analysis of Merge-Sort
    • The height h of the merge-sort tree is $O(log n)$
      • at each recursive call we divide in half the sequence,
    • The overall amount or work done at the nodes of depth i is $O(n)$
      • we partition and merge $2^i$ sequences of size $n/2^i$
      • we make $2^(i+1)$ recursive calls
    • Thus, the total running time of merge-sort is $O(n \log n)$

๊ณต๊ฐ„๋ณต์žก๋„ ๋ถ„์„

  • in-place sort ์•Œ๊ณ ๋ฆฌ์ฆ˜ (์ œ์ž๋ฆฌ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜)
    • ์ž…๋ ฅ์„ ์ €์žฅํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋งŒํผ ์ €์žฅ์žฅ์†Œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ •๋ ฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
    • mergesort ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ in-place sort ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์•„๋‹ˆ๋‹ค.
      • ์™œ๋ƒํ•˜๋ฉด ์ž…๋ ฅ์ธ ๋ฐฐ์—ด S ์ด์™ธ์— U์™€ V๋ฅผ ์ถ”๊ฐ€๋กœ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • ๊ทธ๋Ÿฌ๋ฉด ์–ผ๋งˆ๋งŒํผ์˜ ์ถ”๊ฐ€์ ์ธ ์ €์žฅ์žฅ์†Œ๊ฐ€ ํ•„์š”ํ• ๊นŒ?
    • mergesort๋ฅผ ์žฌ๊ท€ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค ํฌ๊ธฐ๊ฐ€ S์˜ ๋ฐ˜์ด ๋˜๋Š” U์™€ V๊ฐ€ ์ถ”๊ฐ€์ ์œผ๋กœ ํ•„์š”ํ•˜๋‹ค.
    • merge ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ๋Š” U์™€ V๊ฐ€ ์ฃผ์†Œ๋กœ ์ „๋‹ฌ์ด ๋˜์–ด ๊ทธ๋ƒฅ ์‚ฌ์šฉ๋˜๋ฏ€๋กœ ์ถ”๊ฐ€์ ์ธ ์ €์žฅ์žฅ์†Œ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๋Š”๋‹ค.
      ๋”ฐ๋ผ์„œ mergesort๋ฅผ ์žฌ๊ท€ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค ์–ผ๋งˆ๋งŒํผ์˜ ์ถ”๊ฐ€์ ์ธ ์ €์žฅ์žฅ์†Œ๊ฐ€ ๋งŒ๋“ค์–ด์ ธ์•ผ ํ•˜๋Š”์ง€๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๋ฉด ๋œ๋‹ค.
    • ์ฒ˜์Œ S์˜ ํฌ๊ธฐ๊ฐ€ n์ด๋ฉด ์ถ”๊ฐ€์ ์œผ๋กœ ํ•„์š”ํ•œ U์™€ V์˜ ์ €์žฅ์žฅ์†Œ ํฌ๊ธฐ์˜ ํ•ฉ์€ n์ด ๋œ๋‹ค. ๋‹ค์Œ ์žฌ๊ท€ํ˜ธ์ถœ์—๋Š” n์˜ ์ถ”๊ฐ€ ์ ์œผ๋กœ ํ•„์š”ํ•œ ์ด ์ €์žฅ์žฅ์†Œ์˜ ํฌ๊ธฐ๋Š” $n + \frac{n}{2} + \frac{n}{4} + \cdots = 2n$ ์ด๋‹ค.
  • ๊ฒฐ๋ก ์ ์œผ๋กœ ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ณต๊ฐ„๋ณต์žก๋„๋Š” $2n\in \Theta (n)$์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ถ”๊ฐ€์ ์œผ๋กœ ํ•„์š”ํ•œ ์ €์žฅ์žฅ์†Œ๊ฐ€ n์ด ๋˜๋„๋ก, ์ฆ‰, ๊ณต๊ฐ„๋ณต์žก๋„๊ฐ€ n์ด ๋˜๋„๋ก ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค (HOW?).
  • ๊ทธ๋Ÿฌ๋‚˜ ํ•ฉ๋ณ‘์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ œ์ž๋ฆฌ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋  ์ˆ˜๋Š” ์—†๋‹ค.

Quick Sort

  • ๋น ๋ฅธ ์ •๋ ฌ(Quicksort)์ด๋ž€ ์ด๋ฆ„์ด ์˜คํ•ด์˜ ์—ฌ์ง€๊ฐ€ ์žˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์‚ฌ์‹ค ์ ˆ๋Œ€์ ์œผ๋กœ ๊ฐ€์žฅ ๋น ๋ฅธ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ  ํ•  ์ˆ˜๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ฐจ๋ผ๋ฆฌ โ€œ๋ถ„ํ• ๊ตํ™˜์ •๋ ฌ(partition exchange sort)โ€๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๊ฒŒ ๋” ์ •ํ™•ํ•˜๋‹ค.

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-04-16 -แ…ฉแ„’แ…ฎ 8 35 39

  • Quick-sort๋Š” divide-and-conquer paradigm์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” randomized sorting algorithm์ด๋‹ค.
    • Divide: pick a random element x (called pivot) and partition S into
      • L elements less than x
      • E elements equal x
      • G elements greater than x
    • Recur: sort L and G
    • Conquer: join L, E and G
Algorithm inPlaceQuickSort(S, a, b)
    if a >= b then return
    p = S.elemAtRank(b) // pivotting
    l = a
    r = b-1
    while (l <= r) // partioning
    {
        while (l <= r and S.elemAtRank(l) <= p)
            l++;
        while (l <= r and S.elemAtRank(r) >= p)
            r--;
        if (l < r)
            S.swap(S.atRank(l), S.atRank(r));
    }
    S.swap(S.atRank(l), S.atRank(b));
    inPlaceQuickSort(S, a, l-1)
    inPlaceQuickSort(S, l+1, b)

Worst-case Running Time

  • The worst case for quick-sort occurs when the pivot is the unique minimum or maximum element
  • One of L and G has size n - 1 and the other has size 0
  • The running time is proportional to the sum n + (n - 1) + ... + 2 + 1
  • Thus, the worst-case running time of quick-sort is O(n^2)
    • Best-case is O(n log n)

Expected Running Time

  • The expected height of the quick-sort tree is O(log n)
  • The amount or work done at the nodes of the same depth is $O(n)$
  • Thus, the expected running time of quick-sort is O(n log n)

์„ฑ๋Šฅ ํ–ฅ์ƒ ๋ฐฉ๋ฒ•

  1. ์Šคํƒ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ˆœํ™˜์„ ์ œ๊ฑฐ
  2. ์ž‘์€ ๋ถ€๋ถ„ํŒŒ์ผ์˜ ๊ฒฝ์šฐ ์‚ฝ์ž… ์ •๋ ฌ ์‚ฌ์šฉ
  3. ์ค‘๊ฐ„๊ฐ’ ๋ถ„ํ•  (Median-of-Three Partioning)

1. ์ˆœํ™˜ ์ œ๊ฑฐ

void QuickSort(int a[], int l, int r)
{
    int i, j, v;
    for (;;) {
        while (r > l) {
            i = partition(a, l, r);
            if (i-l > r-i) {
                push(&top, l); push(&top, i-1); l = i+1;
            } else {
                push(&top, i+1); push(&top, r); r = i-1;
            }
        }
        if (top < 0) break;
        r = pop(&top);
        l = pop(&top);
    }
}

์‚ฝ์ž… ์ •๋ ฌ ์‚ฌ์šฉ

  • if (r>l)์„ if (r-l <= M) { InsertionSort(a, l, r) }๋กœ
    • M ๊ฐ’์œผ๋กœ 5 ~ 25 ์‚ฌ์šฉ
void QuickSort(int a[], int l, int r)
{
    int i, j, v;
    if (r-l <= M) // ํŒŒํ‹ฐ์…˜์˜ ํฌ๊ธฐ๊ฐ€ M๋ณด๋‹ค ์ž‘์œผ๋ฉด
        InsertionSort(a, l, r); // ๊ทธ๋ƒฅ n^2์„ ์“ฐ์ž
    else {
        // ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ
        v = a[r]; i = l-1; j = r;
        for ( ; ; ) {
            while (a[++i] < v);
            while (a[--j] > v);
            if (i >= j) break;
            swap(a, i, j);
        }
        swap(a, i, r);
        // ์—ฌ๊ธฐ๊นŒ์ง€๋Š” ๋™์ผํ•˜๋‹ค.
        QuickSort(a, l, i-1);
        QuickSort(a, i+1, r);
    }
}
  • ๋งŽ์€ ์‘์šฉ์—์„œ ์•ฝ 20% ์ •๋„์˜ ์‹œ๊ฐ„ ์ ˆ๊ฐ ํšจ๊ณผ๊ฐ€ ์žˆ์Œ

์ค‘๊ฐ„๊ฐ’ ๋ถ„ํ• 

  • ๋ถ„ํ•  ์›์†Œ๋ฅผ ์„ ํƒํ•  ๋•Œ ์™ผ์ชฝ, ๊ฐ€์šด๋ฐ, ์˜ค๋ฅธ์ชฝ ์›์†Œ ์ค‘ ๊ฐ’์ด ์ค‘๊ฐ„์ธ ์›์†Œ๋ฅผ ์„ ํƒ
  • ์™ผ์ชฝ, ๊ฐ€์šด๋ฐ, ์˜ค๋ฅธ์ชฝ ์›์†Œ๋ฅผ ์ •๋ ฌํ•œ ํ›„ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ a[l], ๊ฐ€์žฅ ํฐ ๊ฐ’์„ a[r], ์ค‘๊ฐ„ ๊ฐ’์„ a[r-1]์— ๋„ฃ๊ณ , a[l+1], ..., a[r-2]์— ๋Œ€ํ•ด ๋ถ„ํ•  ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ˆ˜ํ–‰
  • ์žฅ์ 
    • ์ตœ์•…์˜ ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ํ™•๋ฅ ์„ ๋‚ฎ์ถ”์–ด ์คŒ
    • ๊ฒฝ๊ณ„ ํ‚ค(sentinel key)๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†์Œ
    • ์ „์ฒด ์ˆ˜ํ–‰ ์‹œ๊ฐ„์„ ์•ฝ 5% ๊ฐ์†Œ์‹œํ‚ด
void QuickSort(int a[], int l, int r)
{
    int i, j, m, v;
    if (r - l > 1) {
        m = (l + r) / 2;

        // 3๊ฐœ๋ฅผ ์ž„์˜๋กœ ์„ ํƒํ•˜๊ณ , ๊ทธ ์ค‘์— ์ค‘๊ฐ„๊ฐ’์„ pivot์œผ๋กœ ์„ ํƒํ•˜๋Š” ์ฝ”๋“œ
        if (a[l] > a[m]) swap(a, l, m);
        if (a[l] > a[r]) swap(a, l, r);
        if (a[m] > a[r]) swap(a, m, r);
        swap(a, m, r-1);

        // ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ
        v = a[r-1]; i = l; j = r-1;
        for (;;) {
            while (a[++i] < v);
            while (a[--j] > v);
            if (i >= j) break;
            swap(a, i, j);
        }
        swap(a, i, r-1);
        // ์—ฌ๊ธฐ๊นŒ์ง€๋Š” ๋™์ผ
        QuickSort(a, l, i-1);
        QuickSort(a, i+1, r);
    }
    else if (a[l] > a[r]) swap(a, l, r);
}

Merge Sort VS. Quick Sort

  • ํ•ฉ๋ณ‘ ์ •๋ ฌ : ๋ถ„ํ• -์ •๋ณต (divide-and-conquer)
    • ์ฒ˜์Œ์— ํ™”์ผ์„ ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๋ถ„ํ• ํ•˜๊ณ  ๋‚˜์„œ, ๊ฐ๊ฐ์˜ ๋ถ€๋ถ„์„ ๊ฐœ๋ณ„์ ์œผ๋กœ ์ •๋ณตํ•จ
    • Stable !
  • ํ€ต ์ •๋ ฌ : ์ •๋ณต-๋ถ„ํ• (conquer-and-divide)
    • ์ˆœํ™˜ ํ˜ธ์ถœ์ด ์ด๋ฃจ์–ด์ง€๊ธฐ ์ „์— ๋Œ€๋ถ€๋ถ„์˜ ์ž‘์—…์ด ์ˆ˜ํ–‰
    • Stable ?

Shell Sort

  • ์‚ฝ์ž… ์ •๋ ฌ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ณ€ํ˜•
    • k๊ฐœ์˜ ์„œ๋ธŒ๋ฆฌ์ŠคํŠธ๋กœ ๋ถ„ํ• ํ•˜์—ฌ ์‚ฝ์ž…์ •๋ ฌ
  • ๋ฉ€๋ฆฌ ๋–จ์–ด์ง„ ์›์†Œ๋ผ๋ฆฌ ๊ตํ™˜์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜์—ฌ ์ •๋ ฌ ์†๋„๋ฅผ ํ–ฅ์ƒ์‹œํ‚ด
  • h-์ •๋ ฌ ํ™”์ผ : ๋ชจ๋“  h๋ฒˆ์งธ ์›์†Œ๋ฅผ ์ •๋ ฌํ•œ ํ™”์ผ
  • ์ฆ๊ฐ€ ์ˆœ์„œ์˜ ์˜ˆ (๊ฐ„๊ฒฉ) : ..., 1093, 364, 121, 40, 13, 4, 1
    • ํ™€์ˆ˜, ์ง์ˆ˜๊ฐ€ ๋ฐ˜๋ณต๋˜๋Š” ๊ฒƒ์ด ์ข‹์Œ
    • ์˜ˆ) ์ฆ๊ฐ€์‹: h = 3*h + 1, ๊ฐ์†Œ์‹: h = h/3
  • ํŠน์ง•
    • ์ˆœ์—ด h๊ฐ€ 1, 4, 13, 40, ...์ผ๋•Œ, ์‰˜ ์ •๋ ฌ์˜ ๋น„๊ต ํšŸ์ˆ˜๋Š” $N^{3/2}$ ์„ ๋„˜์ง€ ์•Š์Œ
    • ์•ˆ์ •์ ์ธ ์ œ์ž๋ฆฌ ์ •๋ ฌ
ShellSort(a[], n)
    for (h โ† 1; h < n; h โ† 3*h+1) do { }; // ์ฒซ ๋ฒˆ์งธ h ๊ฐ’ ๊ณ„์‚ฐ
    for ( ; h > 0; h โ† h/3) do { // h ๊ฐ’์„ ๊ฐ์†Œ์‹œํ‚ค๋ฉฐ ์ง„ํ–‰
        for (i โ† h + 1; i โ‰ค n; i โ† i+1) do {
            v โ† a[i];
            j โ† i;
            while (j > h and a[j-h] > v) do {
                a[j] โ† a[j-h];
                j โ† j-h;
            } // while
            A[j] โ† v;
        } // for
    } // for
end ShellSort()

Analysis

  • $O(n^{2})$ (worst known worst case gap sequence)
  • $O(n \log n)$ (most gap sequences)
  • $O(n \log ^{2} n)$ (best known worst case gap sequence)

Heap Sort

Heap Sort | GeeksforGeeks Youtube

  • ํžˆํ”„(heap)๋ฅผ ์ด์šฉํ•ด ์ •๋ ฌ

    • ์ •๋ ฌํ•  ์›์†Œ๋ฅผ ๋ชจ๋‘ ๊ณต๋ฐฑ ํžˆํ”„์— ํ•˜๋‚˜์”ฉ ์‚ฝ์ž…
    • ํ•œ ์›์†Œ์”ฉ ์‚ญ์ œ โ†’ ์ œ์ผ ํฐ ์›์†Œ๊ฐ€ ์‚ญ์ œ๋จ
    • ์ด ์›์†Œ๋ฅผ ๋ฆฌ์ŠคํŠธ์˜ ๋’ค์—์„œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ์‚ฝ์ž…
    • ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑ
  • ์ œ์ž๋ฆฌ ์ •๋ ฌ(in-place)์ด์ง€๋งŒ ๋ถˆ์•ˆ์ •์ (unstable)

  • $N$๊ฐœ์˜ ์›์†Œ๋ฅผ ์ •๋ ฌํ•˜๋Š”๋ฐ $N \log N$ ๋‹จ๊ณ„๊ฐ€ ํ•„์š”ํ•จ

  • ์ž…๋ ฅ ๋ฐฐ์—ด์˜ ์ˆœ์„œ์— ๋ฏผ๊ฐํ•˜์ง€ ์•Š์Œ

  • ๋‚ด๋ถ€ ๋ฃจํ”„๊ฐ€ ํ€ต ์ •๋ ฌ๋ณด๋‹ค ์•ฝ๊ฐ„ ๊ธธ์–ด์„œ ํ‰๊ท ์ ์œผ๋กœ ํ€ต ์ •๋ ฌ๋ณด๋‹ค 2๋ฐฐ ์ •๋„ ๋Š๋ฆผ

  • ์ฐธ๊ณ  : ํžˆํ”„ - ์šฐ์„ ์ˆœ์œ„ ํ์˜ ์ผ์ข…

    • ํžˆํ”„ ๊ตฌ์กฐ
      • แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-04-16 - แ…ฉแ„’แ…ฎ 11 27 19
    • ํžˆํ”„๋Š” ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ(complete binary tree) ์ด๋ฉฐ ์ค‘๋ณต๋œ ๊ฐ’์„ ํ—ˆ์šฉํ•จ์— ์œ ์˜ํ•œ๋‹ค.
      • ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ์—์„œ๋Š” ์ค‘๋ณต๋œ ๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์•˜๋‹ค.
  • ์ฐธ๊ณ  : ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ : ๋…ธ๋“œ๋ฅผ ์‚ฝ์ž…ํ•  ๋•Œ ์™ผ์ชฝ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์‚ฝ์ž…ํ•˜๋Š” ํŠธ๋ฆฌ

    • ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ
    • ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ X

Heap Sort Algorithm

HeapSort(a[])
{
    n โ† a.length-1; // n์€ ํžˆํ”„ ํฌ๊ธฐ (์›์†Œ์˜ ์ˆ˜)
                    // a[0]์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  a[1 : n]์˜ ์›์†Œ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
    for (i โ† n/2; i โ‰ฅ 1; i โ† i-1) do    // ๋ฐฐ์—ด a[]๋ฅผ ํžˆํ”„๋กœ ๋ณ€ํ™˜
        heapify(a, i, n);               // i๋Š” ๋‚ด๋ถ€ ๋…ธ๋“œ

    for (i โ† n-1; i โ‰ฅ 1; i โ† i-1) do {  // ๋ฐฐ์—ด a[]๋ฅผ ํžˆํ”„๋กœ ๋ณ€ํ™˜
        temp โ† a[1];    // a[1]์€ ์ œ์ผ ํฐ ์›์†Œ
        a[1] โ† a[i+1];  // a[1]๊ณผ a[i+1]์„ ๊ตํ™˜
        a[i+1] โ† temp;
        heapify(a, 1, i);
    }
}
end heapSort()

Heapify Algorithm

heapify(a[], h, m)
{
    // ๋ฃจํŠธ h๋ฅผ ์ œ์™ธํ•œ h์˜ ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์™€ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋Š” ํžˆํ”„
    // ํ˜„์žฌ ์‹œ์ ์œผ๋กœ ๋…ธ๋“œ์˜ ์ตœ๋Œ€ ๋ ˆ๋ฒจ ์ˆœ์„œ ๋ฒˆํ˜ธ๋Š” m
    v โ† a[h];
    for (j โ† 2*h; j โ‰ค m; j โ† 2*j) do {
        if (j < m and a[j] < a[j+1]) then j โ† j+1;
                                // j๋Š” ๊ฐ’์ด ํฐ ์™ผ์ชฝ ๋˜๋Š” ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ
        if (v โ‰ฅ a[j]) then exit;
        else a[j/2] โ† a[j];     // a[j]๋ฅผ ๋ถ€๋ชจ ๋…ธ๋“œ๋กœ ์ด๋™
    }
    a[j/2] โ† v;
}
end heapify()

Counting Sort

  • ๊ณ„์ˆ˜ ์ •๋ ฌ
  • ์žฅ์ 
    • ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ $O(n)$์ด๋‹ค.
    • ๋น„๊ต ๊ธฐ๋ฐ˜ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋น„ํ•ด ๋น ๋ฅด๋‹ค.
  • ๋‹จ์ 
    • N์— ๋น„๋ก€ํ•˜๋Š” ์ถ”๊ฐ€ ๊ธฐ์–ต์žฅ์†Œ๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ œ์ž๋ฆฌ ์ •๋ ฌ์€ ์•„๋‹ˆ๋‹ค.
      • N์˜ ๋ฒ”์œ„๊ฐ€ ํฌ๋ฉด ๊ณต๊ฐ„์˜ ๋‚ญ๋น„๊ฐ€ ์‹ฌํ•ด์ง„๋‹ค.
    • ์ค‘๋ณต์ด ๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด, ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์˜๋ฏธ๊ฐ€ ์—†๋‹ค.
  • ์ ์šฉ ๋ฒ”์œ„
    • ์ž…๋ ฅ ํ‚ค๊ฐ€ ์–ด๋–ค ๋ฒ”์œ„์— ์žˆ์„ ๋•Œ ์ ์šฉ ๊ฐ€๋Šฅ
    • ์˜ˆ๋ฅผ ๋“ค์–ด 1๋ถ€ํ„ฐ k ์‚ฌ์ด์˜ ์ž‘์€ ์ •์ˆ˜ ๋ฒ”์œ„์— ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ฏธ๋ฆฌ ์•Œ๊ณ  ์žˆ์„ ๋•Œ์—๋งŒ ์ ์šฉ ๊ฐ€๋Šฅ

Radix Sort

  • ์ „์ฒด ํ‚ค๋ฅผ ์—ฌ๋Ÿฌ ์ž๋ฆฌ๋กœ ๋‚˜๋ˆ„์–ด ๊ฐ ์ž๋ฆฌ๋งˆ๋‹ค ๊ณ„์ˆ˜ ์ •๋ ฌ๊ณผ ๊ฐ™์€ ์•ˆ์ •์  ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์šฉํ•˜์—ฌ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•
  • $d$ ์ž๋ฆฌ์ˆ˜ ์ˆซ์ž๋“ค์— ๋Œ€ํ•˜์—ฌ ๊ณ„์ˆ˜ ์ •๋ ฌ๋กœ ์ •๋ ฌ (์ž๋ฆฟ์ˆ˜๋งŒํผ ๋บ‘๋บ‘์ด๋ฅผ ๋ˆ๋‹ค.) - ๊ฐ ์ž๋ฆฌ์ˆ˜๋งˆ๋‹ค $O(n)$ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋ฏ€๋กœ ์ „์ฒด๋กœ๋Š” $O(dN)$ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋Š”๋ฐ, $d$ ๋ฅผ ์ƒ์ˆ˜๋กœ ์ทจ๊ธ‰ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด $O(n)$ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๊ฒŒ ๋จ
  • ์ „์ฒด ์ •๋ ฌ ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜๋งŒํผ์˜ ๊ธฐ์–ต ์žฅ์†Œ์™€ ์ง„๋ฒ• ํฌ๊ธฐ๋งŒํผ์˜ ๊ธฐ์–ต ์žฅ์†Œ๊ฐ€ ์ถ”๊ฐ€๋กœ ํ•„์š”ํ•จ
  • ํ‚ค๊ฐ€ $m$์ž๋ฆฌ ์ˆซ์ž๋กœ ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ $m$๋ฒˆ์˜ ํŒจ์Šค๋ฅผ ๋ฐ˜๋ณต ์ˆ˜ํ–‰
  • $N$ ๊ฐœ์˜ ์›์†Œ์— ๋Œ€ํ•ด ์ด ์—ฐ์‚ฐ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” $ฮŸ(N)$
  • ์‚ฌ์šฉ ์˜ˆ : ํ•™๋ฒˆ ๋ฐ ์‚ฌ๋ฒˆ ๊ฐ™์ด ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๊ณ ์œ ๋ฒˆํ˜ธ, ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ ๋“ฑ
RadixSort(a[], n)
{
    for (k โ† 1; k โ‰ค m; k โ† k+1) do {    // m์€ digit ์ˆ˜, k=1์€ ๊ฐ€์žฅ ์ž‘์€ ์ž๋ฆฌ ์ˆ˜
        for (i โ† 0; i < n; i โ† i+1) do {
            kd โ† digit(a[i], k);    // k๋ฒˆ์งธ ์ˆซ์ž๋ฅผ kd์— ๋ฐ˜ํ™˜
            enqueue(Q[kd], a[i]);   // Q[kd]์— a[i]๋ฅผ ์‚ฝ์ž…
        }
        p โ† 0;
        for (i โ† 0; i โ‰ค 9; i โ† i+1) do {
            while (Q[i] =ฬธ ร˜) do {   // Q[i]์˜ ๋ชจ๋“  ์›์†Œ๋ฅผ a[]๋กœ ์ด๋™
                p โ† p+1;
                a[p] โ† dequeue(Q[i]);
            }
        }
    }
}
end RadixSort()

Parallel Sorting Algorithm

CPU GPU
- ๋ฒ”์šฉ ์ปดํ“จํŒ… - Graphic ํŒŒ์ดํ”„๋ผ์ธ ์ฒ˜๋ฆฌ
- ๋ฉ€ํ‹ฐ์ฝ”์–ด (1~8) - ๋ฉ€ํ‹ฐ์ฝ”์–ด (์ˆ˜๋ฐฑ ~ ์ˆ˜๋งŒ)
- Out-of-order control (Sophisticated control) - In-order processing (Simple control)
- Fancy Branch predictor - High bandwidth data access
- Few Arithmetic Logic Units (ALU) - A lot of Arithmetic Logic Units (ALU)
- Powerful ALU - Energy efficient ALUs
- Reduced operation latency - Many, long latency but heavily pipelined for high throughput
- For sequential parts where latency matters - For parallel parts where throughput wins
- Big cache - Small cache

Parallelism

  • Thread Divergence
    • different threads in the same warp (scheduling units in GPU, 32 threads) taking different conditional branches
  • Warp Serialization
    • different threads in the same warp competing for the same bank of shared memory
  • Memory Coalescing
    • different threads in the same warp accessing different cache lines
  • Partition Camping
    • unbalanced access to memory partitions
  • Occupancy
    • create a massive number of threads
  • Register Pressure
    • reuse data in registers
  • Memory Pressure
    • reuse data in shared memory

๋ณ‘๋ ฌ์„ฑ์˜ ๋น„์œ 

30์ธ 31๊ฐ์„ ํ•œ๋‹ค๊ณ  ์ƒ์ƒํ•ด๋ณด์ž.
1๋ช…์ด ์‚๋—ํ•˜๋ฉด ์ „๋ถ€ ๋„˜์–ด์งˆ ๊ฒƒ์ด๋‹ค.

Bitonic Sort

  • Bubble Sort ๊ธฐ๋ฐ˜์˜ Parallel Sorting Algorithm์œผ๋กœ, Bi-tone ์ฆ‰, 2๊ฐœ์˜ ํ†ค์„ ์˜๋ฏธํ•œ๋‹ค.
    • ์•„๋ž˜ ์‚ฌ์ง„์„ ๋ณด๋ฉด ๋ฐฉํ–ฅ์ด ์œ„ ์•„๋ž˜ 2๊ฐœ ์ฆ‰, ํ†ค์ด 2๊ฐœ์ด๋‹ค.
  • Sorting Network(์ •๋ ฌ๋ง)์„ ๋ฏธ๋ฆฌ ์ •์˜ํ•ด์•ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

In computer science, comparator networks are abstract devices built up of a fixed number of "wires", carrying values, and comparator modules that connect pairs of wires, swapping the values on the wires if they are not in a desired order.
Such networks are typically designed to perform sorting on fixed numbers of values, in which case they are called sorting networks.

"Sorting network" From Wikipedia

  • ์™„๋ฒฝํ•˜๊ฒŒ ๋ณ‘๋ ฌ์€ ์•„๋‹ˆ์ง€๋งŒ, ํŠน์ • ์‹œ์ ์—์„œ๋Š” ๊ตํ™˜์ด ๋™์‹œ์— ์ผ์–ด๋‚˜๋ฏ€๋กœ, ๋ณ‘๋ ฌ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • Analysis
    • $O (n\log^2 n)$ comparators (๋น„๊ตํšŸ์ˆ˜)
    • $O( \log ^{2} n)$ parallel time
  • Space complexity
    • $O (n\log^2 n)$

Odd-even Sort

Odd-even transposition Sort

  • Odd-Even Transposition Sort๋Š” Bubble Sort ๊ธฐ๋ฐ˜์˜ parallel sorting algorithm์ด๋‹ค.
    • a.k.a. Brick Sort, Parity Sort

  • The Odd-Even Transition Sorting Network for Keys
  • Analysis
    • $O(n)$ parallel time

Odd-even merge Sort

  • The Odd-Even Merge Sorting Network for Keys
  • Analysis
    • $O( \log ^{2} n)$ parallel time
      • Worst-case performance - $O( \log ^{2} n)$ parallel time
      • Best-case performance - $O( \log ^{2} n)$ parallel time
      • Average performance - $O( \log ^{2} n)$ parallel time
      • Worst-case space complexity - $O(n \log ^{2} n)$ non-parallel time

  • Faster than other sorting networks that have a complexity of $O(n \log ^{2} n)$, e.g. bitonic sort and shellsort
n odd-even
mergesort
bitonic sort shellsort
4 5 6 6
16 63 80 83
64 543 672 724
256 3839 4608 5106
1024 24063 28160 31915