big Oํ๊ธฐ๋ฒ์ด๋
์ฌ๋ฌ๊ฐ์ง ์ฝ๋๋ฅผ ์ผ๋ฐ์ ์ผ๋ก ์๋ก ๋น๊ตํ๊ณ ์ฑ๋ฅ์ ํ๊ฐํ๋ ๋ฐฉ๋ฒ. ํ ๋ง๋๋ก ์๊ณ ๋ฆฌ์ฆ์ ํจ์จ์ฑ์ ํ๊ธฐํด์ฃผ๋ ํ๊ธฐ๋ฒ
Big-O(๋๋ Big-Oh) ์๊ณ ๋ฆฌ์ฆ์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๋ํ๋ด๋ ํ๊ธฐ๋ฒ
์ฝ๋๋ฅผ ์งค ๋ ์ค์ํ ์
๋์ ๋ ๊ฐ ๊ตฌํ๊ธฐ
// ๋๋ฆฐver
function addUpTo(n) {
let total = 0;
for (let i = 1; i <= n; i++) {
total += i;
}
return total;
}
var t1 = performance.now();
addUpTo(1000000000);
var t2 = performance.now();
console.log(`Time Elapsed: ${(t2 - t1) / 1000} seconds.`);
// Time Elapsed: 0 seconds.
// ๋น ๋ฅธ ver
function addUpTo(n) {
return (n * (n + 1)) / 2;
}
var time1 = performance.now();
addUpTo(1000000000);
var time2 = performance.now();
console.log(`Time Elapsed: ${(time2 - time1) / 1000} seconds.`);
// Time Elapsed: 0.9598000000119209 seconds.
๋น ๋ฅธ ver์ addUpTo(n)
์ n์ ์ด๋ค ์ซ์๊ฐ ์ค๋๋ผ๋ 3๋ฒ์ ์ฐ์ฐ๋ง ํ๋ฉด ๋๋ค.
(n * (n + 1)) / 2;
: *
+
/
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ์ฐ์ฐ ์๊ฐ์ ๋งค๋ฒ ๊ฑฐ์ ๊ฐ๋ค.
๋น ๋ฅธver O(1)
๋๋ฆฐver O(n)
ex) 1
function countUpAndDown(n) {
console.log('Going up!');
for (let i = 0; i < n; i++) {
console.log(i);
}
// ์๊ฐ๋ณต์ก๋: O(n)
console.log('At the top! \\nGoing down...');
for (let j = n - 1; j >= 0; j--) {
console.log(j);
}
// ์๊ฐ๋ณต์ก๋: O(n)
console.log('Back down. Bye!');
}
Going up!
0
1
...
9
At the top!
Going down...
9
...
1
0
Back down. Bye!
10n
O(n)
์ฐ์ฐ์ countUpAndDown(n)
์ n๊ฐ์ ๋ฐ์ธ๋ฉ๋๋ค.
ex) 2
function printAllPairs(n) {
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
console.log(i, j);
}
// ์๊ฐ๋ณต์ก๋: O(n)
}
// ์๊ฐ๋ณต์ก๋: O(n)
}
์ฌ๊ธฐ์์ big O๋ O(n^2)
big O ํํ ๋จ์ํํ๊ธฐ
O(2n) โ O(n)
O(500) โ O(1)
O(13n^2) โ O(n^2)
O(500)์ ์ฐ์ฐ ๊ฐฏ์๊ฐ ์ด๋ค ์ํฉ์๋ 500๊ฐ๊ฐ ์๋ค๋ ๋ป
function logAtLeast(n) {
for(let i = 1; i <= Math.max(5, n); i++){
console.log(i);
}
}
// ์๊ฐ๋ณต์ก๋: O(n)
function logAtMost5(n) {
for(let i = 1; i <= Math.min(5, n); i++){
console.log(i);
}
}
// ์๊ฐ๋ณต์ก๋: O(1)
function onlyElementsAtEvenIndex(array) {
var newArray = Array(Math.ceil(array.length / 2));
for (var i = 0; i < array.length; i++) {
if (i % 2 === 0) {
newArray[i / 2] = array[i];
}
}
return newArray;
}
// ์๊ฐ๋ณต์ก๋: O(n)
function subtotals(array) {
var subtotalArray = Array(array.length);
for (var i = 0; i < array.length; i++) {
var subtotal = 0;
for (var j = 0; j <= i; j++) {
subtotal += array[j];
}
subtotalArray[i] = subtotal;
}
return subtotalArray;
}
// ์๊ฐ๋ณต์ก๋: O(n^2)
๊ณต๊ฐ๋ณต์ก๋
ex 1)
function sum(arr) {
let total = 0;
for (let i = 0; i < arr.length; i++) {
total += arr[i];
}
return total;
}
// ๊ณต๊ฐ๋ณต์ก๋: O(1)
total
๋ณ์์ 0์ด๋ผ๋ ์ซ์ ํ ๋น
i
๋ณ์์ 0์ด๋ผ๋ ์ซ์ ํ ๋น
๊ทธ๋ฌ๋ฏ๋ก O(1)
๊ณต๊ฐ
ex 2)
function double(arr) {
let newArr = [];
for (let i = 0; i < arr.length; i++) {
newArr.push(2 * arr[i]);
}
return newArr;
}
// ๊ณต๊ฐ๋ณต์ก๋: O(n)
arr ์ ๊ธธ์ด๊ฐ 50์ด๋ฉด newArr์ 50์ ๊ธธ์ด๋งํผ ๋ฆฌํดํ๋ค.
์ฐจ์งํ๋ ๊ณต๊ฐ์ ์
๋ ฅ๋ ๋ฐฐ์ด์ ํฌ๊ธฐ์ ๋น๋กํด์ ์ปค์ง๋ค. ๊ทธ๋ฌ๋ฏ๋ก O(n)
๊ณต๊ฐ
ex 3)
function subtotals(array) {
var subtotalArray = Array(array.length);
for (var i = 0; i < array.length; i++) {
var subtotal = 0;
for (var j = 0; j <= i; j++) {
subtotal += array[j];
}
subtotalArray[i] = subtotal;
}
return subtotalArray;
}
// ๊ณต๊ฐ๋ณต์ก๋: O(n)