๊ณต๊ฐ ๋ณต์ก๋(Space Complexity) : ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉ๋๋ ๋ฉ๋ชจ๋ฆฌ ์ด๋
์๊ฐ ๋ณต์ก๋(Time Complexity) : ์๊ณ ๋ฆฌ์ฆ์ ์ํ๋๋ ์ฐ์ฐ ํ์ ์ด๋
์์๋ค์ ํตํด ๊ณต๊ฐ๋ณต์ก๋์ ๋ํด ์์๋ณด๊ฒ ๋ค.
ใ กใ กใ กใ กใ ก
int get_sum(int arr[], int n) {
int sum = 0;
int i = 0;
for (i = 0; i < n; ++i) {
sum += arr[i];
}
return sum;
}
ใ กใ กใ กใ กใ ก
๊ณต๊ฐ๋ณต์ก๋ = n + 3
3์ sum, i, n ์ด ์ธ ๊ฐ์ ๋ณ์๋ฅผ ์๋ฏธํ๋ค.
ใ กใ กใ กใ กใ ก
int get_sum(int** arr, int a, int b) {
int sum = 0;
int i = 0, j = 0;
for (i = 0; i < a; ++i) {
for(j = 0; j < b; ++j){
sum += arr[i][j];
}
}
return sum;
}
ใ กใ กใ กใ กใ ก
๊ณต๊ฐ๋ณต์ก๋ = a * b + 5
5: sum, a, b, i, j
์์๋ค์ ํตํด ์๊ฐ๋ณต์ก๋์ ๋ํด ์์๋ณด๊ฒ ๋ค.
ใ กใ กใ กใ กใ ก
int get_sum(int arr[], int n) {
int sum = 0;
int i = 0;
for (i = 0; i < n; ++i) {
sum += arr[i];
}
return sum;
}
ใ กใ กใ กใ กใ ก
์๊ฐ๋ณต์ก๋: n
์ for๋ฌธ์ nํ ๋ฐ๋ณตํ๋ค.
ใ กใ กใ กใ กใ ก
int get_sum(int arr[], int n) {
int sum = 0;
int i = 0;
for (i = 0; i < n; i+=2) {
sum += arr[i];
}
return sum;
}
ใ กใ กใ กใ กใ ก
์๊ฐ๋ณต์ก๋ = n/2
ใ กใ กใ กใ กใ ก
int get_sum(int** arr, int a, int b) {
int sum = 0;
int i = 0, j = 0;
for (i = 0; i < a; ++i) {
for(j = 0; j < b; ++j){
sum += arr[i][j];
}
}
return sum;
}
ใ กใ กใ กใ กใ ก
๊ณต๊ฐ๋ณต์ก๋ = a * b
ใ กใ กใ กใ กใ ก
int get_sum(int n) {
int sum = 0;
int i;
for (i = 0; i < n; ++i) {
sum += i;
}
return sum;
}
ใ กใ กใ กใ กใ ก
์๊ฐ๋ณต์ก๋ = n
ใ กใ กใ กใ กใ ก
int get_sum(int** arr, int n) {
int sum = 0;
int i = 0, j = 0;
for (i = 0; i < n; ++i) {
for(j = 0; j < n; ++j){
sum += arr[i][j];
}
}
return sum;
}
ใ กใ กใ กใ กใ ก
์๊ฐ๋ณต์ก๋ = n**2 (n์ 2์น)
ใ กใ กใ กใ กใ ก
int get_sum(int n) {
int sum = 0;
int i;
for (i = 1; i <= n; i*=2) {
sum += i;
}
return sum;
}
ใ กใ กใ กใ กใ ก
์๊ฐ๋ณต์ก๋ = log2(n)
๋ฐ์ด 2์ธ ๋ก๊ทธ์ n
--> ๋๋ต log2(n) ๊ผด ์ด๋ค.
**๊ทธ๋ํ๊ฐ ๊ฐํ๋ฅผ์๋ก (๊ธฐ์ธ๊ธฐ๊ฐ ํด์๋ก) ์ค์ฉ์ฑ์ด ๊ฐ์ํ๋ค.**
ใ กใ กใ กใ กใ ก
int get_sum(int n) {
int sum = 0;
int i, j;
for (i = 1; i <= n; ++i) {
for(j = 1; j <= i; ++j){
sum += j;
}
}
return sum;
}
ใ กใ กใ กใ กใ ก
์๊ฐ๋ณต์ก๋ = n ( n + 1 ) / 2
ใ กใ กใ กใ กใ ก
ใ กใ กใ กใ กใ ก
int get_sum(int n) {
int sum = 0;
int i, j;
for (i = 1; i <= n; ++i) { // nํ ๋ฐ๋ณต.
for(j = 1; j <= i; i*=2){ // log2(n). --> n*log2(n)
sum += j;
}
}
return sum;
}
ใ กใ กใ กใ กใ ก
์๊ฐ๋ณต์ก๋ = n log2(n)
๋น -์ค ํ๊ธฐ๋ฒ(Big-O Notation)
์๊ฐ๋ณต์ก๋์ ๊ณต๊ฐ๋ณต์ก๋์ ์ต๊ณ ์ฐจํญ๋ง์ ํ๊ธฐํ์ฌ
๊ฐ๋ตํ๊ฒ ๋ํ๋ด๋ ํ๊ธฐ๋ฒ
๋ณดํต ์๊ณ ๋ฆฌ์ฆ์ ์ํํ๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ํ์ค ์๊ฐ ๋์ ์ต์ ์ ์๊ฐ์ ์ฐ์ถํ์ฌ ํ๊ธฐํจ
์)
T(n) = 3n + 1 -> O(n)
T(n) = n**2 + 3n + 1 -> O(n2)
T(n) = 26n**3+ n + 255 -> O(n3)
ํ๊ธฐ |
์ด๋ฆ |
1 |
2 |
4 |
8 |
16 |
32 |
O(1) |
์์ํ(Constant) |
1 |
1 |
1 |
1 |
1 |
1 |
O(n) |
์ ํ(Linear) |
1 |
2 |
4 |
8 |
16 |
32 |
O(log n) |
๋ก๊ทธํ(Logarithmic) |
0 |
1 |
2 |
3 |
4 |
5 |
O(n log n) |
์ ํ ๋ก๊ทธํ(Log Linear) |
0 |
2 |
8 |
24 |
64 |
160 |
O(n2) |
ํ๋ฐฉํ(Quadratic) |
1 |
4 |
16 |
64 |
256 |
1024 |
O(n3) |
์ ๋ฐฉํ(Cubic) |
1 |
8 |
64 |
512 |
4,096โฌ |
32,768 |
O(2n) |
์ง์ํ(Exponential) |
2 |
4 |
16 |
256 |
65,536โฌ |
4,294,967,296โฌ |
O(n!) |
๊ณ์นํ(Factorial) |
1 |
2 |
24 |
40,320โฌ |
20,922,789,888,000โฌ |
2.6313083693369353016721801216e+35โฌ |
O(1)์ ์ (๋ฐ๋ณต์ด ๋์ง ์๋ ์๊ณ ๋ฆฌ์ฆ)
void test1() {
printf("Hello!");
}
void test2(int arr[]) {
printf("%d", arr[2]);
}
void test3(int n) {
if (n % 2)
printf("ํ์");
else
printf("์ง์");
}
O(n)์ ์ (์ ํ ๊ตฌ์กฐ์ ์์ฐจ ํ์)
void test(int arr[], int n) {
int i;
for (i = 0; i < n; ++i)
printf("%d", arr[i]);
}
O(log n)์ ์ (์ด์ง ํ์)
int index(int arr[], int n, int target) {
int i = n / 2; // i๋ 2๋ก ๋๋ ๋ชซ.
while (i >= -1 && i < n) {
if (target == arr[i])
return i;
if (target > arr[i]) {
i = (n - i) / 2 + i;
continue;
}
if (target < arr[i])
i = i / 2;
}
return -1;
}
O(n log n)์ ์ (Quick Sort, Heap Sort)
O(n2)์ ์ (์ด์ค ๋ฐ๋ณต๋ฌธ์ ์ฒ๋ฆฌ)
O(n3)์ ์ (์ผ์ค ๋ฐ๋ณต๋ฌธ์ ์ฒ๋ฆฌ)
':: C_ ๐ฉ > ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๋ฃ๊ตฌ์กฐ] by C์ธ์ด #09 [stack ๊ตฌ์กฐ] (0) | 2021.03.30 |
---|---|
[์๋ฃ๊ตฌ์กฐ] by C์ธ์ด -#08 [Quick sort] (0) | 2021.03.29 |
[์๋ฃ๊ตฌ์กฐ] by C์ธ์ด #07 [insertion sort] (0) | 2021.03.28 |
[์๋ฃ๊ตฌ์กฐ] by C์ธ์ด -05- [Merge Sort, Quick Sort] (0) | 2021.03.27 |
[์๋ฃ๊ตฌ์กฐ] #03 by C์ธ์ด [์ฌ๊ทํธ์ถ ์๊ณ ๋ฆฌ์ฆ] (0) | 2021.03.26 |
๋๊ธ