Skip to content

Commit

Permalink
Merge pull request #65 from ajay-dhangar/dev-1
Browse files Browse the repository at this point in the history
Dev 1
  • Loading branch information
ajay-dhangar authored Oct 5, 2024
2 parents e0671fa + 452ba8c commit ce156ef
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 57 deletions.
12 changes: 0 additions & 12 deletions blog/2019-05-28-first-blog-post.md

This file was deleted.

7 changes: 0 additions & 7 deletions blog/2019-05-29-long-blog-post.md

This file was deleted.

7 changes: 0 additions & 7 deletions blog/2021-08-01-mdx-blog-post.mdx

This file was deleted.

Binary file not shown.
7 changes: 0 additions & 7 deletions blog/2021-08-26-welcome/index.md

This file was deleted.

35 changes: 14 additions & 21 deletions blog/authors.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
endi:
name: Endilie Yacop Sucipto
title: Maintainer of Docusaurus
url: https://github.com/endiliey
image_url: https://github.com/endiliey.png

yangshun:
name: Yangshun Tay
title: Front End Engineer @ Facebook
url: https://github.com/yangshun
image_url: https://github.com/yangshun.png

slorber:
name: Sébastien Lorber
title: Docusaurus maintainer
url: https://sebastienlorber.com
image_url: https://github.com/slorber.png

ajay-dhangar:
name: Ajay Dhangar
title: Algo maintainer
url: /
image_url: https://github.com/ajay-dhangar.png
title: Founder of CodeHarborHub
url: https://ajay-dhangar.github.io/
image_url: https://avatars.githubusercontent.com/u/99037494?v=4
email: ajaydhangar49@gmail.com
page: true # Turns the feature on
description: >
A passionate developer who loves to code and build new things. I am a Full Stack Developer and a Cyber Security, ML & AI Enthusiast. I am also a Technical Content Writer and a Speaker. I love to share my knowledge with the community. I am the Founder of CodeHarborHub. I am also a Technical Content Writer at GeeksforGeeks. I am a Girl Script Summer of Code 2024 Project Manager (PA).
socials:
x: CodesWithAjay
linkedin: ajay-dhangar
github: ajay-dhangar
stackoverflow: 18530900
newsletter: https://ajay-dhangar.github.io
80 changes: 80 additions & 0 deletions blog/understanding-time-space-complexity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
slug: understanding-time-space-complexity
title: Understanding Time and Space Complexity in Algorithms
authors: [ajay-dhangar]
tags: [ajay-dhangar, algo, dsa, algorithms, time-complexity, space-complexity]
---

When building efficient algorithms, one of the most crucial concepts to grasp is **Time and Space Complexity**. This concept helps us measure the performance of an algorithm in terms of the time it takes to run and the memory it consumes.

<!-- truncate -->

In this blog, we'll break down:

- **Big O Notation**: The mathematical notation used to describe the performance of an algorithm.
- **Common time complexities** like $O(1)$, $O(n)$, and $O(log n)$.
- **Space complexity** and its impact on algorithm design.

---

## What is Time Complexity?

Time complexity is a way to describe how the runtime of an algorithm grows as the size of the input grows. It is denoted using **Big O Notation**, which describes the upper limit of an algorithm's execution time.

### Common Time Complexities:

1. **$O(1)$ - Constant Time:**
- The algorithm's runtime is constant regardless of the input size.
- Example: Accessing an element in an array by its index.

2. **$O(n)$ - Linear Time:**
- The algorithm's runtime grows linearly with the input size.
- Example: Looping through an array of `n` elements.

3. **$O(log n)$ - Logarithmic Time:**
- The algorithm reduces the problem size with each step, making it very efficient for large inputs.
- Example: Binary search on a sorted array.

4. **$O(n^2)$ - Quadratic Time:**
- The runtime grows proportionally to the square of the input size.
- Example: Nested loops, such as comparing each pair of elements in a list.

### Example of Time Complexity Analysis:

Consider a simple loop that prints each element of an array of size `n`:

```javascript title="index.js"
for (let i = 0; i < n; i++) {
console.log(arr[i]);
}
```

The time complexity of this operation is **$O(n)$** because the algorithm's runtime increases linearly with the size of the input array.

---

## What is Space Complexity?

Space complexity refers to the amount of memory an algorithm needs to execute relative to the input size. It includes both the space required for the input data and any additional variables or data structures the algorithm uses.

### Example of Space Complexity:

If an algorithm uses a constant amount of extra space, its space complexity is **$O(1)$**. On the other hand, if the algorithm requires a data structure proportional to the input size (like an extra array of size `n`), the space complexity is **$O(n)$**.

### Space-Time Tradeoff:

In many cases, improving time complexity might require using more memory, and vice versa. For example, **Memoization** techniques in dynamic programming optimize time at the cost of extra space.

---

## Why Does This Matter?

Understanding time and space complexity allows developers to make informed decisions about which algorithms to use based on the problem at hand. It's important to select algorithms that perform well, especially for large inputs.

---

## Final Thoughts

Analyzing the time and space complexity of algorithms is a fundamental skill for optimizing code. While Big O Notation helps in understanding the worst-case scenario, always aim to balance both time and space complexity when designing efficient algorithms.

With this foundation, you're better equipped to analyze and select algorithms for your projects.
6 changes: 3 additions & 3 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ const config = {
/** @type {import('@docusaurus/preset-classic').Options} */
({
docs: {
sidebarPath: "./sidebars.js",
// https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/

sidebarPath: "./sidebars.js",
editUrl: "https://github.com/Ajay-Dhangar/algo/tree/main/",
remarkPlugins: [remarkMath],
rehypePlugins: [rehypeKatex],
},
blog: {
showReadingTime: true,
editUrl: "https://github.com/ajay-dhangar/algo/tree/main/",
remarkPlugins: [remarkMath],
rehypePlugins: [rehypeKatex],
},
theme: {
customCss: "./src/css/custom.css",
Expand Down
23 changes: 23 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"extends": [
"config:base",
"config:recommended",
":dependencyDashboard"
],
"schedule": [
"at any time"
],
"packageRules": [
{
"packagePatterns": ["*"],
"groupName": "all dependencies",
"groupSlug": "all"
}
],
"automerge": false,
"automergeType": "pr",
"prHourlyLimit": 24,
"prConcurrentLimit": 10,
"labels": ["dependencies"],
"reviewers": ["Ajay-Dhangar"]
}

0 comments on commit ce156ef

Please sign in to comment.