Computer-Science

Sorting

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];
}
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;
    }
}

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];
    }
}

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$ ์ œ์ž๋ฆฌ์ •๋ ฌ

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];
}

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

Quick Sort

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

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

Expected Running Time

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

  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);
    }
}

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

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);
    }
}

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

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

Shell Sort

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

Heap Sort

[Heap Sort GeeksforGeeks Youtube](https://www.youtube.com/watch?v=MtQL_ll5KhQ)

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

Radix Sort

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

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

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

Bitonic Sort

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


Odd-even Sort

Odd-even transposition Sort

Odd-even merge Sort


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