-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Time and Space Complexity | ||
|
||
## Time Complexity | ||
|
||
- Measures how the execution time of an algorithm increases with input size (n) | ||
|
||
## Space Complexity | ||
|
||
- Measures how much memory an algorithm needs as input size (n) increases. |
10 changes: 10 additions & 0 deletions
10
data_structures/time_space_complexity/time_complexity/constant_time.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Constant Time - 0(1) | ||
* @description | ||
* - Time doesn't depend on input size | ||
* @param {number[]} arr | ||
*/ | ||
function getFirstElement(arr) { | ||
return arr[0]; | ||
} | ||
console.log(getFirstElement([1, 2, 3])); |
12 changes: 12 additions & 0 deletions
12
data_structures/time_space_complexity/time_complexity/linear_time.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Linear Time - O(n) | ||
* @description | ||
* - Time grows proportionally with input size | ||
* @param {number[]} arr | ||
*/ | ||
function printAll(arr) { | ||
for (let i = 0; i < arr.length; i++) { | ||
console.log('arr --> ', arr[i]); | ||
} | ||
} | ||
printAll([1, 2, 3]); |
20 changes: 20 additions & 0 deletions
20
data_structures/time_space_complexity/time_complexity/logarithmic_time.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Logarithmic Time - O(log n) | ||
* @param {number[]} arr | ||
* @param {number} target | ||
* @returns {number} | ||
*/ | ||
function binarySearch(arr, target) { | ||
let left = 0, | ||
right = arr.length - 1; | ||
|
||
while (left <= right) { | ||
let mid = Math.floor((left + right) / 2); | ||
if (arr[mid] === target) return mid; | ||
arr[mid] < target ? (left = mid + 1) : (right = mid - 1); | ||
} | ||
return -1; | ||
} | ||
|
||
const result = binarySearch([1, 3, 5, 7, 9], 5); | ||
console.log('result', result); |
14 changes: 14 additions & 0 deletions
14
data_structures/time_space_complexity/time_complexity/quadratic_time.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Quadratic Time - O(n²) | ||
* @description | ||
* - Nested Loops cause exponential growth. | ||
* @param {number[]} arr | ||
*/ | ||
function printPair(arr) { | ||
for (let i = 0; i < arr.length; i++) { | ||
for (let j = 0; j < arr.length; j++) { | ||
console.log(arr[i], arr[j]); | ||
} | ||
} | ||
} | ||
printPair([1, 2, 3, 4, 5, 6]); |