0% found this document useful (0 votes)
26 views

CS Class Lecture 1

sasssasdad

Uploaded by

ss240102222
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

CS Class Lecture 1

sasssasdad

Uploaded by

ss240102222
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

A Beginner’s Guide to Time & Space Complexity

For Middle School Students

Your Name

January 14, 2025

This document is designed for students around Class 8 who are new to the concepts of
algorithmic complexity.

Contents
1 Introduction 1
1.1 Why Should You Care? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

2 Time Complexity 2
2.1 What is Time Complexity? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Big-O Notation (Simplified) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.3 Common Time Complexity Examples . . . . . . . . . . . . . . . . . . . . . . . . . 2

3 Space Complexity 3
3.1 What is Space Complexity? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.2 Example (Array Copy) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

4 Basic Steps to Analyze Your Code 3

5 Why It Matters in Simple Terms 3

6 Practical Tips 4

7 Conclusion 4

1 Introduction
When we write computer programs, we want to know:

1. How fast our program runs (Time Complexity).

2. How much memory (or space) it needs (Space Complexity).

These two ideas help us decide if our solution to a problem is efficient and can be used in
real-world applications or programming contests.

1.1 Why Should You Care?


Even if your program gives the correct answer, it might be too slow and not finish on time. Or
it might use too much memory and crash. By learning time and space complexity, you will:

• Understand how your program’s performance changes as you increase the input size.

1
• Write code that can handle larger problems (like sorting thousands or millions of numbers).

• Prepare for competitions where you must solve problems within strict time and memory
limits.

2 Time Complexity
2.1 What is Time Complexity?
Time Complexity is a way to measure how the running time of your program grows as the size
of the input increases. We usually call the input size n. For example:

• If you have an array (list) of n numbers.

• If you have a string of length n.

• If you have n students’ names to process.

We want to know how many steps or operations our program does in the worst case (most
difficult scenario).

2.2 Big-O Notation (Simplified)


Big-O notation is a short way to say “this is how fast my program grows as n gets very large.”
We write something like O(n) to say “it grows roughly in proportion to n.”
Example (Linear Search):
Imagine you have a list of n numbers, and you want to find if a certain number (target) is
in that list. You look at each number one by one:
1 // Linear Search Example
2 bool linear_search ( int arr [] , int n , int target ) {
3 for ( int i = 0; i < n ; i ++) {
4 if ( arr [ i ] == target ) {
5 return true ; // Found it
6 }
7 }
8 return false ; // Not found
9 }
- In the worst case, you have to look at all n numbers. - Therefore, the time complexity is
O(n).

2.3 Common Time Complexity Examples


You might see these notations:

• O(1): Constant time (instant, like getting the 5th item in an array).

• O(log n): Logarithmic time (like binary search in a sorted list).

• O(n): Linear time (one loop over all data).

• O(n log n): “n log n” time (common in efficient sorting).

• O(n2 ): Quadratic time (double nested loops).

At this level (Class 8), knowing these few examples is enough to start. If you see something
with multiple loops, it might be O(n2 ). If you see a single loop, it might be O(n).

2
3 Space Complexity
3.1 What is Space Complexity?
Space Complexity tells us how much extra memory our program needs, besides the input data.

• If your program only uses a few variables (like counters), that is O(1) space (constant
space).

• If your program creates a new array or list of size n, that is O(n) space.

3.2 Example (Array Copy)

1 // Copy Array Example


2 void copy_array ( int source [] , int n ) {
3 int * newArray = new int [ n ]; // create a new array of size n
4 for ( int i = 0; i < n ; i ++) {
5 newArray [ i ] = source [ i ]; // copy each element
6 }
7 // ...
8 delete [] newArray ;
9 }
Here, we are allocating memory for a new array of size n. So the space complexity is O(n).

4 Basic Steps to Analyze Your Code


1. Spot the Input Size: Identify the main variable that grows, often called n.

2. Count the Loops:

• A single for loop from 0 to n suggests O(n).


• A loop inside another loop (nested loops) might suggest O(n2 ).

3. Look for Extra Data Structures:

• If you make a new list of size n, that is O(n) space.


• If you only use a few extra variables, that might be O(1) space.

4. Apply Big-O Simplification: We usually focus on the largest factor of growth and
ignore smaller details. For example, if your code runs in 2n + 5 steps, we say O(n).

5 Why It Matters in Simple Terms


• Saving Time: If your program has to check every single item (like in a loop of n), it
might be slower than a program that can skip around cleverly (like binary search, which
is O(log n)).

• Saving Memory: Storing lots of data (like n new things) means your computer has
to use more RAM. In some contests, you have a memory limit, and using too much can
disqualify your solution.

3
6 Practical Tips
• Start Small: Try to write a simple program and see how it grows with bigger and bigger
inputs.

• Estimate Before Coding: Ask yourself, “Is this program going to take too long or use
too much memory if n is large?”

• Use Examples:

– If n = 10 (small) vs. n = 100,000 (large), how does your program behave?

• Learn from Known Algorithms: Sorting, searching, and other common tasks already
have well-known time and space complexities.

7 Conclusion
Time and Space Complexity help us see if our programs will be fast enough and use a reasonable
amount of memory. As a Class 8 student:

• Focus on identifying loops, nested loops, and any extra arrays or lists.

• Use Big-O notation to summarize the growth.

• Practice by estimating how your code scales when n becomes large.

Understanding these basics will help you write better, more efficient programs now and in
the future, especially if you continue with more advanced computer science and programming
courses.

Further Reading/Watching:

• Basic videos on “Time Complexity” on YouTube (search for “Big O for Beginners”).

• Simple blog posts or websites like GeeksforGeeks with beginner tutorials.

• Practice small programs on onlinegdb.com or similar sites to observe execution times.

You might also like