๋ฐฑ๋ฌธ์ด ๋ถ์ฌ์ผ๊ฒฌ.
๋์์ด ๋๋ ์ฌ์ดํธ : 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๋ โ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];
}
if (S[j] < S[i])
์ ๋ถ๋ฑํธ ๋ฐฉํฅ์ ๋ฐ๋๋ก ๋ฐ๊พธ๋ฉด ๋๋ค.if (S[j] < S[i])
์์ S[j]
์ S[i]
๋ $n^{2}$๋ฒ ๋น๊ต๋๋ค.exchange
์ฐ์ฐ์ ์๋์ 3๋จ๊ณ ์ฐ์ฐ์ ๊ฑฐ์น๋ค.temp = a;
a = b;
b = temp;
๊ทธ๋ฌ๋ฏ๋ก Time Complexity๋ $O(n^{2})$ ์ด๋ค.
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;
}
}
for (i = 2; i < n; i++)
์์ i
๊ฐ 2๋ถํฐ ์์ํ๋ ์ด์ ๋ i
๊ฐ 1์ผ ๋๊ฐ ์ด๊ธฐ๊ฐ์ด๊ธฐ ๋๋ฌธ์ด๋ค.
while (j > 0 && S[j] > x)
์์ S[j]
๊ณผ x
๋ $n^{2}$ ๋ฒ ๋น๊ต๋๋ค.S[j+1] = S[j];
)์ $n^{2}$ ๋ฒ ํ ๋น๋๋ค.์ฃผ์ด์ง i
์ ๋ํด i-1
๋ฒ์ ๋น๊ต๊ฐ ์ด๋ฃจ์ด์ง๋ค.
๊ทธ๋ฌ๋ฏ๋ก Time Complexity๋ $O(n^{2})$ ์ด๋ค.
์ฃผ์ด์ง i
์ ๋ํด, x
๊ฐ ์ฝ์
๋ ์ ์๋ ์ฅ์๋ i
๊ณณ์ด ์๋ค.
๋ฐ๋ผ์ ํ๊ท ๋น๊ต ํ์๋ $\Theta(n^2)$ ์ด๋ค.
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];
}
}
for (j = i+1; j <= n; j++)
์์ j
์ n
์ $n^{2}$ ๋ฒ ๋น๊ต๋๋ค.exchange S[i] and S[smallest];
์์๋ ํ ๋น์ด $3n$๋ฒ ์ด๋ฃจ์ด์ง๋ค.$O(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$ | ์ ์๋ฆฌ์ ๋ ฌ |
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];
}
h
of the merge-sort tree is $O(log n)$
i
is $O(n)$
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)
L
and G
has size n - 1
and the other has size 0n + (n - 1) + ... + 2 + 1
O(n^2)
O(n log n)
O(log n)
O(n log n)
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) }
๋ก
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);
}
}
a[l]
, ๊ฐ์ฅ ํฐ ๊ฐ์ a[r]
, ์ค๊ฐ ๊ฐ์ a[r-1]
์ ๋ฃ๊ณ , a[l+1]
, โฆ, a[r-2]
์ ๋ํด ๋ถํ ์๊ณ ๋ฆฌ์ฆ์ ์ํ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);
}
h = 3*h + 1
, ๊ฐ์์: h = h/3
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()
[Heap Sort | GeeksforGeeks Youtube](https://www.youtube.com/watch?v=MtQL_ll5KhQ) |
๋ด๋ถ ๋ฃจํ๊ฐ ํต ์ ๋ ฌ๋ณด๋ค ์ฝ๊ฐ ๊ธธ์ด์ ํ๊ท ์ ์ผ๋ก ํต ์ ๋ ฌ๋ณด๋ค 2๋ฐฐ ์ ๋ ๋๋ฆผ
์ฐธ๊ณ : ํํ - ์ฐ์ ์์ ํ์ ์ผ์ข
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(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()
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()
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 |
30์ธ 31๊ฐ์ ํ๋ค๊ณ ์์ํด๋ณด์.
1๋ช
์ด ์๋ํ๋ฉด ์ ๋ถ ๋์ด์ง ๊ฒ์ด๋ค.
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
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 |